Joel.Net.DatabaseUtility.dll Class Library

DatabaseUtility.ExecuteNonQuery Method (SqlConnection, String, CommandType)

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

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

Parameters

connection
Represents an open connection to a SQL Server database.
commandText
The text of the query.
commandType
Specifies how a command string is interpreted.

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#]
SqlConnection connection = new SqlConnection("Server=127.0.0.1;Database=Northwind;Uid=sa;Pwd=;");

DatabaseUtility.ExecuteNonQuery(connection, "INSERT INTO Categories (CategoryName) VALUES ('New Category')", CommandType.Text);
[Visual Basic]
Dim connection As New SqlConnection("Server=127.0.0.1;Database=Northwind;Uid=sa;Pwd=;")

DatabaseUtility.ExecuteNonQuery(connection, "INSERT INTO Categories (CategoryName) VALUES ('New Category')", CommandType.Text)

See Also

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