Joel.Net.DatabaseUtility.dll Class Library

DatabaseUtility.ExecuteNonQuery Method (String, CommandType, SqlParameter[])

Executes a Transact-SQL statement against the connection and returns the number of rows affected.

[Visual Basic]
Shared  OverloadsPublic Function ExecuteNonQuery( _ 
   ByVal commandText As String, _ 
   ByVal commandType As CommandType, _ 
   ByVal ParamArray parameters As SqlParameter() _ 
) As Integer
[C#]
public static int ExecuteNonQuery(
   string commandText,
   CommandType commandType,
   params SqlParameter[] parameters
);
[C++]
public: static int ExecuteNonQuery(
   String* commandText,
   CommandType commandType,
   SqlParameter* parameters __gc[]
);
[JScript]
public static function ExecuteNonQuery(
   String commandText,
   CommandType commandType,
   SqlParameter[] parameters
): int;

Parameters

commandText
The text of the query.
commandType
Specifies how a command string is interpreted.
parameters
A list of type System.Data.SqlClient.SqlParameter that maps to the System.Data.SqlClient.SqlCommand.

Return Value

The number of rows affected.

Example

[C#, Visual Basic] The following example creates a SqlCommand and then executes it using ExecuteNonQuery. The example is passed a string that is a Transact-SQL statement (such as UPDATE, INSERT, or DELETE) and a string to use to connect to the data source.

[C#]
DatabaseUtility.Connection = new SqlConnection("Server=127.0.0.1;Database=Northwind;Uid=sa;Pwd=;");

DatabaseUtility.ExecuteNonQuery(
    "INSERT INTO Categories (CategoryName) VALUES (@CategoryName)",
    CommandType.Text,
    new SqlParameter("@CategoryName", "New Category")
);
[Visual Basic]
DatabaseUtility.Connection = New SqlConnection("Server=127.0.0.1;Database=Northwind;Uid=sa;Pwd=;")

DatabaseUtility.ExecuteNonQuery( _
    "INSERT INTO Categories (CategoryName) VALUES (@CategoryName)", _
    CommandType.Text, _
    new SqlParameter("@CategoryName", "New Category") _
)

See Also

DatabaseUtility Class | Joel.Net Namespace | DatabaseUtility.ExecuteNonQuery Overload List