By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,710 Members | 1,601 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,710 IT Pros & Developers. It's quick & easy.

reflecting changes of DataTable to Database

P: 13
Hi All,

Ive spent days breaking my head over why the code below doesnt update changes to the database. Could someone please help...


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.             OleDbDataAdapter adp = new OleDbDataAdapter("select * from TestTable", connection );
  4.             adp.InsertCommand = new OleDbCommandBuilder(adp).GetInsertCommand();
  5.  
  6.             DataTable dt_dst = new DataTable("TestTable");
  7.             adp.Fill(dt_dst);
  8.  
  9.         DataRow rw = dt_dst.NewRow();
  10.         rw["ID"] =123;
  11.         rw["CAT_ID"] = "adadas";
  12.  
  13.         dt_dst.Rows.Add(rw);
  14.  
  15.  
  16.     dt_dst.AcceptChanges();
  17.     adp.Update(dt_dst);
  18.  
  19.  
Dec 7 '06 #1
Share this Question
Share on Google+
3 Replies


bplacker
100+
P: 121
Hi All,

Ive spent days breaking my head over why the code below doesnt update changes to the database. Could someone please help...


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.             OleDbDataAdapter adp = new OleDbDataAdapter("select * from TestTable", connection );
  4.             adp.InsertCommand = new OleDbCommandBuilder(adp).GetInsertCommand();
  5.  
  6.             DataTable dt_dst = new DataTable("TestTable");
  7.             adp.Fill(dt_dst);
  8.  
  9.         DataRow rw = dt_dst.NewRow();
  10.         rw["ID"] =123;
  11.         rw["CAT_ID"] = "adadas";
  12.  
  13.         dt_dst.Rows.Add(rw);
  14.  
  15.  
  16.     dt_dst.AcceptChanges();
  17.     adp.Update(dt_dst);
  18.  
  19.  
I've never done it this way... the way I've done it before involves writing the insert command out.
so once you have your insert command... just do:

apd.InsertCommand = new SQLCommand(insertSQL)
adp.InsertCommand.Connection = connection
adp.InsertCommand.ExecuteNonQuery()
Dec 7 '06 #2

bplacker
100+
P: 121
You might also try something like this:

Dim addedFields as new DataTable
Dim builder as new OleDBCommandBuilder
builder.DataAdapter = myTableAdapter

newFields = myDataTable.GetChanges(Data.DataRowState.Added)

builder.refreshSchema()
myTableAdapter.UpdateCommand = builder.getUpdateCommand()
'or insert command, I'm not sure.

myTableAdapter.Update(newFields)

myDataTable.AcceptChanges()

please let me know if this works, that might be somewhat useful
Dec 8 '06 #3

P: 37
You might also try something like this:

Dim addedFields as new DataTable
Dim builder as new OleDBCommandBuilder
builder.DataAdapter = myTableAdapter

newFields = myDataTable.GetChanges(Data.DataRowState.Added)

builder.refreshSchema()
myTableAdapter.UpdateCommand = builder.getUpdateCommand()
'or insert command, I'm not sure.

myTableAdapter.Update(newFields)

myDataTable.AcceptChanges()

please let me know if this works, that might be somewhat useful

Hi,

Steps:

Instantiate the DataAdapter
Set the DataAdapter command properties
The DataAdapter class has four properties:

SelectCommand
InsertCommand
UpdateCommand
DeleteCommand

Each of the property perform a specific operation on database.

InsertCommand:
This read-write property returns or sets the SQL statement that will be used whjen rows are inserted into the data source.

DataAdapter needs to retrieve or send data to and from the database, it uses Command objects which must be specified.

This is done by creating command objects, and then assigning them to teh appropriate DataAdapter property:

Ex:

MyAdapter.InsertCommand=comInsert

Updation happens with Update() method of DataAdapter.

Try this book: "Database programming using Vb.net and sql server", [Link Removed]
Regards
Madhv
Dec 11 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.