/// <summary> /// Executes a command on the data store /// </summary> /// <param name="command">The command to execute</param> /// <param name="connection">The connection to use</param> public virtual int ExecuteCommand(IDbCommand command, IDataConnection connection) { using (IDbConnection conn = connection.GetConnection()) { conn.Open(); command.Connection = conn; try { FireEvent(command, connection); return command.ExecuteNonQuery(); } catch (Exception e) { //throw new QueryException(e, command); throw e; } } }
private async Task <SqlCommand> CreateCommand() { var connection = await _dataConnection.GetConnection(); var command = connection.CreateCommand(); return(command); }
public virtual IDbConnection OpenConnection(IDataConnection connection) { IDbConnection conn = connection.GetConnection(); if (conn.State != ConnectionState.Open) { conn.Open(); } return(conn); }
protected virtual T ExecuteCommand <T>(IDbCommand command, IDataConnection connection, Func <T> action) { T toReturn = default(T); foreach (IDbDataParameter parm in command.Parameters) { connection.DatastoreConverter.CoerceValue(parm); } using (IDbConnection conn = connection.GetConnection()) { if (conn.State != ConnectionState.Open) { conn.Open(); } command.Connection = conn; try { FireExecutingEvent(command, connection, conn); toReturn = action(); FireExecutedEvent(command, connection, conn); } catch (Exception e) { throw new QueryException(e, command); } finally { command.Dispose(); conn.Close(); } } return(toReturn); }
public virtual DataSet ExecuteQuery(IDbCommand command, IDataConnection connection) { DataSet _datasetForStore = new DataSet(); IDbConnection conn = connection.GetConnection(); if (conn.State != ConnectionState.Open) conn.Open(); if (connection.IsTransactional) command.Transaction = connection.GetTransaction; command.Connection = conn; command.CommandTimeout = 10000; IDataAdapter adapter = connection.GetDataAdapter(command); try { FireEvent(command, connection); adapter.Fill(_datasetForStore); } catch (Exception e) { //throw new QueryException(e, command); throw e; } finally { if (!connection.IsTransactional) conn.Close(); } return _datasetForStore; }
/// <summary> /// Executes a command and returns the result /// </summary> /// <param name="command">The command to execute</param> /// <param name="connection">The connection to use</param> /// <returns></returns> public virtual QueryData ExecuteCommandQuery(IDbCommand command, IDataConnection connection) { QueryData toReturn = new QueryData(); IDbConnection conn = connection.GetConnection(); if (conn.State != ConnectionState.Open) conn.Open(); if (connection.IsTransactional) command.Transaction = connection.GetTransaction; command.Connection = conn; command.CommandTimeout = 10000; IDataReader reader; try { FireEvent(command, connection); using (reader = command.ExecuteReader()) { if (!_mappings.ContainsKey(command.CommandText)) { _mappings[command.CommandText] = new Dictionary<string, int>(); DataTable schema = reader.GetSchemaTable(); if (schema != null) { for (int i = 0; i < schema.Rows.Count; i++) { _mappings[command.CommandText].Add(schema.Rows[i]["ColumnName"].ToString().ToUpper(), i); } } } toReturn.SetFieldMappings(_mappings[command.CommandText]); while (reader.Read()) { object[] values = new object[reader.FieldCount]; reader.GetValues(values); toReturn.AddRowData(values); } } } catch (Exception e) { //throw new QueryException(e, command); throw e; } finally { if (!connection.IsTransactional) conn.Close(); } toReturn.QuerySuccessful = true; return toReturn; }
/// <summary> /// Initializes a new instance of the <see cref="SqlServerConnection"/> class. /// </summary> /// <param name="ConnectionString">The connection string.</param> public MsSqlServerConnection(string ConnectionString, bool isTransactional = false, IDataConnection con = null) { _commandGenerator = new MsSqlServerCommandGenerator(); _connectionString = ConnectionString; _tConverter = new TSqlTypeConverter(); _Istransactional = isTransactional; _connection = new SqlConnection(_connectionString); if (con != null) { _Istransactional = con.IsTransactional; _connection = con.GetConnection() as SqlConnection; if (con.IsTransactional) { _transaction = con.GetTransaction; } } }