Advertisement
Guest User

Sql Helper

a guest
Oct 14th, 2015
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.69 KB | None | 0 0
  1.     public interface ISqlCommand
  2.     {
  3.  
  4.         ISqlCommand CommandText(string commandText);
  5.         ISqlCommand Connection(SqlConnection connection);
  6.         ISqlCommand Transaction(SqlTransaction transaction);
  7.         ISqlCommand CommandTimeout(int timeout);
  8.         ISqlCommand CommandType(CommandType commandType);
  9.         ISqlCommand Parameters(Func<ISqlCommandParameter, object> parameter);
  10.         DataSet ToDataSet();
  11.         DataTable ToDataTable();
  12.         DataRow ToDataRow(int rowIndex = 0);
  13.         TResult ToValue<TResult>(int rowIndex = 0, int cellIndex = 0);
  14.         SqlDataReader ExecuteReader(CommandBehavior commandBehavior = CommandBehavior.Default);
  15.         int ExecuteNonQuery();
  16.         object ExecuteScalar();
  17.         TResult ExecuteScalar<TResult>();
  18.  
  19.  
  20.  
  21.  
  22.  
  23.     }
  24.  
  25.     public interface ISqlCommandParameter
  26.     {
  27.         ISqlCommandParameter Add(SqlParameter parameter);
  28.         ISqlCommandParameter Add(string parameterName, object value);
  29.         ISqlCommandParameter Add(string parameterName, SqlDbType sqlDbType);
  30.         ISqlCommandParameter Add(string parameterName, SqlDbType sqlDbType, int size);
  31.         ISqlCommandParameter Add(string parameterName, SqlDbType sqlDbType, int size, string sourceColumn);
  32.         ISqlCommandParameter AddWithValue(string parameterName, object value);
  33.  
  34.     }
  35.  
  36.     public class SqlHelper : ISqlCommand, IDisposable
  37.     {
  38.  
  39.         #region Filds
  40.  
  41.         public static readonly SqlConnection DefaultConnection = new SqlConnection("");
  42.         private readonly SqlCommand _command = null;
  43.         private SqlConnection _connection = null;
  44.         private SqlTransaction _transaction = null;
  45.  
  46.         #endregion
  47.  
  48.         public SqlHelper()
  49.         {
  50.             _connection = DefaultConnection;
  51.             _command = new SqlCommand { Connection = _connection };
  52.         }
  53.  
  54.         #region ISqlCommand
  55.  
  56.  
  57.         public ISqlCommand CommandText(string commandText)
  58.         {
  59.             _command.CommandText = commandText;
  60.             return this;
  61.         }
  62.  
  63.         public ISqlCommand Connection(SqlConnection connection)
  64.         {
  65.             _connection = connection;
  66.             return this;
  67.         }
  68.  
  69.         public ISqlCommand Transaction(SqlTransaction transaction)
  70.         {
  71.  
  72.             _transaction = transaction;
  73.             _command.Transaction = _transaction;
  74.             return this;
  75.         }
  76.  
  77.         public ISqlCommand CommandTimeout(int timeout)
  78.         {
  79.             _command.CommandTimeout = timeout;
  80.             return this;
  81.         }
  82.  
  83.         public ISqlCommand CommandType(CommandType commandType)
  84.         {
  85.             _command.CommandType = commandType;
  86.             return this;
  87.         }
  88.  
  89.         public ISqlCommand Parameters(Func<ISqlCommandParameter, object> parameter)
  90.         {
  91.             SqlCommandParameter param = new SqlCommandParameter(_command);
  92.             parameter.Invoke(param);
  93.             return this;
  94.         }
  95.  
  96.         public DataSet ToDataSet()
  97.         {
  98.             using (SqlDataAdapter adapter = new SqlDataAdapter(_command))
  99.             {
  100.                 DataSet dataSet = new DataSet();
  101.                 adapter.Fill(dataSet);
  102.                 return dataSet;
  103.             }
  104.         }
  105.  
  106.         public DataTable ToDataTable()
  107.         {
  108.             using (SqlDataAdapter adapter = new SqlDataAdapter(_command))
  109.             {
  110.                 DataTable table = new DataTable();
  111.                 adapter.Fill(table);
  112.                 adapter.Dispose();
  113.                 return table;
  114.             }
  115.         }
  116.  
  117.         public DataRow ToDataRow(int rowIndex = 0)
  118.         {
  119.             DataTable table = ToDataTable();
  120.             if (table.Rows.Count == 0) return null;
  121.             return table.Rows[rowIndex];
  122.         }
  123.  
  124.         public TResult ToValue<TResult>(int rowIndex = 0, int cellIndex = 0)
  125.         {
  126.             DataTable table = ToDataTable();
  127.             if (table.Rows.Count == 0) return default(TResult);
  128.  
  129.             object value = table.Rows[rowIndex][cellIndex];
  130.  
  131.  
  132.             if (value is TResult)
  133.             {
  134.                 return (TResult)value;
  135.             }
  136.             throw new InvalidCastException();
  137.         }
  138.  
  139.         public SqlDataReader ExecuteReader(CommandBehavior commandBehavior = CommandBehavior.Default)
  140.         {
  141.             return _command.ExecuteReader(commandBehavior);
  142.         }
  143.  
  144.         public int ExecuteNonQuery()
  145.         {
  146.             return _command.ExecuteNonQuery();
  147.         }
  148.  
  149.         public object ExecuteScalar()
  150.         {
  151.             return _command.ExecuteScalar();
  152.         }
  153.  
  154.         public TResult ExecuteScalar<TResult>()
  155.         {
  156.             object value = _command.ExecuteScalar();
  157.             if (value is TResult)
  158.             {
  159.                 return (TResult)value;
  160.             }
  161.             throw new InvalidCastException();
  162.         }
  163.  
  164.         #endregion
  165.  
  166.         #region IDisposable
  167.  
  168.         public void Dispose()
  169.         {
  170.             if (_transaction != null)
  171.             {
  172.                 _transaction.Dispose();
  173.             }
  174.  
  175.             _command.Dispose();
  176.             if (_connection.State != ConnectionState.Open)
  177.             {
  178.                 _connection.Close();
  179.             }
  180.             _connection.Dispose();
  181.         }
  182.  
  183.         #endregion
  184.  
  185.     }
  186.  
  187.     public class SqlCommandParameter : ISqlCommandParameter
  188.     {
  189.         private readonly SqlCommand _command;
  190.         public SqlCommandParameter(SqlCommand command)
  191.         {
  192.             _command = command;
  193.         }
  194.  
  195.         public ISqlCommandParameter Add(SqlParameter parameter)
  196.         {
  197.             _command.Parameters.Add(parameter);
  198.             return this;
  199.         }
  200.  
  201.         public ISqlCommandParameter Add(string parameterName, object value)
  202.         {
  203.             _command.Parameters.Add(parameterName, value);
  204.             return this;
  205.         }
  206.  
  207.         public ISqlCommandParameter Add(string parameterName, SqlDbType sqlDbType)
  208.         {
  209.             _command.Parameters.Add(parameterName, sqlDbType);
  210.             return this;
  211.         }
  212.  
  213.         public ISqlCommandParameter Add(string parameterName, SqlDbType sqlDbType, int size)
  214.         {
  215.             _command.Parameters.Add(parameterName, sqlDbType, size);
  216.             return this;
  217.         }
  218.  
  219.         public ISqlCommandParameter Add(string parameterName, SqlDbType sqlDbType, int size, string sourceColumn)
  220.         {
  221.             _command.Parameters.Add(parameterName, sqlDbType, size, sourceColumn);
  222.             return this;
  223.         }
  224.  
  225.         public ISqlCommandParameter AddWithValue(string parameterName, object value)
  226.         {
  227.             _command.Parameters.AddWithValue(parameterName, value);
  228.             return this;
  229.         }
  230.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement