OleDbDataAdapter and more tables
-
Hi, I want to use DataSet and OleDbDataAdapter for manipulating some tables of a database. I know that when I want to do it for a table I can use below code:
{ DataSet custDS = new DataSet(); OleDbConnection myConn = new OleDbConnection(myConnection); OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(); myDataAdapter.SelectCommand = new OleDbCommand(); myDataAdapter.SelectCommand.Connection = myConnection; OleDbCommandBuilder custCB = new OleDbCommandBuilder(myDataAdapter); myDataAdapter.SelectCommand.CommandText = "Select * from Customers"; myDataAdapter.Fill(custDS, "Customers"); //code to modify data in dataset here //Without the OleDbCommandBuilder this line would fail myDataAdapter.Update(custDS, "Customers"); }
but when I want to use more tables I have, some problems:{ DataSet custDS = new DataSet(); OleDbConnection myConn = new OleDbConnection(myConnection); OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(); myDataAdapter.SelectCommand = new OleDbCommand(); myDataAdapter.SelectCommand.Connection = myConnection; OleDbCommandBuilder custCB = new OleDbCommandBuilder(myDataAdapter); myDataAdapter.SelectCommand.CommandText = "Select * from Customers"; myDataAdapter.Fill(custDS, "Customers"); myDataAdapter.SelectCommand.CommandText = "Select * from Orders"; myDataAdapter.Fill(custDS, "Orders"); myDataAdapter.SelectCommand.CommandText = "Select * from Products"; myDataAdapter.Fill(custDS, "Products"); //code to modify data in dataset here //Without the OleDbCommandBuilder this line would fail myDataAdapter.Update(custDS, "Customers"); myDataAdapter.Update(custDS, "Orders"); myDataAdapter.Update(custDS, "Products"); }
Because the myDataAdapter only gives one SelectCommand and CommandBuilder generates some commannds for one of tables. So myDataAdapter.Update method works for one of tables. Can anyone help me please? How can I use OleDbDataAdpter, DataSet and CommandBuilder to solve my problem? -
Hi, I want to use DataSet and OleDbDataAdapter for manipulating some tables of a database. I know that when I want to do it for a table I can use below code:
{ DataSet custDS = new DataSet(); OleDbConnection myConn = new OleDbConnection(myConnection); OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(); myDataAdapter.SelectCommand = new OleDbCommand(); myDataAdapter.SelectCommand.Connection = myConnection; OleDbCommandBuilder custCB = new OleDbCommandBuilder(myDataAdapter); myDataAdapter.SelectCommand.CommandText = "Select * from Customers"; myDataAdapter.Fill(custDS, "Customers"); //code to modify data in dataset here //Without the OleDbCommandBuilder this line would fail myDataAdapter.Update(custDS, "Customers"); }
but when I want to use more tables I have, some problems:{ DataSet custDS = new DataSet(); OleDbConnection myConn = new OleDbConnection(myConnection); OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(); myDataAdapter.SelectCommand = new OleDbCommand(); myDataAdapter.SelectCommand.Connection = myConnection; OleDbCommandBuilder custCB = new OleDbCommandBuilder(myDataAdapter); myDataAdapter.SelectCommand.CommandText = "Select * from Customers"; myDataAdapter.Fill(custDS, "Customers"); myDataAdapter.SelectCommand.CommandText = "Select * from Orders"; myDataAdapter.Fill(custDS, "Orders"); myDataAdapter.SelectCommand.CommandText = "Select * from Products"; myDataAdapter.Fill(custDS, "Products"); //code to modify data in dataset here //Without the OleDbCommandBuilder this line would fail myDataAdapter.Update(custDS, "Customers"); myDataAdapter.Update(custDS, "Orders"); myDataAdapter.Update(custDS, "Products"); }
Because the myDataAdapter only gives one SelectCommand and CommandBuilder generates some commannds for one of tables. So myDataAdapter.Update method works for one of tables. Can anyone help me please? How can I use OleDbDataAdpter, DataSet and CommandBuilder to solve my problem?mkomasi wrote: OleDbCommandBuilder custCB = new OleDbCommandBuilder(myDataAdapter); myDataAdapter.SelectCommand.CommandText = "Select * from Customers"; myDataAdapter.Fill(custDS, "Customers"); myDataAdapter.SelectCommand.CommandText = "Select * from Orders"; myDataAdapter.Fill(custDS, "Orders"); myDataAdapter.SelectCommand.CommandText = "Select * from Products"; myDataAdapter.Fill(custDS, "Products"); You need to run the COmmandBuilder each time after you change your "select" statement. But then u have a problem with the Update (you will have to do the whole thing again). I recommend setting up a dataadapter for each table. MyDUMeter: a .NET DUMeter clone