执行多条SQL语句,实现数据库事务。

 /// <summary>
        /// 执行多条SQL语句,实现数据库事务。
        /// </summary>
        /// <param name="SQLStringList">多条SQL语句</param>  
        public int ExecuteSqlTran(List<String> SQLStringList)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                SqlTransaction tx = conn.BeginTransaction();
                cmd.Transaction = tx;
                try
                {
                    int count = 0;
                    for (int n = 0; n < SQLStringList.Count; n++)
                    {
                        string strsql = SQLStringList[n];
                        if (strsql.Trim().Length > 1)
                        {
                            cmd.CommandText = strsql;
                            count += cmd.ExecuteNonQuery();
                        }
                    }
                    tx.Commit();
                    return count;
                }
                catch
                {
                    tx.Rollback();
                    return 0;
                }
            }
        }

///

create procedure [dbo].[p_suspend_business] 
03   @Bfixednum  varchar(40)
04as
05begin transaction
06  
07  declare @SNumber int
08  declare @messageNumber int
09  select @SNumber=SNumber from sendnumber where Bfixednum=@Bfixednum
10  select @messageNumber = count(@Bfixednum) from [message] where Bfixednum = @Bfixednum and mDate between (select  dateadd(dd,-day(getdate())+1,getdate()))  and   (select  dateadd(dd,-day(getdate()),dateadd(m,1,getdate())))
11  
12  
13  --参数检查
14  if (@SNumber is null or @messageNumber = '')
15    begin
16      raiserror('错误!缺少参数 ,请检查!',16,1)
17      rollback
18      return
19    end
20    
21  if (@messageNumber >= @SNumber)
22    begin
23     update dbo.Business set BState=0 where  Bfixednum=@Bfixednum
24   commit transaction    
25    end
26  
27GO
来源

 

转载于:https://www.cnblogs.com/fengdiechang/archive/2010/03/24/1693973.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值