-8

我在一个解决方案中有这个代码,它工作正常:

        private readonly List<Department> departments = new List<Department>();

        . . .

        private void LoadDepartments(string serialNum)
        {
            string dbContext = HandheldServerUtils.GetDBContextForSerialNum(serialNum);
            string connStr =
                string.Format(
                    @"Provider=Microsoft.ACE.OLEDB.12.0;User ID=NRBQSP;Password=NRBQSP;Data Source=C:\CCRWin\DATA\CCRDAT{0}.MDB;Jet OLEDB:System database=C:\CCRWin\Data\sscs.mdw",
                    dbContext);
            using (var conn = new OleDbConnection(connStr))
            {
                using (OleDbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText =
                        @"SELECT t_accounts.account_no as AccountID, IIF(ISNULL(t_accounts.name),'[blank]',t_accounts.name) AS Name 
                        FROM t_accounts 
                        INNER JOIN td_department_accounts ON (t_accounts.account_no = 
td_department_accounts.account_no) where type = 'DE'";
                    cmd.CommandType = CommandType.Text;
                    conn.Open();
                    int i = 1;
                    using (OleDbDataReader oleDbD8aReader = cmd.ExecuteReader())
                    {
                        while (oleDbD8aReader != null && oleDbD8aReader.Read())
                        {
                            string accountNum = oleDbD8aReader.GetString(0);
                            string deptName = oleDbD8aReader.GetString(1);
                            Add(new Department {Id = i, AccountId = accountNum, Name = deptName});
                            i++;
                        }
                    }
                }
            }
        }

我在另一个中有这个非常相似的代码:

    public IEnumerable<Department> GetDepartments()
    {
        private readonly List<Department> departments = new List<Department>();
        string dbContext = "42"; 
        string connStr =
            string.Format(
                @"Provider=Microsoft.ACE.OLEDB.12.0;User ID=NRBQSP;Password=NRBQSP;Data Source=C:\CCRWin\DATA\CCRDAT{0}.MDB;Jet OLEDB:System database=C:\CCRWin\Data\sscs.mdw",
                dbContext);
        using (var conn = new OleDbConnection(connStr))
        {
            using (OleDbCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText =
                    @"SELECT t_accounts.account_no as AccountID, IIF(ISNULL(t_accounts.name),'[blank]',t_accounts.name) AS Name 
                    FROM t_accounts 
                    INNER JOIN td_department_accounts ON (t_accounts.account_no = td_department_accounts.account_no) where type = 'DE'";
                cmd.CommandType = CommandType.Text;
                conn.Open();
                int i = 1;
                using (OleDbDataReader oleDbD8aReader = cmd.ExecuteReader())
                {
                    while (oleDbD8aReader != null && oleDbD8aReader.Read())
                    {
                        string accountNum = oleDbD8aReader.GetString(0);
                        string deptName = oleDbD8aReader.GetString(1);
                        departments.Add(new Department { Id = i, AccountId = oleDbD8aReader.GetInt16(0), Name = oleDbD8aReader.GetString(1) });
                        i++;
                    }
                }                    
            }
        }
        return departments;
    }

...它会咳出 26 个错误,例如:

“; 预期的”

在这条线上和其他线上:

using (var conn = new OleDbConnection(connStr))

- 和"} 预期"在开头 "{" 之后

--and “命名空间不能直接包含字段或方法等成员”

在这条线上:

departments.Add(new Department { Id = i, AccountId = oleDbD8aReader.GetInt16(0), Name = oleDbD8aReader.GetString(1) });

- 和此行上的“类、结构或接口成员声明中的无效标记 '!=' ”:

while (oleDbD8aReader != null && oleDbD8aReader.Read())

...以及“(”和“,”和“;”和“{”和“=”和“使用”的许多其他“无效令牌”错误

- 和两个“类型或命名空间定义,或预期的文件结尾

如果我注释掉除 return 语句之外的所有代码,那么它就是:

public IEnumerable<Department> GetDepartments()
{
    return null;
}

...它编译得很好。

为什么这里对鹅有好处的东西对鹅没有好处?

4

1 回答 1

4

这条线...

private readonly List<Department> departments = new List<Department>();

... 不应在函数中,而应在类范围之外,或使其成为var.

于 2014-07-15T17:10:59.080 回答