Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save changes in database using BindingSource

Tags:

c#

sql

ado.net

I retrieve data from database like this:

OleDbDataAdapter dataAdapter 
            = new OleDbDataAdapter("SELECT * "
              + " FROM myTab1, myTab2"
              + " WHERE myTab1.col1 = myTab2.col3"
              , connection);//OleDbConnection connection = new OleDbConnection();
DataTable dataTable = new DataTable("myDataTable");
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;
dataAdapter.Fill(dataTable);  

Then I use bindingSource in order to access the data in my program, everything works perfect. But after all changes I've made in bindingSource, I need so save them into the database. How can I do this?

like image 616
Romz Avatar asked Dec 05 '25 19:12

Romz


1 Answers

Well normally you'd call Update() on the DataAdapter:

dataAdapter.Update(dataTable);

But since your query "joins" two tables it may not be so easy. It may be possible if you turn your SELECT query into a proper join:

  "SELECT * "
+ "FROM myTab1 "
+ "JOIN myTab2 ON myTab1.col1 = myTab2.col3"

Some other alternatives:

  • Write an UPDATE statement that will put the right values in the right tables and set your dataAdapter's UpdateStatement
  • Populate the dataset with separate tables (one per base table) and join in your app

More info from MSDN:

http://msdn.microsoft.com/en-us/library/xzb1zw3x(v=vs.80).aspx

like image 91
D Stanley Avatar answered Dec 07 '25 09:12

D Stanley