/// <summary> /// Runs the command. /// </summary> /// <param name="connection">The connection.</param> /// <param name="trans">The trans.</param> /// <param name="sqlCommand">The SQL command.</param> /// <returns>The number of rows that were processed.</returns> private int RunCommand(DbConnection connection, DbTransaction trans, QueryInformation sqlCommand) { using (DbCommand command = this.CreateCommand(connection, trans, sqlCommand)) { return(command.ExecuteNonQuery()); } }
/// <summary> /// Runs the query. /// </summary> /// <param name="connection">The connection.</param> /// <param name="trans">The trans.</param> /// <param name="sqlCommand">The SQL command.</param> /// <param name="behavior">The behavior.</param> /// <returns>A data reader with the results.</returns> private DbDataReader RunQuery(DbConnection connection, DbTransaction trans, QueryInformation sqlCommand, CommandBehavior behavior) { using (DbCommand command = this.CreateCommand(connection, trans, sqlCommand)) { return(command.ExecuteReader(behavior)); } }
/// <summary> /// Given an open transaction, this opens the corresponding connection if necessary, executes the SQL command /// and returns a DataReader. /// </summary> /// <param name="sqlCommand">The query information to execute the query.</param> /// <param name="transaction">An open SQL transaction.</param> /// <returns> /// A DataReader, the result of executing the sqlCommand. /// </returns> public DbDataReader ExecuteQuery(QueryInformation sqlCommand, DbTransaction transaction) { if (transaction == null) { throw new ArgumentNullException("transaction"); } return(this.RunQuery(transaction.Connection, transaction, sqlCommand, CommandBehavior.Default)); }
/// <summary> /// Opens a connection to the database, executes the SQL command, and returns a DataReader. /// </summary> /// <param name="sqlCommand">The query information to execute the query.</param> /// <returns> /// A DataReader, the result of executing the sqlCommand. /// </returns> /// <remarks> /// The database connection's execution behavior is set to close connection. Once the returned DataReader /// is closed, the connection to the database will close. /// </remarks> public DbDataReader ExecuteQuery(QueryInformation sqlCommand) { DbDataReader reader; DbConnection connection; connection = this.GetConnection(); reader = this.RunQuery(connection, null, sqlCommand, CommandBehavior.CloseConnection); return(reader); }
/// <summary> /// Given an open transaction, this opens the corresponding connection if necessary, executes the SQL command /// and returns a DataReader. /// </summary> /// <param name="sqlCommand">The query information to execute the query.</param> /// <returns> /// A DataReader, the result of executing the sqlCommand. /// </returns> public int ExecuteNonQuery(QueryInformation sqlCommand) { int numRows; using (DbConnection connection = this.GetConnection()) { numRows = this.RunCommand(connection, null, sqlCommand); connection.Close(); return(numRows); } }
/// <summary> /// Copies the parameters. /// </summary> /// <param name="command">The command.</param> /// <param name="sqlCommand">The SQL command.</param> private static void CopyParameters(DbCommand command, QueryInformation sqlCommand) { DbParameter parameter; if (!sqlCommand.HasParameters) { return; } for (int i = 0; i < sqlCommand.Parameters.Count; i++) { parameter = command.CreateParameter(); sqlCommand.Parameters[i].CopyTo(parameter); command.Parameters.Add(parameter); } }
/// <summary> /// Creates the command. /// </summary> /// <param name="connection">The connection.</param> /// <param name="trans">The trans.</param> /// <param name="sqlCommand">The SQL command.</param> /// <returns>A new command object.</returns> private DbCommand CreateCommand(DbConnection connection, DbTransaction trans, QueryInformation sqlCommand) { DbCommand command; if (connection == null) { throw new ArgumentNullException("connection"); } if (string.IsNullOrEmpty(sqlCommand.CommandText)) { throw new ArgumentException("empty", "sqlCommand"); } command = connection.CreateCommand(); command.CommandText = sqlCommand.CommandText; command.CommandType = sqlCommand.CommandType; if (trans != null) { command.Transaction = trans; } DatabaseCommandExecution.CopyParameters(command, sqlCommand); return(command); }
/// <summary> /// Executes the SQL command as a "Non-Query" using the provided Transaction, and returns the number of rows. /// </summary> /// <param name="sqlCommand">The query information to execute the query.</param> /// <param name="transaction">An open SQL transaction.</param> /// <returns>A DataReader, the result of executing the sqlCommand.</returns> public int ExecuteNonQuery(QueryInformation sqlCommand, DbTransaction transaction) { return(this.RunCommand(transaction.Connection, transaction, sqlCommand)); }
/// <summary> /// Executes the SQL command as a "Non-Query" using the provided Connection, and returns the number of rows /// and returns a DataReader. /// </summary> /// <param name="sqlCommand">The query information to execute the query.</param> /// <param name="connection">A connection to a database.</param> /// <returns> /// A DataReader, the result of executing the sqlCommand. /// </returns> public int ExecuteNonQuery(QueryInformation sqlCommand, DbConnection connection) { return(this.RunCommand(connection, null, sqlCommand)); }
/// <summary> /// Given a connection to a database, this opens the connection if necessary, executes the SQL command and /// returns a DataReader. /// </summary> /// <param name="sqlCommand">The query information to execute the query.</param> /// <param name="connection">A connection to a database.</param> /// <returns> /// A DataReader, the result of executing the sqlCommand. /// </returns> public DbDataReader ExecuteQuery(QueryInformation sqlCommand, DbConnection connection) { return(this.RunQuery(connection, null, sqlCommand, CommandBehavior.Default)); }