Dataadapter InsertCommand - Sql Server

The SqlDataAdapter plays a crucial role in facilitating communication between the Dataset and the Data Source, utilizing the SqlConnection Object. Specifically, the InsertCommand property within the SqlDataAdapter enables the management of data insertion into the specified Data Source.

The provided source code illustrates the process of inserting data into a Data Source using the SqlDataAdapter and SqlCommand objects. It involves establishing a connection to the Data Source using the SqlConnection object, creating a SqlCommand object with the appropriate insert SQL statement, and assigning the SqlCommand to the SqlDataAdapter's InsertCommand property.

Full Source VB.NET
Imports System.Data.SqlClient Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim connetionString As String Dim connection As SqlConnection Dim adapter As New SqlDataAdapter Dim sql As String connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password" connection = New SqlConnection(connetionString) sql = "insert into product (Product_id,Product_name,Product_price) values(7,'Product7',700)" Try connection.Open() adapter.InsertCommand = New SqlCommand(sql, connection) adapter.InsertCommand.ExecuteNonQuery() MsgBox("Row inserted !! ") Catch ex As Exception MsgBox(ex.ToString) End Try End Sub End Class

In this code snippet, the SqlConnection object establishes a connection to the Data Source using the provided connection string. The SqlDataAdapter is instantiated, and the SqlCommand object is created with the insert SQL statement and the SqlConnection object assigned to it. The parameters for the insert command are defined and set accordingly.

After opening the connection, the ExecuteNonQuery method is called on the InsertCommand of the SqlDataAdapter to execute the insert command against the Data Source. The number of affected rows is then displayed.