I. Overview
-
To update the data in the database using the DataSet class in C# language, you also need to use the SqlCommandBuilder class in the System.Data.SqlClient namespace to automatically generate the add, modify and delete methods of the SqlDataAdapter object
-
When used in conjunction with the SqlDataAdapter class, you only need to use the SqlDataAdapter object as a parameter when creating an instance of the SqlCommandBuilder class. The syntax is as follows:
SqlCommandBuilder object name = new SqlCommandBuilder (object of SqlDataAdapter class);
<!--more-->
Two examples
The following examples demonstrate how to use DataSet to update the database
2.1 Example 1: Use DataSet to realize user registration function
2.1.1 Interface layout
2.1.2 Code
//Register button click event private void button1_Click(object sender, EventArgs e) { //Database connection string string connStr = "Data Source=.;Initial Catalog=test;User ID=sa;Password=root"; //Create an instance of SQLConnection SqlConnection conn = null; try { conn = new SqlConnection(connStr); //Open the database connection conn.Open(); string sql = "select * from userinfo"; //Create an object of the SqlDataAdapter class SqlDataAdapter sda = new SqlDataAdapter(sql, conn); //Create an object of the DataSet class DataSet ds = new DataSet(); //Use the SQLDataAdapter object sda to fill the query result into the DataTable object ds sda.Fill(ds); //Create an object of the SqlCommandBuilder class SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(sda); //Create an object of DataRow class DataRow dr = ds.Tables[0].NewRow(); //Set the value of the name column dr["name"] = textBox1.Text; //Set the value of the password column dr["password"] = textBox2.Text; //Add a row to the DataTable object ds.Tables[0].Rows.Add(dr); //Update the database sda.Update(ds); MessageBox.Show("Registered successfully!"); } catch(Exception ex) { MessageBox.Show("Registration failed!"+ex.Message); } finally { if (conn != null) { //Close the database connection conn.Close(); } } }
2.1.3 Effect picture
2.1.4 Description
Run the form, enter the user name and password, and click the "register" button to add user information to the data table
Replacing the DataSet class in the above code with the DataTable class can also accomplish the same function and simplify the code
Use the DataTable class to complete the above functions, and replace the DataSet with the part of the DataTable code as follows
//Create an object of DataTable class DataTable dt=new DataTable(); //Use the SqlDataAdapter object sda to fill the query results into the DataTable object dt sda.Fill(dt); //Create an object of SqlCommandBuilder class SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(sda); //Create an object of DataRow class DataRow dr=dt.NewRow(); //Set the value of the name column dr["name"] = txtName.Text; //Set the value of the password column dr["password"] = txtPwd.Text; //Add a row to the DataTable object dt.Rows.Add(dr); //Update the database sda.Update(dt)
2.2 Example 2 Use DataSet to realize the function of changing user password
2.2.1 Interface layout
2.2.2 Function code
//Confirm button click event private void button1_Click(object sender, EventArgs e) { //Database connection string string connStr = "Data Source=.;Initial Catalog=test;User ID=sa;Password=root"; //Create an instance of SQLConnection SqlConnection conn = null; try { conn = new SqlConnection(connStr); //Open the database connection conn.Open(); string sql = "select * from userinfo where name='{0}' and password='{1}'"; //Fill in the SQL statement sql = string.Format(sql, textBox1.Text, textBox2.Text); //Create an object of the SqlDataAdapter class SqlDataAdapter sda = new SqlDataAdapter(sql, conn); //Create an object of the DataSet class DataSet ds = new DataSet(); //Use the SQLDataAdapter object sda to fill the query result into the DataTable object ds sda.Fill(ds); if (ds.Tables[0].Rows.Count == 1) { //Judging that the new password cannot be empty, and that the passwords entered twice are consistent if (!"".Equals(textBox3.Text) && textBox3.Text.Equals(textBox4.Text)) { //Create an object of the SqlCommandBuilder class SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(sda); //Create an object of DataRow class DataRow dr = ds.Tables[0].Rows[0]; //Set the value of the password column dr["password"] = textBox3.Text; //Update the database sda.Update(ds); //Update the data in the DataSet object ds.Tables[0].AcceptChanges(); MessageBox.Show("Password changed successfully!"); } else { MessageBox.Show("The new password is empty or the passwords entered on both sides are inconsistent!"); } } } catch(Exception ex) { MessageBox.Show("Password modification failed!"+ex.Message); } finally { if (conn != null) { //Close the database connection conn.Close(); } } }
2.2.3 Effect picture
2.2.4 Description
It can be seen from the above running effect that the password has been successfully modified, and the data in the user information table (userinfo) can also be checked in the database to verify whether the password has been modified
If you need to delete the data in the data table through the DataSet, use the following code.
//Delete the specified row in the DataTable, ds represents the DataSet object ds.Tables[0].Rows[ 行数 ].Delete(); //Update the database, sda represents the SqlDataAdapter object sda.Update(ds); //Update the data in the DataSet object ds.Tables[0] .AcceptChanges();