I have this code in one solution, and it works fine:
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++;
}
}
}
}
}
I have this very similar code in another:
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;
}
...and it coughs up 26 Errors, such as:
"; expected"
on this line and others:
using (var conn = new OleDbConnection(connStr))
-and "} expected" right after the opening "{"
--and "A namespace cannot directly contain members such as fields or methods"
on this line:
departments.Add(new Department { Id = i, AccountId = oleDbD8aReader.GetInt16(0), Name = oleDbD8aReader.GetString(1) });
-and "Invalid token '!=' in class, struct, or interface member declaration" on this line:
while (oleDbD8aReader != null && oleDbD8aReader.Read())
...as well as many other "invalid token" errors for "(" and "," and ";" and "{" and "=" and "using"
-and two "Type or namespace definition, or end-of-file expected"
If I comment out all the code except a return statement so that it is just:
public IEnumerable<Department> GetDepartments()
{
return null;
}
...it compiles fine.
Why is what is good for the goose not good for the gander here?