Guest User

Untitled

a guest
Jun 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. public string GetCommandLogString(IDbCommand command)
  2. {
  3. string outputText;
  4. if(command.Parameters.Count == 0)
  5. {
  6. outputText = command.CommandText;
  7. }
  8. else
  9. {
  10. StringBuilder output = new StringBuilder();
  11. output.Append(command.CommandText);
  12. output.Append("; ");
  13.  
  14. IDataParameter objIDataParameter;
  15. int parameterCount = command.Parameters.Count;
  16. for(int i = 0; i < parameterCount; i++)
  17. {
  18. objIDataParameter = (IDataParameter)command.Parameters[i];
  19. output.Append(string.Format("{0} = '{1}'", objIDataParameter.ParameterName, objIDataParameter.Value));
  20.  
  21. if(i + 1 < parameterCount)
  22. {
  23. output.Append(", ");
  24. }
  25. }
  26. outputText = output.ToString();
  27. }
  28. return outputText;
  29. }
  30.  
  31. using (var conn = new WrappedDbConnection(GetMyConnection()))
  32. {
  33. // Do work using Dapper here against "conn"
  34. }
  35.  
  36. public class WrappedDbConnection : IDbConnection
  37. {
  38. private readonly IDbConnection _conn;
  39. public WrappedDbConnection(IDbConnection connection)
  40. {
  41. if (connection == null)
  42. throw new ArgumentNullException(nameof(connection));
  43.  
  44. _conn = connection;
  45. }
  46.  
  47. public string ConnectionString
  48. {
  49. get { return _conn.ConnectionString; }
  50. set { _conn.ConnectionString = value; }
  51. }
  52.  
  53. public int ConnectionTimeout
  54. {
  55. get { return _conn.ConnectionTimeout; }
  56. }
  57.  
  58. public string Database
  59. {
  60. get { return _conn.Database; }
  61. }
  62.  
  63. public ConnectionState State
  64. {
  65. get { return _conn.State; }
  66. }
  67.  
  68. public IDbTransaction BeginTransaction()
  69. {
  70. return _conn.BeginTransaction();
  71. }
  72.  
  73. public IDbTransaction BeginTransaction(IsolationLevel il)
  74. {
  75. return _conn.BeginTransaction(il);
  76. }
  77.  
  78. public void ChangeDatabase(string databaseName)
  79. {
  80. _conn.ChangeDatabase(databaseName);
  81. }
  82.  
  83. public void Close()
  84. {
  85. _conn.Close();
  86. }
  87.  
  88. public IDbCommand CreateCommand()
  89. {
  90. return new WrappedDbCommand(_conn.CreateCommand());
  91. }
  92.  
  93. public void Dispose()
  94. {
  95. _conn.Dispose();
  96. }
  97.  
  98. public void Open()
  99. {
  100. _conn.Open();
  101. }
  102. }
  103.  
  104. public class WrappedDbCommand : IDbCommand
  105. {
  106. private readonly IDbCommand _cmd;
  107. public WrappedDbCommand(IDbCommand command)
  108. {
  109. if (command == null)
  110. throw new ArgumentNullException(nameof(command));
  111.  
  112. _cmd = command;
  113. }
  114.  
  115. public string CommandText
  116. {
  117. get { return _cmd.CommandText; }
  118. set { _cmd.CommandText = value; }
  119. }
  120.  
  121. public int CommandTimeout
  122. {
  123. get { return _cmd.CommandTimeout; }
  124. set { _cmd.CommandTimeout = value; }
  125. }
  126.  
  127. public CommandType CommandType
  128. {
  129. get { return _cmd.CommandType; }
  130. set { _cmd.CommandType = value; }
  131. }
  132.  
  133. public IDbConnection Connection
  134. {
  135. get { return _cmd.Connection; }
  136. set { _cmd.Connection = value; }
  137. }
  138.  
  139. public IDataParameterCollection Parameters
  140. {
  141. get { return _cmd.Parameters; }
  142. }
  143.  
  144. public IDbTransaction Transaction
  145. {
  146. get { return _cmd.Transaction; }
  147. set { _cmd.Transaction = value; }
  148. }
  149.  
  150. public UpdateRowSource UpdatedRowSource
  151. {
  152. get { return _cmd.UpdatedRowSource; }
  153. set { _cmd.UpdatedRowSource = value; }
  154. }
  155.  
  156. public void Cancel()
  157. {
  158. _cmd.Cancel();
  159. }
  160.  
  161. public IDbDataParameter CreateParameter()
  162. {
  163. return _cmd.CreateParameter();
  164. }
  165.  
  166. public void Dispose()
  167. {
  168. _cmd.Dispose();
  169. }
  170.  
  171. public int ExecuteNonQuery()
  172. {
  173. Console.WriteLine($"[ExecuteNonQuery] {_cmd.CommandText}");
  174. return _cmd.ExecuteNonQuery();
  175. }
  176.  
  177. public IDataReader ExecuteReader()
  178. {
  179. Console.WriteLine($"[ExecuteReader] {_cmd.CommandText}");
  180. return _cmd.ExecuteReader();
  181. }
  182.  
  183. public IDataReader ExecuteReader(CommandBehavior behavior)
  184. {
  185. Console.WriteLine($"[ExecuteReader({behavior})] {_cmd.CommandText}");
  186. return _cmd.ExecuteReader();
  187. }
  188.  
  189. public object ExecuteScalar()
  190. {
  191. Console.WriteLine($"[ExecuteScalar] {_cmd.CommandText}");
  192. return _cmd.ExecuteScalar();
  193. }
  194.  
  195. public void Prepare()
  196. {
  197. _cmd.Prepare();
  198. }
  199. }
Add Comment
Please, Sign In to add comment