net C#程序中的事务

public bool Insert_Week_Plan_Chan(DataTable data, DataTable dtWeekPlan,string classicYearID,string year,string ChannelID)
       {
           bool state = false;
           SqlConnection conn = new SqlConnection(DBBase.strconn);
           conn.Open();

           SqlCommand cmd = new SqlCommand();
           SqlTransaction tran=conn.BeginTransaction(IsolationLevel.ReadCommitted);
           cmd.Connection = conn;
           cmd.Transaction = tran;
           try
           {
               foreach (DataRow dr in data.Rows)
               {
                  
                  string sql = "exec pr_UW_InsertWeekPlan_Chan '"+dr["ChannelID"].ToString()+"',"+int.Parse(dr["ClassicYearID"].ToString())+","
                      +int.Parse(dr["UWYear"].ToString())+",'"+dr["WeekID"].ToString()+"','"+dr["SeasonName"].ToString()+"',"
                      +float.Parse(dr["CropCount"].ToString());
                  cmd.CommandText=sql;
                   cmd.CommandType=CommandType.Text;
                   cmd.ExecuteNonQuery();
               }

               foreach (DataRow dr2 in dtWeekPlan.Rows)
               {
                   string sql3 = "DELETE UW_WEEK_PLAN WHERE ClassicYearID=" + dr2["ClassicYearID"].ToString() + " AND UWYear=" + dr2["UWYear"].ToString() + " AND UWWeek='" + dr2["UWWeek"].ToString() + "' and UWQuarter='" + dr2["UWSeason"].ToString() + "' and ChannelID='"+dr2["ChannelID"].ToString()+"'";
                   cmd.CommandText = sql3;
                   cmd.CommandType = CommandType.Text;
                   cmd.ExecuteNonQuery();
                   string sql2 = "INSERT INTO UW_WEEK_PLAN(ChannelID,ClassicYearID,UWYear,UWWeek,UWQuarter,UWCrop,UWRain,UWIrrigate)VALUES('" + dr2["ChannelID"].ToString() + "'," + dr2["ClassicYearID"].ToString() + "," + dr2["UWYear"].ToString() + ",'" + dr2["UWWeek"].ToString() + "','" + dr2["UWSeason"].ToString() + "'," + float.Parse(dr2["UWCrop"].ToString()) + "," + float.Parse(dr2["UWRain"].ToString()) + "," + float.Parse(dr2["UWIrrigate"].ToString()) + ")";
                   cmd.CommandText = sql2;
                   cmd.CommandType = CommandType.Text;
                   cmd.ExecuteNonQuery();
               }

               string sql4 = "exec pr_UW_FinalWaterPlanState " + year + "," + classicYearID + ",'" + ChannelID + "'";
               cmd.CommandText = sql4;
               cmd.CommandType = CommandType.Text;
               cmd.ExecuteNonQuery();

               cmd.Transaction.Commit();
               state = true;
           }
           catch {
               cmd.Transaction.Rollback();
               state = false;
           }
           finally
           {
               conn.Close();
           }
           return state;

       }

转载于:https://www.cnblogs.com/30ErLi/archive/2010/09/17/1828900.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值