DataAdapter.Fill(Dataset)
Asked Answered
L

4

11

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 !

Ledaledah answered 30/6, 2011 at 8:53 Comment(2)
Do you have any tables in dataset? I think you may need to add some tables with corresponding structureUdela
@Blabla: It's not required. Schema is populated automaticallyLashawnda
L
21
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;
Lashawnda answered 30/6, 2011 at 9:2 Comment(9)
@abatishchev: That is just a recommendation. Isn't it? Does it solve the problem ??Mehetabel
@Akram: If the query is correct, this code will do the work 100%Lashawnda
@abatishchev: Still not specifying the problem, nor providing a solution for it. I think it is not an answer?Mehetabel
and how can i get the values ? ds.Tables[0].Rows[0].ItemArray[0] don´t workLedaledah
@User: Do you need only the value of first column in first row?Lashawnda
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 hintLedaledah
@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 arrLashawnda
then use arr[0]["Columnname"] for access ?Ledaledah
DataRow provides strongly typed access via .Field<T>(): int value = arr[0].Field<int>("Columnname");.Settlings
S
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".

Sheepfold answered 30/6, 2011 at 8:58 Comment(7)
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?Hornbill
i´m with Reniuz, i think the new code brings no changes on logicLedaledah
@User: Please don't propose such edits. Just edit your own post (question)Lashawnda
@ Akram Shahda and @Reniuz. The problem is just a typo I think. Rather than writing DataSet, the user had written DataSet1.Sheepfold
how to get access more comfortable as ds.Tables[0].Rows[0].ItemArray ? in this way the object[] has no Columnnames.Ledaledah
@user820831. Do you want to access data from the dataset?Sheepfold
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.Sheepfold
S
0
leDbConnection 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);
Stereotyped answered 12/11, 2014 at 15:26 Comment(1)
Your answer should contain an explanation of your code and a description how it solves the problem.Stout
H
0

it works for me, just change: Provider=Microsoft.Jet.OLEDB.4.0 (VS2013)

OleDbConnection connection = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Z:\\GENERAL\\OFMPTP_PD_SG.MDB");
DataSet DS = new DataSet();
connection.Open();

string query =
@"SELECT * from MONTHLYPROD";
OleDbDataAdapter DBAdapter = new OleDbDataAdapter();
DBAdapter.SelectCommand = new OleDbCommand(query, connection);
DBAdapter.Fill(DS);

Luis Montoya

Hispaniola answered 17/7, 2018 at 13:40 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.