3

好吧,我是一个新手,并且正在尝试编译一个 .NET 应用程序,但是我在重新编译时遇到了很多错误,尤其是这个错误:

只有赋值、调用、递增、递减和新对象表达式可以用作语句

在第 116 行,您有{ SqlDataReader CS;1;0000; }什么建议?

namespace ProBall
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.IO;
    using System.Net;
    using System.Runtime.InteropServices;

    public class DataManager
    {
        public static List<string> barcodes = new List<string>();
        private string connStr;
        private string queryStr;
        public static bool queueMessage;
        public static bool serverStatus = false;

        public DataManager()
        {
        }

        public DataManager(string connStr, string queryStr)
        {
            this.connStr = connStr;
            this.queryStr = queryStr;
        }

        public static IPAddress FindIPAddress(bool localPreference)
        {
            return FindIPAddress(Dns.GetHostEntry(Dns.GetHostName()), localPreference);
        }

        public static IPAddress FindIPAddress(IPHostEntry host, bool localPreference)
        {
            if (host == null)
            {
                throw new ArgumentNullException("host");
            }
            if (host.AddressList.Length != 1)
            {
                foreach (IPAddress address in host.AddressList)
                {
                    bool local = IsLocal(address);
                    if (local && localPreference)
                    {
                        return address;
                    }
                    if (!(local || localPreference))
                    {
                        return address;
                    }
                }
            }
            return host.AddressList[0];
        }

        public string FormatDates(string date)
        {
            string tmpDate = date;
            try
            {
                string[] nDate = null;
                string day = string.Empty;
                string month = string.Empty;
                string year = string.Empty;
                if (tmpDate.Contains("/"))
                {
                    nDate = date.Split(new char[] { '/' });
                    day = nDate[0];
                    month = nDate[1];
                    year = nDate[2].Split(new char[] { ' ' })[0];
                    return (month + "/" + day + "/" + year);
                }
                if (tmpDate.Contains("-"))
                {
                    tmpDate = date.Split(new char[] { ' ' })[0];
                }
            }
            catch (Exception er)
            {
                this.LogError(er.Message, "GENERIC");
            }
            return tmpDate;
        }

        public string GetUserName(string tableName = "employees")
        {
            return this.ReadValue(string.Concat(new object[] { "select id from ", tableName, " where CurrentlyLoggedIn = 1 and LastLoginStation = '", ReturnHostName(), "' and LastLoginStationIP = '", FindIPAddress(true), "'" }), ConfigurationSettings.AppSettings["kcam"]);
        }

        public static bool IsLocal(IPAddress address)
        {
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }
            byte[] addr = address.GetAddressBytes();
            return (((addr[0] == 10) || ((addr[0] == 0xc0) && (addr[1] == 0xa8))) || (((addr[0] == 0xac) && (addr[1] >= 0x10)) && (addr[1] <= 0x1f)));
        }

        public void LogError(string message, string logType = "GENERIC")
        {
            StreamWriter ftmp = new StreamWriter(@"c:\coopnet\server\debug.txt", true);
            ftmp.WriteLine(message);
            ftmp.Close();
        }

        public bool LogOffUser(string tableName = "employees")
        {
            return this.SaveEntityData(string.Concat(new object[] { "update ", tableName, " set CurrentlyLoggedIn = 0  where LastLoginStation = '", ReturnHostName(), "' and LastLoginStationIP = '", FindIPAddress(true), "'" }), ConfigurationSettings.AppSettings["kcam"]);
        }

        public SqlDataReader ReadData()
        {
            SqlDataReader CS;1;0000;
            using (SqlConnection conn = new SqlConnection(this.connStr))
            {
                using (SqlCommand cmd = new SqlCommand(this.queryStr, conn))
                {
                    conn.Open();
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        CS;1;0000 = reader;
                    }
                }
            }
            ; return CS; 1; 0000;
        }

        public bool ReadData(string query)
        {
            bool status = false;
            this.queryStr = query;
            using (SqlConnection conn = new SqlConnection(this.connStr))
            {
                using (SqlCommand cmd = new SqlCommand(this.queryStr, conn))
                {
                    conn.Open();
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            status = true;
                        }
                    }
                    return status;
                }
            }
        }

        public bool ReadData(string query, string connection)
        {
            bool status = false;
            using (SqlConnection conn = new SqlConnection(connection))
            {
                using (SqlCommand cmd = new SqlCommand(query, conn))
                {
                    conn.Open();
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            status = true;
                        }
                    }
                    return status;
                }
            }
        }

        public string ReadValue(string query, string connectionStr)
        {
            string value = string.Empty;
            this.queryStr = query;
            try
            {
                using (SqlConnection conn = new SqlConnection(connectionStr))
                {
                    using (SqlCommand cmd = new SqlCommand(this.queryStr, conn))
                    {
                        conn.Open();
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                reader.Read();
                                return reader[0].ToString();
                            }
                        }
                    }
                    return value;
                }
            }
            catch (Exception)
            {
                return value;
            }
            return value;
        }

        public static string ReturnHostName()
        {
            return Dns.GetHostName();
        }

        public bool SaveData()
        {
            try
            {
                bool status = false;
                using (SqlConnection conn = new SqlConnection(this.connStr))
                {
                    using (SqlCommand cmd = new SqlCommand(this.queryStr, conn))
                    {
                        conn.Open();
                        cmd.ExecuteReader();
                        status = true;
                    }
                }
                return status;
            }
            catch (Exception se)
            {
                this.LogError(se.Message, "GENERIC");
                return false;
            }
        }

        public bool SaveEntityData(string query, string connectionString)
        {
            try
            {
                Func<string, bool> performQuery = delegate (string querySql) {
                    this.connStr = connectionString;
                    this.queryStr = querySql;
                    return this.SaveData();
                };
                return performQuery(query);
            }
            catch (Exception e)
            {
                this.LogError("Error: " + e.Message, "GENERIC");
                return false;
            }
        }
    }
}
4

4 回答 4

4

看起来您正在尝试将该值CS;1;0000;用作变量名,但根据 Language Spec §2.4.2 Identifiers ,这是无效的。您不能在变量名中使用分号,因为它们作为语句的结尾具有特殊含义。如果您将该变量重命名为CS10000CS_1_0000应该可以解决您的问题。

于 2012-10-19T17:48:07.540 回答
2

正如您可能已经猜到的那样:

SqlDataReader CS; 1; 0000;
CS;1;0000 = reader;
; return CS; 1; 0000;

无效。您需要修复此问题才能编译您的代码。

尝试;用下划线或其他东西替换上面的代码:

SqlDataReader CS_1_0000;
CS_1_0000 = reader;
return CS_1_0000;

在 .NET 中,分号表示一行的结尾。这也意味着以下代码将是有效的:

SqlDataReader CS; SqlDataReader cs2;

这一行将创建两个变量。CScs2

于 2012-10-19T17:48:00.730 回答
2

看看你的public SqlDataReader ReadData()方法。它有分号的问题。

SqlDataReader CS;1;0000;不是合法的语法。您不能命名变量 CS;1;10000; 将其称为 CS1000 并重复该方法。

于 2012-10-19T17:47:32.227 回答
0

您应该使用变量名而不是“CS;1;0000;” (例如 tempReader)

于 2012-10-19T17:48:56.783 回答