第一、sql中写事物

begin try 
 begin transaction 
 insert into shiwu (asd) values ('aasdasda'); 
 commit transaction 
end try 
begin catch 
 select ERROR_NUMBER() as errornumber 
 rollback transaction 
end catch 

第二、c#中执行事物

 

复制代码
SqlParameter[] paras = new SqlParameter[] { };
                paras = list.ToArray();

                Baitour.Utility.DBHelper db = Baitour.Utility.DBHelper.CreateMapping();
                DbTransaction dbTran = db.CreateTran();
                try
                {
                    //在这里将插入得到的子订单号返回
                    object obj = db.ExecuteScalar(sql.ToString(), paras, dbTran);
                    fsId = obj.ToString();
                    //int index = db.ExecuteNonQuery(sql.ToString(), paras, dbTran);
                    if (!string.IsNullOrWhiteSpace(fsId))
                    {
                        dbTran.Commit();
                        flag = true;
                    }
                }
                catch (Exception ex)
                {
                    flag = false;
                    dbTran.Rollback();
                    //throw;
                }
                finally
                {
                    db.CloseConn();
                }
posted on 2013-06-07 14:59  王洪洪  阅读(157)  评论(0编辑  收藏  举报