Note: This method is only applicable to a single table.
Private sqldataadapter adapter = NULL;
Private sqlcommandbuilder builder = NULL;
Private dataset = NULL;
Private sqlconnection connection = NULL;
Private void btnsave_click (Object sender, eventargs E)
{
// Code to modify data in dataset here
// Dataset. Tables [0]. Rows [0] [2] = 9; // modifications made on the datagridview interface are automatically updated to dataset.
Builder. getupdatecommand ();
// Without the sqlcommandbuilder this line wocould fail
Int COUNT = Adapter. Update (dataset, "Table1 ");
}
Private void btnquery_click (Object sender, eventargs E)
{
Connection = new sqlconnection ("Server = 10.138.141.84; uid = sa; Password = sa; database = mytest ");
Adapter = new sqldataadapter ();
Adapter. selectcommand = new sqlcommand ("select * From Table1 where id1 = 1", connection );
Builder = new sqlcommandbuilder (adapter );
Connection. open ();
// Generate the Chinese name of the column
Adapter. tablemappings. Add ("Table1", "Table1 ");
Adapter. tablemappings [0]. columnmappings. Add ("id1", "user ");
Adapter. tablemappings [0]. columnmappings. Add ("ID2", "name ");
Adapter. tablemappings [0]. columnmappings. Add ("Val", "Age ");
Dataset = new dataset ();
Adapter. fill (dataset, "Table1"); // The fill method must contain the second parameter; otherwise, the adapter. update (dataset, "Table1") will prompt that the table "Table1" cannot be found"
This. datagridview1.datasource = dataset. Tables [0];
// Return dataset;
}
Unfortunately, this method currently only supports single-Table operations and does not support any operations related to multiple tables (including columns from different tables in the same view ).