从 ms access 返回多个结果集到 ado.net
嘿伙计们,我想从我的 MS Access 数据库中在 1 个 ado.net 调用中获取 3 个表,但是当我尝试这样做时,当我将 sql 查询更改为仅获取 1 个表时,我收到错误,
我的代码工作正常
可以有人让我知道如何通过 ms access 实现此目的吗? 因为我多年来一直在使用 sql server 这样做,没有任何问题。 也许 access 不支持多个结果集? 我在访问方面没有做太多工作。 请帮忙。 下面是我的参考代码:
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DatabaseFile.mdb;Persist Security Info=True");
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter("SELECT * FROM Table1; SELECT * FROM Table2; SELECT * FROM Table3;", con);
DataSet ds = new DataSet();
da.Fill(ds);
更新:伙计们,这看起来不可能。 所以我不得不编写非常愚蠢的代码来获得我想要的东西,完全浪费计算资源:
DataSet ds = new DataSet();
ds.Tables.Add(new DataTable("Table1"));
ds.Tables.Add(new DataTable("Table2"));
ds.Tables.Add(new DataTable("Table3"));
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DatabaseFile.mdb;Persist Security Info=True");
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter("SELECT * FROM Table1;", con);
da.Fill(ds, "Table1");
da.SelectCommand.CommandText = "SELECT * FROM Table2;";
da.Fill(ds, "Table2");
da.SelectCommand.CommandText = "SELECT * FROM Table3;";
da.Fill(ds, "Table3");
hey guys i want to fetch 3 tables in 1 single ado.net call from my ms access database, however i get an error when i am trying to do that
when i change my sql query to just fetch 1 table, my code works fine
can anyone let me know how to achieve this with ms access? because i have been doing this with sql server since ages without any problems. perhaps access does not support multiple result sets? i have not worked much with access. please help. below is my code for reference:
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DatabaseFile.mdb;Persist Security Info=True");
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter("SELECT * FROM Table1; SELECT * FROM Table2; SELECT * FROM Table3;", con);
DataSet ds = new DataSet();
da.Fill(ds);
UPDATE: guys this does not look possible. so i had to write really stupid code to get what i wanted, complete waste of computing resources:
DataSet ds = new DataSet();
ds.Tables.Add(new DataTable("Table1"));
ds.Tables.Add(new DataTable("Table2"));
ds.Tables.Add(new DataTable("Table3"));
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DatabaseFile.mdb;Persist Security Info=True");
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter("SELECT * FROM Table1;", con);
da.Fill(ds, "Table1");
da.SelectCommand.CommandText = "SELECT * FROM Table2;";
da.Fill(ds, "Table2");
da.SelectCommand.CommandText = "SELECT * FROM Table3;";
da.Fill(ds, "Table3");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,如果您的数据驻留在 Access mdb 中,则不能拥有多个数据集。 如果您使用 Access 连接到外部数据源,则可以使用传递查询并执行此操作,但我认为这不是您的情况。 (请参阅http://support.microsoft.com/kb/126992)
as far as I know, if your data reside inside the Access mdb, you cannot have multiple data sets. If instead you use Access to connect to an external data source, you could use pass-through queries and do that, but I do not think this is your case. (see http://support.microsoft.com/kb/126992)