为什么这个代码在一个解决方案中编译而在另一个解决方

时间:2014-07-15 17:08:18

标签: c# ienumerable oledbconnection oledbcommand oledbdatareader

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

        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;
}

......编译得很好。

为什么鹅对这对雄鹅不利有益?

1 个答案:

答案 0 :(得分:3)

这一行...

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

...不应该在函数中,而应该在类外,在类范围内,或者使它成为var