internal IDbConnection GetConnection() { IDbConnection connection = ProviderFactory.GetConnection(this.connection, this.provider); connection.Open(); return(connection); }
// Jeff Lanning ([email protected]): Added overload for OPath support public DataSet GetDataSet(Type entityType, CommandInfo commandInfo, CompiledQuery query, object[] parameterValues) { using (IDbConnection conn = ProviderFactory.GetConnection(this.connection, this.provider)) { IDbCommand cmd = CreateDbCommand(Guid.NewGuid(), entityType, commandInfo, conn, query, parameterValues); IDbDataAdapter adapter = ProviderFactory.GetAdapter(cmd, this.provider); DataSet dataSet = new DataSet("WilsonORMapper"); adapter.Fill(dataSet); return(dataSet); } }
public int ExecuteCommand(Type entityType, CommandInfo commandInfo, string sqlStatement, params Parameter[] parameters) { int output = 0; // Success if any Rows are Affected using (IDbConnection connection = ProviderFactory.GetConnection(this.connection, this.provider)) { IDbCommand command = CreateDbCommand(Guid.NewGuid(), entityType, commandInfo, connection, sqlStatement, parameters); connection.Open(); // Connection will be Closed in all Cases output = command.ExecuteNonQuery(); this.SetOutputParameters(command, parameters); } return(output); }
public object GetScalarValue(Type entityType, CommandInfo commandInfo, string sqlStatement, params Parameter[] parameters) { string sqlStatement1 = sqlStatement; string sqlStatement2 = null; int splitIndex = sqlStatement.IndexOf(Connection.SplitToken); if (splitIndex >= 0) { sqlStatement1 = sqlStatement.Substring(0, splitIndex); sqlStatement2 = sqlStatement.Substring(splitIndex + Connection.SplitToken.Length); } object value = null; using (IDbConnection connection = ProviderFactory.GetConnection(this.connection, this.provider)) { IDbCommand command = CreateDbCommand(Guid.NewGuid(), entityType, commandInfo, connection, sqlStatement1, parameters); if (this.provider.UseInsertReturn && sqlStatement.ToUpper().StartsWith("INSERT")) { IDbDataParameter dbParameter = command.CreateParameter(); dbParameter.ParameterName = "KeyField"; dbParameter.DbType = DbType.Decimal; dbParameter.Direction = ParameterDirection.ReturnValue; command.Parameters.Add(dbParameter); } connection.Open(); // Connection will be Closed in all Cases if (sqlStatement2 != null) { bool success = (command.ExecuteNonQuery() > 0); if (!success) { return(null); } command = CreateDbCommand(Guid.NewGuid(), entityType, CommandInfo.GetScalar, connection, sqlStatement2, null); } if (this.provider.UseInsertReturn && sqlStatement.ToUpper().StartsWith("INSERT")) { if (command.ExecuteNonQuery() > 0) { value = ((IDbDataParameter)command.Parameters["KeyField"]).Value; } } else { value = command.ExecuteScalar(); } this.SetOutputParameters(command, parameters); } return(value); }
public IDataReader GetDataReader(Type entityType, CommandInfo commandInfo, out IDbCommand dbCommand, string sqlStatement, params Parameter[] parameters) { IDbConnection connection = null; try { connection = ProviderFactory.GetConnection(this.connection, this.provider); dbCommand = CreateDbCommand(Guid.NewGuid(), entityType, commandInfo, connection, sqlStatement, parameters); connection.Open(); // Must Close DataReader to Close Connection return(dbCommand.ExecuteReader(CommandBehavior.CloseConnection)); } catch { if (connection != null) { connection.Close(); } throw; } }
// Jeff Lanning ([email protected]): Added overload for OPath support public IDataReader GetDataReader(Type entityType, CommandInfo commandInfo, CompiledQuery query, object[] parameterValues) { IDbConnection conn = null; try { conn = ProviderFactory.GetConnection(this.connection, this.provider); IDbCommand cmd = CreateDbCommand(Guid.NewGuid(), entityType, commandInfo, conn, query, parameterValues); cmd.Connection.Open(); return(cmd.ExecuteReader(CommandBehavior.CloseConnection)); } catch { if (conn != null) { conn.Close(); } throw; } }
internal Connection(string connectString, CustomProvider customProvider) { if (connectString == null || connectString.Length == 0) { throw new ORMapperException("Internals: ConnectionString was Empty"); } if (customProvider.Provider == Provider.Access && connectString.ToUpper().IndexOf("PROVIDER") < 0) { this.connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + connectString; } else { this.connection = connectString; } this.provider = customProvider; // Check Timeout Support and Valid Connection String IDbConnection connection = null; try { connection = ProviderFactory.GetConnection(this.connection, this.provider); this.commandTimeout = connection.ConnectionTimeout; try { IDbCommand command = connection.CreateCommand(); command.CommandTimeout = this.commandTimeout; } catch { this.supportsTimeout = false; } connection.Open(); } catch (Exception exception) { throw new ORMapperException("ObjectSpace: Connection String is Invalid - " + exception.Message, exception); } finally { if (connection != null) { connection.Close(); } } }
// Includes support for typed datasets from Ben Priebe (http://stickfly.com) public DataSet GetDataSet(Type entityType, CommandInfo commandInfo, DataSet dataSet, string sqlStatement, params Parameter[] parameters) { if (dataSet == null) { dataSet = new DataSet("WilsonORMapper"); } using (IDbConnection connection = ProviderFactory.GetConnection(this.connection, this.provider)) { IDbCommand command = CreateDbCommand(Guid.NewGuid(), entityType, commandInfo, connection, sqlStatement, parameters); IDbDataAdapter adapter = ProviderFactory.GetAdapter(command, this.provider); if (dataSet.Tables.Count > 0) { string tableName = dataSet.Tables[0].TableName; (adapter as System.Data.Common.DbDataAdapter).Fill(dataSet, tableName); } else { adapter.Fill(dataSet); } this.SetOutputParameters(command, parameters); } return(dataSet); }