Dataset update

  • Thread starter Thread starter pei_world
  • Start date Start date
P

pei_world

Hi all,

I have a dataset that filled with many table by some relations, how can I
update those tables when My dataset changed?

give sample code if possible, many thanks!

pei
 
You can use a SqlDataAdapter (or OleDbDataAdapter if you're not use SQL
Server) to accomplish this. It would go something like this:

SqlConnection con = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(sqlStatement, con); // where
sqlStatement is your SQL/sp to populate the dataset
adapter.TableMappings.Add("Blah", "dataBlah"); // mapping between source
table and data table
adapter.DeleteCommand = con.CreateCommand();
adapter.DeleteCommand.CommandText = "..."; // sql command for delete
adapter.DeleteCommand.Parameters.Add("..."); // if your sql command takes
any parameters
// do the same for InsertCommand, UpdateCommand, and SelectCommand as
applicable

adapter.Fill(dataset);

// code that makes changes to dataset...

adapter.Update(dataset);

See
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemDataSqlClientSqlDataAdapterClassTopic.asp for more detailed
information.

hth

-Joel
--------------------
 
Back
Top