private T Execute <T>(Func <IDbCommand, T> commandFunc, CommandType commandType = CommandType.Text, bool cloneParameters = true) { T result = default(T); IDbConnection connection = null; try { connection = _dbProviderFactory.CreateConnection(); connection.ConnectionString = ConnectionString; var command = CreateCommand(connection, cloneParameters); command.CommandType = commandType; connection.Open(); TraceCommand(command); result = commandFunc(command); } catch (Exception ex) { Trace.TraceError("Error during connection open. Details: {0}", ex.ToString()); throw; } finally { if (connection != null) { connection.CloseConnection(); connection.Dispose(); } } return(result); }
private T Execute <T>(Func <IDbCommand, T> commandFunc) { T result = default(T); IDbConnection connection = null; IDbCommand command = null; while (true) { try { connection = _dbProviderFactory.CreateConnection(); connection.ConnectionString = ConnectionString; command = CreateCommand(connection); connection.Open(); result = commandFunc(command); break; } finally { if (connection != null) { connection.Dispose(); } } } return(result); }
private T Execute <T>(Func <DbCommand, T> commandFunc) #endif { using (var connection = _dbProviderFactory.CreateConnection()) { connection.ConnectionString = ConnectionString; var command = CreateCommand(connection); connection.Open(); LoggerCommand(command); return(commandFunc(command)); } }
//methods public DataTable GetAllData() { try { if (_dataOrder.ProviderType == @"PIOLEDB") { return(null); } DataTable rawData = new DataTable(); using (IDbConnection connection = _providerFactory.CreateConnection()) { connection.ConnectionString = _dataOrder.ConnectionString; if (FileOpening != null) { FileOpening("Opening File..."); } connection.Open(); if (FileOpened != null) { FileOpened("Opened file successfully"); } IDbCommand command = connection.CreateCommand(); command.CommandText = _dataOrder.DefaultSQL; command.CommandType = CommandType.Text; var reader = command.ExecuteReader(CommandBehavior.CloseConnection); if (FileLoading != null) { FileLoading("Loading file..."); } //load dataset from reader using (reader) { rawData.Load(reader); } if (FileLoaded != null) { FileLoaded("File loaded"); } } //return the data return(rawData); } catch (Exception e) { throw e; } }
public void RemoveRegistration(string connectionString) { lock (Sync) { if (DependencyList.Any()) { using (IDbConnection nc = _dbProviderFactory.CreateConnection()) { nc.ConnectionString = connectionString; nc.Open(); foreach (ISignalRDbDependency dependency in DependencyList) { try { var oracleConnection = nc as OracleConnection; dependency.RemoveRegistration(oracleConnection); } catch (Exception ex) { _traceSource.TraceError( "Error during unregistering of oracle dependency. Details: {0}", ex.Message); } } nc.CloseConnection(); DependencyList.Clear(); } } } }
/// <summary> /// Initializes a new instance of the DbDalc for specified factory and connection string. /// </summary> public DbDalc(IDbProviderFactory factory, string connectionStr) { DbFactory = factory; Connection = factory.CreateConnection(); Connection.ConnectionString = connectionStr; CommandGenerator = new DbCommandGenerator(factory); }
internal static Database OpenConnectionStringInternal( IDbProviderFactory providerFactory, string connectionString ) { return(new Database(() => providerFactory.CreateConnection(connectionString))); }
private static void Main(string[] args) { IDbProviderFactory dbProviderFactory = LoadDbProviderFactory(); PrintHeader("Connection"); var dbConnection = dbProviderFactory.CreateConnection(); dbConnection.Open(); dbConnection.Close(); PrintHeader("Command"); var dbCommand = dbProviderFactory.CreateCommand(); dbCommand.Execute(); Console.ReadKey(); }
public IDbConnection CreateConnectionObject() { var connection = _dbFactory.CreateConnection(); if (connection != null) { try { connection.ConnectionString = ConnectionString; connection.Open(); } catch (Exception) { connection.Dispose(); throw; } } return(connection); }
public void OpenDispose_Basic_Success() { bool[] dbReaderReads = { true, true, false }; var fakeDbReader = A.Fake <IDataReader>(); var fakeDbReaderReadCall = A.CallTo(() => fakeDbReader.Read()); fakeDbReaderReadCall.ReturnsNextFromSequence(dbReaderReads); var fakeDbCommand = A.Fake <IDbCommand>(); A.CallTo(() => fakeDbCommand.ExecuteNonQuery()).Returns(2); A.CallTo(() => fakeDbCommand.ExecuteScalar()).Returns(3); A.CallTo(() => fakeDbCommand.ExecuteReader()).Returns(fakeDbReader); var fakeConnection = A.Fake <IDbConnection>(); var fakeConnectionDisposeCall = A.CallTo(() => fakeConnection.Dispose()); var fakeConnectionOpenCall = A.CallTo(() => fakeConnection.Open()); A.CallTo(() => fakeConnection.CreateCommand()) .Returns(fakeDbCommand); IDbProviderFactory fakeDbProviderFactory = A.Fake <IDbProviderFactory>(); var createConnectionCall = A.CallTo(() => fakeDbProviderFactory.CreateConnection()); createConnectionCall.Returns(fakeConnection); DbOperation dbOperation = new DbOperation(string.Empty, string.Empty, new TraceSource("ss"), fakeDbProviderFactory); dbOperation.ExecuteNonQuery(); int readCount = dbOperation.ExecuteReader(A.Fake <Action <IDataRecord, IDbOperation> >()); Assert.AreEqual(dbReaderReads.Length - 1, readCount); createConnectionCall.MustHaveHappened(Repeated.Exactly.Twice); fakeConnectionOpenCall.MustHaveHappened(Repeated.Exactly.Twice); fakeConnectionDisposeCall.MustHaveHappened(Repeated.Exactly.Twice); }
internal static Database OpenConnectionStringInternal(IDbProviderFactory providerFactory, string connectionString) { return new Database(() => providerFactory.CreateConnection(connectionString)); }
public void DbExceptionThrown() { string msg = Guid.NewGuid().ToString("N"); var fakeDbCommand = A.Fake <IDbCommand>(); A.CallTo(() => fakeDbCommand.ExecuteNonQuery()).Throws(new Exception(msg)); A.CallTo(() => fakeDbCommand.ExecuteScalar()).Throws(new Exception(msg)); A.CallTo(() => fakeDbCommand.ExecuteReader()).Throws(new Exception(msg)); var fakeConnection = A.Fake <IDbConnection>(); A.CallTo(() => fakeConnection.CreateCommand()) .Returns(fakeDbCommand); IDbProviderFactory fakeDbProviderFactory = A.Fake <IDbProviderFactory>(); var createConnectionCall = A.CallTo(() => fakeDbProviderFactory.CreateConnection()); createConnectionCall.Returns(fakeConnection); DbOperation dbOperation = new DbOperation(string.Empty, string.Empty, new TraceSource("ss"), fakeDbProviderFactory); try { dbOperation.ExecuteNonQuery(); Assert.Fail("Expected exception was not thrown."); } catch (Exception e) { Assert.AreEqual(msg, e.Message); } try { dbOperation.ExecuteScalar(); Assert.Fail("Expected exception was not thrown."); } catch (Exception e) { Assert.AreEqual(msg, e.Message); } try { dbOperation.ExecuteReader(A.Fake <Action <IDataRecord, IDbOperation> >()); Assert.Fail("Expected exception was not thrown."); } catch (Exception e) { Assert.AreEqual(msg, e.Message); } try { dbOperation.ExecuteNonQueryAsync().Wait(); Assert.Fail("Expected exception was not thrown."); } catch (Exception e) { Assert.AreEqual(msg, e.Message); } }