Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataAdapter.Fill(Dataset)

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 !

like image 928
Marcus Avatar asked Jun 30 '11 08:06

Marcus


People also ask

Which DataAdapter populate data into DataSet?

The Fill method of the DataAdapter is used to populate a DataSet with the results of the SelectCommand of the DataAdapter . Fill takes as its arguments a DataSet to be populated, and a DataTable object, or the name of the DataTable to be filled with the rows returned from the SelectCommand .

What is difference between DataAdapter and DataSet?

A DataAdapter is used to retrieve data from a data source and populate tables within a DataSet. The DataAdapter also resolves changes made to the DataSet back to the data source. The DataAdapter uses the Connection object of the .


2 Answers

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;
like image 92
abatishchev Avatar answered Oct 05 '22 22:10

abatishchev


You need to do this:

OleDbConnection connection = new OleDbConnection(
    "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Inventar.accdb");
DataSet DS = new DataSet();
connection.Open();

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'))";
OleDbDataAdapter DBAdapter = new OleDbDataAdapter();
DBAdapter.SelectCommand = new OleDbCommand(query, connection); 
DBAdapter.Fill(DS);

By the way, what is this DataSet1? This should be "DataSet".

like image 31
Hasan Fahim Avatar answered Oct 05 '22 23:10

Hasan Fahim