How to DataAdapter Delete Command - SQL SERVER

SqlDataAdapter is an integral component of the ADO.NET Data Provider, serving as a vital tool for data management. The DataAdapter utilizes the power of the Connection object from the .NET Framework data provider to establish a connection with a designated data source. Furthermore, it uses Command objects to efficiently retrieve data from the data source and resolve any changes or modifications.

DeleteCommand properties

The InsertCommand, UpdateCommand, and DeleteCommand properties of the DataAdapter are essential Command objects that orchestrate updates to the data in the data source. These properties ensure that any modifications made to the data in the DataSet are accurately reflected in the data source itself.

To illustrate the usage of the SqlDataAdapter Object for data deletion in a SQL Server database, the following C# code samples demonstrate the implementation of the DeleteCommand properties within the 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()); } } } }