Esempio n. 1
0
        public static void updateAndAddTransaction()
        {
            IDbAccess db = IDbFactory.CreateIDb(connStr, dbType);

            try
            {
                db.IsTran = true;
                db.BeginTransaction();
                //add
                string    tableName = "templjqfortest";
                Hashtable ht        = new Hashtable();
                ht.Add("name", "hello db11");
                ht.Add("createtime", new DateTime(2011, 2, 1, 2, 2, 2, 11));
                ht.Add("supertiem", null);

                Console.WriteLine("" + db.AddData(tableName, ht));
                //delete
                string filterStr = "and name >='hello db7'";
                Console.WriteLine("delete is " + db.DeleteRow(tableName, filterStr));
                db.Commit();
            }
            catch (Exception e)
            {
                db.Rollback();
                db.Close();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Executes a non query in a transaction
        /// </summary>
        /// <param name="dbAccess">The database access.</param>
        /// <param name="cmd">The command.</param>
        /// <param name="useTransaction">if set to <c>true</c> [use transaction].</param>
        /// <param name="isolationLevel">The isolation level.</param>
        /// <returns>Returns the Result</returns>
        public static int ExecuteNonQuery(
            [NotNull] this IDbAccess dbAccess, [NotNull] IDbCommand cmd, bool useTransaction, IsolationLevel isolationLevel = IsolationLevel.ReadUncommitted)
        {
            CodeContracts.VerifyNotNull(dbAccess, "dbAccess");
            CodeContracts.VerifyNotNull(cmd, "cmd");

            if (!useTransaction)
            {
                return(dbAccess.ExecuteNonQuery(cmd));
            }

            using (var dbTransaction = dbAccess.BeginTransaction(isolationLevel))
            {
                var result = dbAccess.Execute(c => c.ExecuteNonQuery(), cmd, dbTransaction);
                dbTransaction.Commit();

                return(result);
            }
        }