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"];
}
}
}