事务,锁,并发

来源:互联网 发布:时间序列数据预测 编辑:程序博客网 时间:2024/05/21 11:20

   1、微软自带事务和锁     

       private static object _lock = new object();

           try
            {
              using (TransactionScope scope = new TransactionScope())
                {
                    lock (_lock)
                    {


                    }
                    scope.Complete();
                }
            }
            catch (Exception ex)
            {
                TJson.setSucc(jOutput, false, "添加失败");
                TTracer.WriteLog(ex.ToString());

            }


2、     StringBuilder sWhere = new StringBuilder();
            IList<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            TransactionOptions transactionOption = new TransactionOptions();
            transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;//设置事务的隔离级别
            transactionOption.Timeout = new TimeSpan(0, 0, 60); //设置事务超时时间60秒
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required,transactionOption))
            {
                foreach (var item in queryWhere)
                {
                    if (item.Value.ToString() != "")
                    {
                        sWhere.Append(" and " + item.Key + " like '%" + item.Value + "%'");
                    }
                }
                string mySQL = string.Format(@"(select * from EHECD_StoreInformation where 1=1 {0})", sWhere.ToMSSQLValue());
                string etpageSQL = EtPageSQL.GetMSSsqlPagingSQL(pageNumber, pageSize, mySQL);
                list = dbhelper.GetDictionaryList(etpageSQL);
                scope.Complete();
               
            }
            return list;

0 0
原创粉丝点击