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


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, ...


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

        return cmd.ExecuteNonQuery();


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

Author: Gaston Verelst

Submitted on: 7 feb. 2011

Language: C#

Type: System.Data.Common.DbConnection

Views: 6681