ADO.NET ExecuteNonQuery in SqlCommand Object

The ExecuteNonQuery() method in the SqlCommand Object is a commonly used method for executing statements that do not return a result set. It is primarily used for performing data manipulation and data definition tasks within the database.


One of the key functions of ExecuteNonQuery() is to handle data definition tasks such as creating stored procedures and views. By using ExecuteNonQuery(), developers can execute SQL statements that create or modify database objects without returning any data. This method allows for the efficient execution of data definition tasks, enabling the creation and modification of stored procedures, views, and other database objects.

Moreover, ExecuteNonQuery() is also utilized for data manipulation tasks such as inserting, updating, and deleting data within the database. This method allows developers to execute SQL statements that modify the data in the database without retrieving any result set. It is particularly useful when performing operations that affect the data directly, such as adding new records, updating existing records, or deleting records from a table.

By using ExecuteNonQuery() in conjunction with the SqlCommand Object, developers can efficiently perform both data definition and data manipulation tasks within the database. This method provides a streamlined approach for executing SQL statements that do not require a result set, making it a fundamental tool for performing various database operations efficiently.

The following example shows how to use the method ExecuteNonQuery() through SqlCommand Object.

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 cnn As SqlConnection Dim cmd As SqlCommand Dim sql As String connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password" sql = "Your SQL Statement Here" cnn = New SqlConnection(connetionString) Try cnn.Open() cmd = New SqlCommand(Sql, cnn) cmd.ExecuteNonQuery() cmd.Dispose() cnn.Close() MsgBox(" ExecuteNonQuery in SqlCommand executed !!") Catch ex As Exception MsgBox("Can not open connection ! ") End Try End Sub End Class

You have to replace the string with your realtime variables.

connetionString = "Data Source = ServerName; Initial Catalog = DatabaseName; User ID = UserName; Password = Password"