Created
July 16, 2015 15:19
-
-
Save gbachs/130ad65d2c402bebf373 to your computer and use it in GitHub Desktop.
Extension methods for IDbConnection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class IDbConnectionExtensions | |
{ | |
public static IDbCommand CreateTextCommand(this IDbConnection thisObj, string sql) | |
{ | |
return CreateCommand(thisObj, sql, CommandType.Text); | |
} | |
public static IDbCommand CreateStoredProcedureCommand(this IDbConnection thisObj, string storedProcedure) | |
{ | |
return CreateCommand(thisObj, storedProcedure, CommandType.StoredProcedure); | |
} | |
internal static IDbCommand CreateCommand(this IDbConnection thisObj, string text, CommandType cmdType) | |
{ | |
IDbCommand cmd = null; | |
try | |
{ | |
cmd = thisObj.CreateCommand(); | |
cmd.Connection = thisObj; | |
cmd.CommandText = text; | |
cmd.CommandType = cmdType; | |
return cmd; | |
} | |
catch (Exception) | |
{ | |
cmd.TryDispose(); | |
throw; | |
} | |
} | |
public static void ExecuteInReader(this IDbConnection thisObj, string sql, Action<IDataRecord> action) | |
{ | |
using (var cmd = thisObj.CreateTextCommand(sql)) | |
{ | |
using (var reader = cmd.ExecuteReader()) | |
{ | |
foreach (var dataRecord in reader.AsEnumerable()) | |
{ | |
action(dataRecord); | |
} | |
} | |
} | |
} | |
public static void ExecuteInReader(this IDbConnection thisObj, string sql, Action<IDbCommand> createCommand, Action<IDataRecord> action) | |
{ | |
using (var cmd = thisObj.CreateTextCommand(sql)) | |
{ | |
createCommand(cmd); | |
using (var reader = cmd.ExecuteReader()) | |
{ | |
foreach (var dataRecord in reader.AsEnumerable()) | |
{ | |
action(dataRecord); | |
} | |
} | |
} | |
} | |
public static T ExecuteScalar<T>(this IDbConnection thisObj, string sql, Action<IDbCommand> action) | |
{ | |
using (var cmd = thisObj.CreateTextCommand(sql)) | |
{ | |
action(cmd); | |
return cmd.ExecuteScalar<T>(); | |
} | |
} | |
public static T ExecuteScalar<T>(this IDbConnection thisObj, string sql) | |
{ | |
return thisObj.ExecuteScalar<T>(sql, cmd => { }); | |
} | |
public static T ExecuteScalar<T>(this IDbConnection thisObj, Action<IDbCommand> action) | |
{ | |
return thisObj.ExecuteScalar<T>(string.Empty, action); | |
} | |
public static int ExecuteNonQuery(this IDbConnection thisObj, string sql, Action<IDbCommand> action) | |
{ | |
using (var cmd = thisObj.CreateTextCommand(sql)) | |
{ | |
action(cmd); | |
return cmd.ExecuteNonQuery(); | |
} | |
} | |
public static int ExecuteNonQuery(this IDbConnection thisObj, Action<IDbCommand> action) | |
{ | |
return thisObj.ExecuteNonQuery(string.Empty, action); | |
} | |
public static int ExecuteNonQuery(this IDbConnection thisObj, string sql) | |
{ | |
using (var cmd = thisObj.CreateTextCommand(sql)) | |
{ | |
return cmd.ExecuteNonQuery(); | |
} | |
} | |
public static int ExecuteNonQuery(this IDbConnection thisObj, string sql, int retryCount) | |
{ | |
return ExecuteNonQuery(thisObj, sql, retryCount, 3000); | |
} | |
public static int ExecuteNonQuery(this IDbConnection thisObj, string sql, int retryCount, int delayInMilliseconds) | |
{ | |
using (var cmd = thisObj.CreateTextCommand(sql)) | |
{ | |
return cmd.ExecuteNonQuery(retryCount, delayInMilliseconds); | |
} | |
} | |
public static IDataReader ExecuteReader(this IDbConnection thisObj, string sql) | |
{ | |
using (var cmd = thisObj.CreateTextCommand(sql)) | |
{ | |
return cmd.ExecuteReader(); | |
} | |
} | |
public static void WithinTransaction(this IDbConnection thisObj, Action<IDbTransaction> action) | |
{ | |
using (var transaction = thisObj.BeginTransaction()) | |
{ | |
action(transaction); | |
transaction.Commit(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment