Beispiel #1
0
 public DataTable GetDataTable(DatabaseCommand command)
 {
     if (Connect())
     {
         using (DbCommand dbCommand = CreateCommand(command))
         {
             using (DbDataAdapter dataAdapter = CreateDataAdapter())
             {
                 using (DataSet dataSet = new DataSet())
                 {
                     if (dataAdapter == null)
                     {
                         return(dataSet.Tables[0]);
                     }
                     dataAdapter.SelectCommand = dbCommand;
                     dataAdapter.Fill(dataSet);
                     return(dataSet.Tables[0]);
                 }
             }
         }
     }
     else
     {
         throw new Exception(DatabaseConnectionError);
     }
 }
Beispiel #2
0
 public int ExecuteNonQuery(DatabaseCommand command)
 {
     if (Connect())
     {
         using (DbCommand dbCommand = CreateCommand(command))
         {
             return(dbCommand.ExecuteNonQuery());
         }
     }
     else
     {
         throw new Exception(DatabaseConnectionError);
     }
 }
Beispiel #3
0
 public object ExecuteScalar(DatabaseCommand command)
 {
     if (Connect())
     {
         using (DbCommand dbCommand = CreateCommand(command))
         {
             return(dbCommand.ExecuteScalar());
         }
     }
     else
     {
         throw new Exception(DatabaseConnectionError);
     }
 }
Beispiel #4
0
 public IDataReader GetDataReader(DatabaseCommand command)
 {
     if (Connect())
     {
         using (DbCommand dbCommand = CreateCommand(command))
         {
             return(dbCommand.ExecuteReader());
         }
     }
     else
     {
         throw new Exception(DatabaseConnectionError);
     }
 }
Beispiel #5
0
        private DbCommand CreateCommand(DatabaseCommand command)
        {
            DbCommand dbCommand = _DbConnection.CreateCommand();

            dbCommand.CommandText = command.Query;
            dbCommand.CommandType = command.IsStoredProcedure ? CommandType.StoredProcedure : CommandType.Text;
            foreach (KeyValuePair <string, object> kvp in command.Parameters)
            {
                IDataParameter dbParameter = dbCommand.CreateParameter();
                dbParameter.ParameterName = kvp.Key;
                dbParameter.Value         = kvp.Value ?? DBNull.Value;
                dbCommand.Parameters.Add(dbParameter);
            }
            return(dbCommand);
        }
Beispiel #6
0
 public IEnumerable <TEntity> ExecuteReader <TEntity>(DatabaseCommand command, Func <IDataRecord, TEntity> selector)
 {
     if (Connect())
     {
         using (DbCommand dbCommand = CreateCommand(command))
         {
             using (IDataReader dataReader = dbCommand.ExecuteReader())
             {
                 while (dataReader.Read())
                 {
                     yield return(selector(dataReader));
                 }
             }
         }
     }
     else
     {
         throw new Exception(DatabaseConnectionError);
     }
 }
Beispiel #7
0
 public IEnumerable <IDataRecord> ExecuteReader(DatabaseCommand command)
 {
     if (Connect())
     {
         using (DbCommand dbCommand = CreateCommand(command))
         {
             using (IDataReader dataReader = dbCommand.ExecuteReader())
             {
                 while (dataReader.Read())
                 {
                     yield return(dataReader);
                 }
             }
         }
     }
     else
     {
         throw new Exception(DatabaseConnectionError);
     }
 }