Created
May 23, 2019 21:41
-
-
Save MiloszKrajewski/c171d334151cfd5e17d89a439d2c0995 to your computer and use it in GitHub Desktop.
Dapperito
This file contains hidden or 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
| /// <summary> | |
| /// Helper class replacing Dapper. | |
| /// </summary> | |
| internal static class Dapperito | |
| { | |
| public static DbCommand CreateCommand(this DbConnection connection, string sql) | |
| { | |
| var command = connection.CreateCommand(); | |
| command.CommandText = sql; | |
| return command; | |
| } | |
| public static DbCommand CreateCommand(this DbTransaction transaction, string sql) | |
| { | |
| var connection = transaction.Connection; | |
| var command = connection.CreateCommand(); | |
| command.CommandText = sql; | |
| command.Transaction = transaction; | |
| return command; | |
| } | |
| public static DbCommand AddParameter( | |
| this DbCommand command, string name, DbType type, object value) | |
| { | |
| var paramerer = command.CreateParameter(); | |
| paramerer.ParameterName = name; | |
| paramerer.DbType = type; | |
| paramerer.Value = value; | |
| command.Parameters.Add(paramerer); | |
| return command; | |
| } | |
| public static int IndexOf(this DbDataReader reader, string fieldName) | |
| { | |
| const StringComparison options = StringComparison.OrdinalIgnoreCase; | |
| for (var i = 0; i < reader.FieldCount; i++) | |
| { | |
| if (string.Compare(reader.GetName(i), fieldName, options) == 0) | |
| return i; | |
| } | |
| throw new ArgumentException($"Field '{fieldName}' could not be found"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment