DataAdapter.Fill方法(数据集) [英] DataAdapter.Fill(Dataset)

查看:297
本文介绍了DataAdapter.Fill方法(数据集)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到一些数据从Access数据库通过OLEDB在数据集。 但数据集为空后,填写()方法。同样的语句的工作,返回1行,当我在D中手动触发它们*。

  OleDbConnection的连接=
   新的OleDbConnection(供应商= Microsoft.ACE.OLEDB.12.0;数据源= Inventar.accdb);
数据集1 DS =新数据集1();
connection.Open();

OleDbDataAdapter的DBAdapter =新OleDbDataAdapter的(
    @SELECT tbl_Computer *,tbl_Besitzer。*
      从tbl_Computer
      INNER JOIN tbl_Besitzer ON tbl_Computer.FK_Benutzer = tbl_Besitzer.ID
      WHERE(((tbl_Besitzer.Vorname)='马'));,
    连接);

DBAdapter.Fill(DS);
 

在此先感谢。

新的工作code:

 的DataSet ds为新的DataSet();
OleDbDataAdapter的DBAdapter =新OleDbDataAdapter的();

OleDbConnection的连接=新的OleDbConnection(供应商= Microsoft.ACE.OLEDB.12.0;数据源= Inventar.accdb);
查询字符串= @
    选择tbl_Computer *,tbl_Besitzer。*
    从tbl_Computer
    INNER JOIN tbl_Besitzer ON tbl_Computer.FK_Benutzer = tbl_Besitzer.ID
    WHERE(((tbl_Besitzer.Vorname)='马'));;

connection.Open();

使用(OleDbCommand的命令=新的OleDbCommand(查询,连接))
使用(OleDbDataAdapter的适配器=新OleDbDataAdapter的(命令))
{
    adapter.Fill(DS);
}

字典<字符串,字符串> DictValues​​ =新字典<字符串,字符串>();

的for(int i = 0; I< = ds.Tables [0] .Rows [0] .ItemArray.Length  -  1;我++)
{
    的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());
}
 

现在正确的code上面贴了,有Dictonary访问数据更加舒适。 希望有人找到这个职位帮助。谢谢大家得到它的工作!

解决方案

 的DataSet ds为新的DataSet();

使用(OleDbConnection的连接=新的OleDbConnection(的connectionString))
使用(OleDbCommand的命令=新的OleDbCommand(查询,连接))
使用(OleDbDataAdapter的适配器=新OleDbDataAdapter的(命令))
{
    adapter.Fill(DS);
}

返回DS;
 

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 !

解决方案

DataSet ds = new DataSet();

using (OleDbConnection connection = new OleDbConnection(connectionString))
using (OleDbCommand command = new OleDbCommand(query, connection))
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
    adapter.Fill(ds);
}

return ds;

这篇关于DataAdapter.Fill方法(数据集)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆