C # Use SqlDataAdapter. Update to Update a database

Source: Internet
Author: User

The data obtained from the database is displayed on the DataGridView. Then change the data. The method for changing data is included in the button event.

The data display method is needless to say.

The SqlCommandBuilder class and SqlDataAdapter. Update () method are used for updating.

The SqlCommandBuilder object is used to generate SQL statements used to update databases. You do not have to create these statements yourself.

The UpDate method automatically traverses the rows in the DataTable to find the changes to the database. Each DataRow object in the Rows set has the RowState attribute, which can be used to track whether the row has been deleted, added, modified, or not changed. Any changes made will be reflected in the database.

 

For example, update the table content:

 

Using System. Data. SqlClient;

Namespace UpdatingData
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
BtnUpdate. Click + = new EventHandler (btnUpdate_Click );
UpdateData ();
}

String sConnection = "Data Source = scott; Initial Catalog = northwind; Persist Security Info = True; User ID = sa; Password = sa123 ";
DataSet dsSet = new DataSet ();
SqlDataAdapter sdaAdapter = null;
SqlCommandBuilder scbBuilder = null;
Private void UpdateData ()
{
// Create a Connection
SqlConnection scConnection = new SqlConnection (sConnection );
// CREATE Command
SqlCommand scCommand = scConnection. CreateCommand ();
ScCommand. CommandText = "select customerID, contactName from customers ";
// Create an Adapter
SdaAdapter = new SqlDataAdapter (scCommand );

// This object generates SQL statements used to update the database. You do not have to create these statements yourself.
ScbBuilder = new SqlCommandBuilder (sdaAdapter );

// Obtain data
SdaAdapter. Fill (dsSet, "MERs ");
DgvView. DataSource = dsSet. Tables ["MERs"];

}

Void btnUpdate_Click (object sender, EventArgs e)
{
// Set the value
DsSet. Tables ["MERs"]. Rows [3] ["contactName"] = "Thomas hard ";
// UpDate data (the UpDate method automatically traverses the rows in the DataTable to find out the changes to the database)
// Each DataRow object in the Rows set has the RowState attribute, which can be used to track whether the row has been deleted, added, modified, or not changed. Any changes made will be reflected in the database.
SdaAdapter. Update (dsSet, "MERs ");
DgvView. DataSource = dsSet. Tables ["MERs"];

}
}
}

 

 

Example: Add rows in the table

 

Using System. Data. SqlClient;

Namespace AddingData
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
BtnAdd. Click + = new EventHandler (btnAdd_Click );

UpdateData ();
}

String sConnection = "Data Source = scott; Initial Catalog = northwind; Persist Security Info = True; User ID = sa; Password = sa123 ";
DataSet dsSet = new DataSet ();
SqlDataAdapter sdaAdapter = null;
SqlCommandBuilder scbBuilder = null;
Private void UpdateData ()
{
// Create a Connection
SqlConnection scConnection = new SqlConnection (sConnection );
// CREATE Command
SqlCommand scCommand = scConnection. CreateCommand ();
ScCommand. CommandText = "select customerID, companyName from customers ";
// Create an Adapter
SdaAdapter = new SqlDataAdapter (scCommand );

// This object generates SQL statements used to update the database. You do not have to create these statements yourself.
ScbBuilder = new SqlCommandBuilder (sdaAdapter );

// Obtain data
SdaAdapter. Fill (dsSet, "MERs ");
DgvView. DataSource = dsSet. Tables ["MERs"];
}

Void btnAdd_Click (object sender, EventArgs e)
{
AddRow ();
}
Private void AddRow ()
{
// Create a row in the table
DataRow drRow = dsSet. Tables ["MERs"]. NewRow ();
DrRow ["customerID"] = "ZaCzi ";
DrRow ["companyName"] = "Zachary Zithers Ltd .";
// Add rows
DsSet. Tables ["MERs"]. Rows. Add (drRow );

// Update the table
SdaAdapter. Update (dsSet, "MERs ");

// Display
DgvView. DataSource = dsSet. Tables ["MERs"];
}
}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.