/*Updating a database table in a dataset * Note: When updating a database table with a dataset, the table must specify a primary key or a unique column*/ stringConnString ="Data source= (local); Initial catalog=linq;integrated Security=sspi";//log in with a Windows userusing(SqlConnection conn =NewSqlConnection (connstring)) {Conn. Open (); using(SqlCommand cmd =Conn. CreateCommand ()) {Cmd.commandtext="select * FROM Orders"; DataSet DataSet=NewDataSet (); SqlDataAdapter Adapter=NewSqlDataAdapter (CMD); /*populate the DataSet with the results of the query and specify a surface: Orders * NOTE: * If you do not specify a table name, use adapter directly. Fill (DataSet) is populated by the way the DataTable is received according to the index number * DataTable table = DataSet. Tables[0]; */adapter. Fill (DataSet,"orders"); DataTable Table= DataSet. tables["orders"]; //update the city of the first row of data to "Panzhihua"DataRow row = table. rows[0]; row[" City"] ="Panzhihua"; /*Builder: Automatically create SqlCommand when users update the database * to view: * Builder. GetUpdateCommand (); * Builder. GetInsertCommand (); * Builder. GetDeleteCommand (); */SqlCommandBuilder Builder=NewSqlCommandBuilder (adapter); /*There are several ways to update here: * Adapter. Update (DataSet, "Orders"); Specifies the table name of the dataset updated * adapter. Update (table "), Updating table * adapter. Update (dataset), updating the entire dataset directly*/adapter. Update (DataSet,"orders"); Console.WriteLine ("Update Successful"); Console.readkey (); }}
Updating a database table with a dataset