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;
}