SqlDataAdapter. Update () method and SqlCommandBuilder, sqlcommandbuilder
When using SqlDataAdapter to manipulate a dataset, the most common methods are Fill () and Update.
Fill () is filled with DataSet or able, and Update () is to Update the changes in DataSet or DataTable to the database. If the Update () method is not used, changes to data in DataSet or DataTable do not affect data in the database.
Example: Fill the able with SqlDataAdapter. Here the Employees table in the Northwind database is used.
String strcon1 = "Data Source =.; Initial Catalog = Northwind; Integrated Security = True ";
Using (SqlConnection conn = new SqlConnection (strcon1 ))
{
Conn. Open ();
String strsql = "select * from employees ";
SqlCommand cmd = new SqlCommand (strsql, conn );
Ad = new SqlDataAdapter ();
Ad. SelectCommand = cmd;
Dt = new DataTable ("employees ");
Ad. Fill (dt );
Ds = new DataSet ();
Ds. Tables. Add (dt );
This. dataGridView1.DataSource = ds. Tables [employees];
Add a record:
// Add a new row
DataRow newRow = dt. NewRow ();
NewRow ["EmployeeID"] = 11;
NewRow ["LastName"] = "Bill ";
NewRow ["FirstName"] = "Gata ";
Dt. Rows. Add (newRow );
Update: When Update is used, the system prompts you to have the InsertCommand of SqlDataAdapter. here we can use SqlCommandBuilder to automatically generate
// Use SqlCommandBuilder to automatically generate InsertCommand with Insert statements
SqlCommandBuilder cb = new SqlCommandBuilder (ad );
Ad. InsertCommand = cb. GetInsertCommand ();
// Update the data in DataSet to the database
Ad. Update (ds, "employees ");
}
From: http://blog.csdn.net/highplayer/article/details/6563238