DataAdapter.Fill(数据集)
我尝试通过 DataSet
中的 OleDB 从 Access 数据库获取一些数据。 但在执行 Fill()
方法之后,DataSet
为空。当我在 D* 中手动触发它们时,相同的语句有效并返回 1 行。
OleDbConnection connection =
new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Inventar.accdb");
DataSet1 DS = new DataSet1();
connection.Open();
OleDbDataAdapter DBAdapter = new OleDbDataAdapter(
@"SELECT tbl_Computer.*, tbl_Besitzer.*
FROM tbl_Computer
INNER JOIN tbl_Besitzer ON tbl_Computer.FK_Benutzer = tbl_Besitzer.ID
WHERE (((tbl_Besitzer.Vorname)='ma'));",
connection);
DBAdapter.Fill(DS);
提前致谢。
新的工作代码:
DataSet ds = new DataSet();
OleDbDataAdapter DBAdapter = new OleDbDataAdapter();
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Inventar.accdb");
string query = @"
SELECT tbl_Computer.*, tbl_Besitzer.*
FROM tbl_Computer
INNER JOIN tbl_Besitzer ON tbl_Computer.FK_Benutzer = tbl_Besitzer.ID
WHERE (((tbl_Besitzer.Vorname)='ma'));";
connection.Open();
using (OleDbCommand command = new OleDbCommand(query, connection))
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
adapter.Fill(ds);
}
Dictionary<string, string> DictValues = new Dictionary<string, string>();
for (int i = 0; i <= ds.Tables[0].Rows[0].ItemArray.Length - 1; i++)
{
MessageBox.Show(ds.Tables[0].Rows[0].ItemArray[i] + " -- " + ds.Tables[0].Rows[0].Table.Columns[i]);
DictValues.Add(ds.Tables[0].Rows[0].Table.Columns[i].ToString(), ds.Tables[0].Rows[0].ItemArray[i].ToString());
}
现在正确的代码已发布在上面,并使用字典来更舒适地访问数据。 希望任何人都能在这篇文章中找到帮助。谢谢大家让它工作!
i try to get some Data from a Access Database via OleDB in a DataSet
.
But the DataSet
is empty after the Fill()
method. The same statement works and return 1 row when i trigger them manually in D*.
OleDbConnection connection =
new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Inventar.accdb");
DataSet1 DS = new DataSet1();
connection.Open();
OleDbDataAdapter DBAdapter = new OleDbDataAdapter(
@"SELECT tbl_Computer.*, tbl_Besitzer.*
FROM tbl_Computer
INNER JOIN tbl_Besitzer ON tbl_Computer.FK_Benutzer = tbl_Besitzer.ID
WHERE (((tbl_Besitzer.Vorname)='ma'));",
connection);
DBAdapter.Fill(DS);
Thanks in advance.
New working code:
DataSet ds = new DataSet();
OleDbDataAdapter DBAdapter = new OleDbDataAdapter();
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Inventar.accdb");
string query = @"
SELECT tbl_Computer.*, tbl_Besitzer.*
FROM tbl_Computer
INNER JOIN tbl_Besitzer ON tbl_Computer.FK_Benutzer = tbl_Besitzer.ID
WHERE (((tbl_Besitzer.Vorname)='ma'));";
connection.Open();
using (OleDbCommand command = new OleDbCommand(query, connection))
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
adapter.Fill(ds);
}
Dictionary<string, string> DictValues = new Dictionary<string, string>();
for (int i = 0; i <= ds.Tables[0].Rows[0].ItemArray.Length - 1; i++)
{
MessageBox.Show(ds.Tables[0].Rows[0].ItemArray[i] + " -- " + ds.Tables[0].Rows[0].Table.Columns[i]);
DictValues.Add(ds.Tables[0].Rows[0].Table.Columns[i].ToString(), ds.Tables[0].Rows[0].ItemArray[i].ToString());
}
Now The Right code is posted above, with an Dictonary to access the Data more comfortable.
hope anybody find help in this post. Thank you all for get it work !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要这样做:
顺便问一下,这个 DataSet1 是什么?这应该是“数据集”。
You need to do this:
By the way, what is this DataSet1? This should be "DataSet".
它对我有用,只需更改:Provider=Microsoft.Jet.OLEDB.4.0 (VS2013)
Luis Montoya
it works for me, just change: Provider=Microsoft.Jet.OLEDB.4.0 (VS2013)
Luis Montoya