ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

DbConnection.ExecuteNonQuery

Execute a SQL command directly on a DbConnection. Needless to say that the other ExecuteXXX methods can be implemented as well. Implementing the method at DbConnection level makes it available for SQLConnection, OleDbConnection, ...

Source

public static class DbConnectionExtensions
{
    public static int ExecuteNonQuery(this DbConnection conn, string sql)
    {
        DbCommand cmd = conn.CreateCommand();
        cmd.CommandText = sql;

        return cmd.ExecuteNonQuery();
    }
}

Example

DbConnection _db= new OleDbConnection(connectionString);
_db.Open();
int affected = _db.ExecuteNonQuery("delete from [Users]");

Author: Gaston Verelst

Submitted on: 7 feb 2011

Language: C#

Type: System.Data.Common.DbConnection

Views: 6596