C# winform

博客园 首页 联系 订阅 管理

 /// <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)
04 as
05 begin 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   
27 GO
来源

 

posted on 2010-03-24 16:54  fffdc  阅读(1106)  评论(2编辑  收藏  举报