DataAdapter.Fill(Dataset)

112,871

Solution 1

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;

Solution 2

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".

Share:
112,871
Marcus
Author by

Marcus

Updated on July 09, 2022

Comments

  • Marcus
    Marcus almost 2 years

    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 !

  • Renatas M.
    Renatas M. almost 13 years
    Sorry I cant see differences, OleDbDataAdapter constructor takes first parameter as select command text and it is used by SelectCommand. looks like you wrote same thing in different way. Can you explain?
  • Akram Shahda
    Akram Shahda almost 13 years
    @abatishchev: That is just a recommendation. Isn't it? Does it solve the problem ??
  • abatishchev
    abatishchev almost 13 years
    @Akram: If the query is correct, this code will do the work 100%
  • Marcus
    Marcus almost 13 years
    i´m with Reniuz, i think the new code brings no changes on logic
  • abatishchev
    abatishchev almost 13 years
    @User: Please don't propose such edits. Just edit your own post (question)
  • Akram Shahda
    Akram Shahda almost 13 years
    @abatishchev: Still not specifying the problem, nor providing a solution for it. I think it is not an answer?
  • Marcus
    Marcus almost 13 years
    and how can i get the values ? ds.Tables[0].Rows[0].ItemArray[0] don´t work
  • abatishchev
    abatishchev almost 13 years
    @User: Do you need only the value of first column in first row?
  • Hasan Fahim
    Hasan Fahim almost 13 years
    @ Akram Shahda and @Reniuz. The problem is just a typo I think. Rather than writing DataSet, the user had written DataSet1.
  • Marcus
    Marcus almost 13 years
    how to get access more comfortable as ds.Tables[0].Rows[0].ItemArray ? in this way the object[] has no Columnnames.
  • Hasan Fahim
    Hasan Fahim almost 13 years
    @user820831. Do you want to access data from the dataset?
  • Marcus
    Marcus almost 13 years
    i need the format same as in the db. i put some code in my question you can have a look it´s alpha :-) hope you can give me a hint
  • Hasan Fahim
    Hasan Fahim almost 13 years
    To access data, do this ds.Tables[0].Rows[0][0]. This would return the data present in the first row / first column of the first table.
  • abatishchev
    abatishchev almost 13 years
    @User: I see you call ds.Tables[0].Rows[0] every time. Don't do that; cache instead: var arr = ds.Tables[0].Rows[0].ItemArray and next use just arr
  • Marcus
    Marcus almost 13 years
    then use arr[0]["Columnname"] for access ?
  • AbcAeffchen
    AbcAeffchen over 9 years
    Your answer should contain an explanation of your code and a description how it solves the problem.
  • D M
    D M over 2 years
    DataRow provides strongly typed access via .Field<T>(): int value = arr[0].Field<int>("Columnname");.