How to DataAdapter Delete Command - SQL SERVER

SqlDataAdapter is a part of the ADO.NET Data Provider. The DataAdapter 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.

InsertCommand , UpdateCommand , and DeleteCommand properties of the DataAdapter are Command objects that manage updates to the data in the data source according to modifications made to the data in the DataSet. The following C# code samples demonstrate how to use the SqlDataAdapter Object to Delete data from SQL Server database using DeleteCommand properties in SqlDataAdapter.

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 = "delete product where Product_name ='Product6'"; try { connection.Open(); adapter.DeleteCommand = connection.CreateCommand(); adapter.DeleteCommand.CommandText = sql; adapter.DeleteCommand.ExecuteNonQuery(); MessageBox.Show ("Row(s) deleted !! "); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } }