SqlDataAdapter is a part of the ADO.NET Data Provider. The SqlDataAdapter uses the Connection object of the .NET Framework data provider to connect to a data source and Command objects to retrieve data from and resolve changes to the data source. The InsertCommand , the UpdateCommand , and the DeleteCommand properties of the SqlDataAdapter Object update the database with the data modifications, that are run on a DataSet object.
The following C# code samples demonstrate how to use the SqlDataAdapter object to update a SQL Server database using UpdateCommand properties .
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string connetionString = null;
SqlConnection connection ;
SqlDataAdapter adapter = new SqlDataAdapter();
string sql = null;
connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
connection = new SqlConnection(connetionString);
sql = "update product set product_price = 1001 where Product_name ='Product6'";
try
{
connection.Open();
adapter.UpdateCommand = connection.CreateCommand();
adapter.UpdateCommand.CommandText = sql;
adapter.UpdateCommand.ExecuteNonQuery();
MessageBox.Show ("Row updated !! ");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}