The data in the DataTable can be modified and updated to the database at the same time, which is convenient. The following code is implemented: DataTabletablenewDataTable (); initialize a DataTable object stringsqlconnectionstringcececomputername-PC; InitialCatalogcharge_sys; UserIDsa;
The data in the DataTable can be modified and updated to the database at the same time, which is convenient. The following code is implemented: DataTable table = new DataTable (); // initialize a DataTable object string sqlConnectionString = Data Source = ComputerName-PC; Initial Catalog = charge_sys; User ID = sa;
The data in the DataTable can be modified and updated to the database at the same time, which is convenient. The following is the implementation code:
DataTable table = new DataTable (); // initialize a DataTable object string sqlConnectionString = "Data Source = ComputerName-PC; Initial Catalog = charge_sys; User ID = sa; PWD = 123456; "; SqlConnection sqlConnection = new SqlConnection (sqlConnectionString); // connect to the database SqlCommand sqlCommand = new SqlCommand (" select * from testB ", sqlConnection); SqlDataAdapter sqlAdap = new SqlDataAdapter (sqlCommand ); sqlCommandBuilder sqlBuilder = new SqlCommandBuilder (sqlAdap); // This sentence is very important, as if it was used for batch update // DataSet dtst = new DataSet (); dataSet can also be used for completion, the corresponding implementation code sqlConnection is as follows. open (); sqlAdap. fill (table); // table = dtst. tables ["testB"]; DataRow dr = table. rows [0]; // The first line // dr. beginEdit (); // dr. endEdit (); it seems that the two statements are mainly used for (int I = 0; I <table. rows. count; I ++) // modify the content of each row in the table {dr = table. rows [I]; dr ["f"] = 3; // The field name is f} // a primary key must exist in the testB table, otherwise, it is very important that you cannot update/******* sqlAdap. update (table); // sqlAdap. update (dtst, "testB"); sqlConnection. close (); MessageBox. show ("aa ");
The following is the able bound to the dataGridView control. When the data in the dataGridView dview changes, update the database code:
DataTable and dataGridView control binding code:
Private void Form1_Load (object sender, EventArgs e) {string sqlConnectionString = "Data Source = ComputerName-PC; Initial Catalog = charge_sys; User ID = sa; PWD = 123456 ;"; sqlConnection sqlConnection = new SqlConnection (sqlConnectionString); // initialize sqlconnection DataTable table = new DataTable (); SqlDataAdapter sqlAdap = new SqlDataAdapter ("select * from testB", sqlConnection); sqlConnection. open (); sqlAdap. fill (table); sqlConnection. close (); // display the data in Table testB in the dview this. dataGridView1.DataSource = table ;}
Update the changes in the dataGridView to the database code:
Private void butUpdate_Click (object sender, EventArgs e) {DataTable table = new DataTable (); table = (DataTable) this. dataGridView1.DataSource; string sqlConnectionString = "Data Source = WangHaitao-PC; Initial Catalog = charge_sys; User ID = sa; PWD = 123456;"; SqlConnection sqlConnection = new SqlConnection (sqlConnectionString ); sqlCommand sqlCommand = new SqlCommand ("select * from testB", sqlConnection); SqlDataAdapter sqlAdap = new SqlDataAdapter (sqlCommand); SqlCommandBuilder sqlBuilder = new SqlCommandBuilder (sqlAdap); // sqlConnection is required. open (); sqlAdap. fill (table); // a primary key must exist in the testB table; otherwise, sqlAdap cannot be updated. update (table); sqlConnection. close (); MessageBox. show ("aa ");}