本文整理汇总了C#中IDbConnection.BeginTransaction方法的典型用法代码示例。如果您正苦于以下问题:C# IDbConnection.BeginTransaction方法的具体用法?C# IDbConnection.BeginTransaction怎么用?C# IDbConnection.BeginTransaction使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IDbConnection
的用法示例。
在下文中一共展示了IDbConnection.BeginTransaction方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AdoNetTransaction
public AdoNetTransaction(IDbConnection connection, IsolationLevel? isolation = null)
{
Assertion.NotNull(connection);
if (connection.State == ConnectionState.Broken || connection.State == ConnectionState.Closed)
{
connection.Open();
}
this.transaction = isolation != null ? connection.BeginTransaction(isolation.Value) : connection.BeginTransaction();
}
示例2: UnitOfWork
public UnitOfWork(string nameOrConnectionString)
{
var connectionFactory = new ConnectionFactory(nameOrConnectionString);
_connection = connectionFactory.Create();
_connection.Open();
_transaction = _connection.BeginTransaction();
}
示例3: DataAccessContext
internal DataAccessContext(string databaseFilename)
{
// initialize a connection and transaction
_databaseFilename = databaseFilename;
_connection = CreateConnection();
_transaction = _connection.BeginTransaction(IsolationLevel.ReadCommitted);
_context = new ConfigurationDataContext(_connection);
//_context.Log = Console.Out;
lock (_syncLock)
{
if (_staticConnection == null)
{
// This is done for performance reasons. It forces a connection to remain open while the
// the app domain is running, so that the database is kept in memory.
try
{
_staticConnection = CreateConnection();
}
catch (Exception ex)
{
Platform.Log(LogLevel.Debug, ex, "Failed to initialize static connection to configuration database");
}
}
}
}
示例4: Execute
public bool Execute(string qry)
{
cn = new OleDbConnection(cnString.GetConnString());
cmd = new OleDbCommand(qry, (OleDbConnection)cn);
try
{
cn.Open();
IDbTransaction tran = cn.BeginTransaction();
cmd.Transaction = tran;
int affectedRows = cmd.ExecuteNonQuery();
Console.WriteLine(affectedRows);
if (affectedRows > 0)
{
tran.Commit();
return true;
}
else
{
tran.Rollback();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
cn.Close();
}
return false;
}
示例5: SqlDBTransaction
public SqlDBTransaction(SqlDB db)
: base(db.ConnectionString, db.SqlAnalyzer, db.StrictTables)
{
_conn = db._getConnection();
_conn.Open();
_transaction = _conn.BeginTransaction();
}
示例6: Setup
public void Setup()
{
connection = Substitute.For<IDbConnection>();
transaction = Substitute.For<IDbTransaction>();
connection.BeginTransaction().Returns(transaction);
strategy = new TransactionPerScriptStrategy(() => connection);
}
示例7: CommitScope
public CommitScope([CallerMemberName] string callerMemberName = "")
{
_callerMemberName = callerMemberName;
_connection = new SqlConnection(Common.ConnectionString);
_connection.Open();
_transaction = _connection.BeginTransaction();
}
示例8: ManagedConnection
public ManagedConnection(IDbConnection connection)
{
_transaction = connection.BeginTransaction();
_connection = connection;
Database.ConnectionStorage.Push(_connection);
Database.TransactionStorage.Push(_transaction);
}
示例9: PostgreSqlDistributedLock_Init_Transaction
private static void PostgreSqlDistributedLock_Init_Transaction(string resource, TimeSpan timeout,
IDbConnection connection, PostgreSqlStorageOptions options)
{
var lockAcquiringTime = Stopwatch.StartNew();
bool tryAcquireLock = true;
while (tryAcquireLock)
{
TryRemoveDeadlock(resource, connection, options);
try
{
int rowsAffected = -1;
using (var trx = connection.BeginTransaction(IsolationLevel.RepeatableRead))
{
rowsAffected = connection.Execute([email protected]"
INSERT INTO ""{options.SchemaName}"".""lock""(""resource"", ""acquired"")
SELECT @resource, @acquired
WHERE NOT EXISTS (
SELECT 1 FROM ""{options.SchemaName}"".""lock""
WHERE ""resource"" = @resource
);
",
new
{
resource = resource,
acquired = DateTime.UtcNow
}, trx);
trx.Commit();
}
if (rowsAffected > 0) return;
}
catch
{
}
if (lockAcquiringTime.ElapsedMilliseconds > timeout.TotalMilliseconds)
{
tryAcquireLock = false;
}
else
{
int sleepDuration = (int)(timeout.TotalMilliseconds - lockAcquiringTime.ElapsedMilliseconds);
if (sleepDuration > 1000) sleepDuration = 1000;
if (sleepDuration > 0)
{
Thread.Sleep(sleepDuration);
}
else
{
tryAcquireLock = false;
}
}
}
throw new PostgreSqlDistributedLockException(
$"Could not place a lock on the resource \'{resource}\': Lock timeout.");
}
示例10: Migrate
/// <summary>
/// Migrates the specified old file.
/// </summary>
/// <param name="oldFile">The old file.</param>
/// <param name="newDatabase">The new database.</param>
/// <param name="logger">The logger.</param>
/// <param name="json">The json.</param>
/// <returns>Task.</returns>
public static async Task Migrate(string oldFile, IDbConnection newDatabase, ILogger logger, IJsonSerializer json)
{
var oldDb = await SqliteExtensions.ConnectToDb(oldFile).ConfigureAwait(false);
using (oldDb)
{
IDbTransaction transaction = null;
var data = GetAllUserData(oldDb, json).ToList();
try
{
transaction = newDatabase.BeginTransaction();
foreach (var userdata in data)
{
PersistUserData(userdata, newDatabase, transaction);
}
transaction.Commit();
}
catch (OperationCanceledException)
{
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
catch (Exception e)
{
logger.ErrorException("Failed to save user data:", e);
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
finally
{
if (transaction != null)
{
transaction.Dispose();
}
}
}
var backupFile = Path.Combine(Path.GetDirectoryName(oldFile), "userdata_v1.db.bak");
if (File.Exists(backupFile))
{
File.Delete(backupFile);
}
File.Move(oldFile, backupFile);
}
示例11: Setup
public void Setup()
{
connection = Substitute.For<IDbConnection>();
transaction = Substitute.For<IDbTransaction>();
connection.BeginTransaction().Returns(transaction);
strategy = new SingleTrasactionStrategy(() => connection);
strategy.Initialise(new ConsoleUpgradeLog());
}
示例12: DbStorage
public DbStorage(string username)
{
this.db = DbConfig.GetConnection ();
this.Username = username;
// start everything as a transaction
trans = db.BeginTransaction ();
}
示例13: DatabaseTransaction
public DatabaseTransaction(IDbConnection connection)
{
if (_currentTransaction != null)
return;
_transaction = connection.BeginTransaction();
_currentTransaction = this;
}
示例14: AdoNetUnitOfWork
/// <summary>
/// </summary>
/// <param name="connection">
/// A connection may not be shared between multiple transactions, so make sure that the connection
/// is unique for this uow
/// </param>
/// <param name="ownsConnection">This unit of work owns the connection and will close it when being disposed.</param>
/// <param name="isolationLevel">Isolation level that the transaction should use.</param>
public AdoNetUnitOfWork(IDbConnection connection, bool ownsConnection, IsolationLevel isolationLevel)
{
if (connection == null) throw new ArgumentNullException("connection");
_connection = connection;
_ownsConnection = ownsConnection;
_transaction = _connection.BeginTransaction(isolationLevel);
}
示例15: DbStorage
public DbStorage(DBUser user)
{
this.db = DbConfig.GetConnection ();
this.User = user;
// start everything as a transaction
trans = db.BeginTransaction ();
}