我正在使用 OleDb 查询 Access 数据库。以下查询在 Access 中有效(如嵌套内连接和许多括号所证明的那样,Access 实际上是从设计 GUI 构建查询)。为丑陋的表/列名称道歉。
SELECT DISTINCT Table1.A, Table2.B, Table3.[C]
FROM (Table4 INNER JOIN (Table2 INNER JOIN Table1 ON Table2.ID = Table1.Table2ID) ON Table4.ID = Table1.Table4ID) INNER JOIN ((Table3 INNER JOIN Table5 ON Table3.ID = Table5.[Table3ID]) INNER JOIN Table6 ON Table3.ID = Table6.Table3ID) ON Table1.ID = Table6.Table1ID
WHERE (((Table1.D)=False) AND ((Table1.E)=True)))
它返回许多行。但是,以下代码会导致空数据集。由于代码的设置方式可能存在语法错误(包装类处理其中一些代码行,所以为了简单起见,我不得不解开它)。
// db initialized prior
OleDbConnection con = new OleDbConnection(db);
con.Open();
OleDbCommand cmd = new OleDbCommand(@"
SELECT DISTINCT Table1.A, Table2.B, Table3.[C]
FROM (Table4 INNER JOIN (Table2 INNER JOIN Table1 ON Table2.ID = Table1.Table2ID) ON Table4.ID = Table1.Table4ID) INNER JOIN ((Table3 INNER JOIN Table5 ON Table3.ID = Table5.[Table3ID]) INNER JOIN Table6 ON Table3.ID = Table6.Table3ID) ON Table1.ID = Table6.Table1ID
WHERE (((Table1.D)=False) AND ((Table1.E)=True)))
");
cmd.Connection = con;
DataSet data = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(data);
以这种方式在数据库上执行的其他选择查询返回数据,那么为什么返回一个空数据集呢?