当前位置: 首页>>代码示例>>C#>>正文


C# IDbConnection.BeginTransaction方法代码示例

本文整理汇总了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();
    }
开发者ID:prokhor-ozornin,项目名称:Catharsis.NET.Repository,代码行数:11,代码来源:AdoNetTransaction.cs

示例2: UnitOfWork

 public UnitOfWork(string nameOrConnectionString)
 {
     var connectionFactory = new ConnectionFactory(nameOrConnectionString);
     _connection = connectionFactory.Create();
     _connection.Open();
     _transaction = _connection.BeginTransaction();
 }
开发者ID:Manikandhar,项目名称:AspNetIdentityRepPatt,代码行数:7,代码来源:UnitOfWork.cs

示例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");
					}
				}
			}
		}
开发者ID:m-berkani,项目名称:ClearCanvas,代码行数:26,代码来源:DataAccessContext.cs

示例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;
        }
开发者ID:hksarwar,项目名称:JobSystem,代码行数:32,代码来源:WriteCommand.cs

示例5: SqlDBTransaction

 public SqlDBTransaction(SqlDB db)
     : base(db.ConnectionString, db.SqlAnalyzer, db.StrictTables)
 {
     _conn = db._getConnection();
     _conn.Open();
     _transaction = _conn.BeginTransaction();
 }
开发者ID:badjer,项目名称:Lasy,代码行数:7,代码来源:SqlDBTransaction.cs

示例6: Setup

 public void Setup()
 {
     connection = Substitute.For<IDbConnection>();
     transaction = Substitute.For<IDbTransaction>();
     connection.BeginTransaction().Returns(transaction);
     strategy = new TransactionPerScriptStrategy(() => connection);
 }
开发者ID:HojjatK,项目名称:DbUp,代码行数:7,代码来源:TransactionPerScriptStrategyTests.cs

示例7: CommitScope

 public CommitScope([CallerMemberName] string callerMemberName = "")
 {
     _callerMemberName = callerMemberName;
     _connection = new SqlConnection(Common.ConnectionString);
     _connection.Open();
     _transaction = _connection.BeginTransaction();
 }
开发者ID:LosManos,项目名称:St4mpede,代码行数:7,代码来源:CommitScope.cs

示例8: ManagedConnection

 public ManagedConnection(IDbConnection connection)
 {
   _transaction = connection.BeginTransaction();
   _connection = connection;
   Database.ConnectionStorage.Push(_connection);
   Database.TransactionStorage.Push(_transaction);
 }
开发者ID:machine,项目名称:machine.uow,代码行数:7,代码来源:ManagedConnection.cs

示例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.");
        }
开发者ID:ofirgeller,项目名称:Hangfire.PostgreSql,代码行数:59,代码来源:PostgreSqlDistributedLock.cs

示例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);
        }
开发者ID:Kampari,项目名称:MediaBrowser,代码行数:67,代码来源:UserDataMigration.cs

示例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());
 }
开发者ID:HojjatK,项目名称:DbUp,代码行数:8,代码来源:SingleTransactionStrategyTests.cs

示例12: DbStorage

        public DbStorage(string username)
        {
            this.db = DbConfig.GetConnection ();
            this.Username = username;

            // start everything as a transaction
            trans = db.BeginTransaction ();
        }
开发者ID:jonpolak,项目名称:Rainy,代码行数:8,代码来源:DbStorage.cs

示例13: DatabaseTransaction

        public DatabaseTransaction(IDbConnection connection)
        {
            if (_currentTransaction != null)
                return;

            _transaction = connection.BeginTransaction();
            _currentTransaction = this;
        }
开发者ID:calumjiao,项目名称:Mono-Class-Libraries,代码行数:8,代码来源:DatabaseTransaction.cs

示例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);
        }
开发者ID:GitItInTheHub,项目名称:Griffin.Framework,代码行数:16,代码来源:AdoNetUnitOfWork.cs

示例15: DbStorage

        public DbStorage(DBUser user)
        {
            this.db = DbConfig.GetConnection ();
            this.User = user;

            // start everything as a transaction
            trans = db.BeginTransaction ();
        }
开发者ID:BooTeK,项目名称:Rainy,代码行数:8,代码来源:DbStorage.cs


注:本文中的IDbConnection.BeginTransaction方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。