How to DataAdapter Update Command - Sql Server

SqlDataAdapter is an integral component of the ADO.NET Data Provider. It uses the Connection object from the .NET Framework data provider to establish a connection with a data source, while utilizing Command objects to retrieve data from and handle modifications to the data source.

SqlDataAdapter Object

The InsertCommand, UpdateCommand, and DeleteCommand properties of the SqlDataAdapter Object enable seamless updating of the database with the data modifications performed on a DataSet object. The subsequent C# code snippets exemplify the effective utilization of the SqlDataAdapter object to update a SQL Server database using the properties provided by the UpdateCommand.

Full Source C#
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()); } } } }