为什么这个事务会出错?
string constr = ConfigurationManager.ConnectionStrings[ "TestConnectionString "].ConnectionString;
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlTransaction tran = con.BeginTransaction();
//生成汇总表
string sqlText1 = "Insert into ... ";
sqlText1 += "... ";
SqlCommand cmd1 = new SqlCommand(sqlText1, con);
cmd1.Parameters.Add(new SqlParameter( "@GZYear ", SqlDbType.Char, 4));
cmd1.Parameters.Add(new SqlParameter( "@GZMonth ", SqlDbType.Char, 2));
//向汇总表插入相应的数据
string sqlText2 = "Update ... ";
sqlText2 += ", ... ";
sqlText2 += ", ... ";
SqlCommand cmd2 = new SqlCommand(sqlText2, con);
cmd2.Parameters.Add(new SqlParameter( "@GZYear ", SqlDbType.Char, 4));
cmd2.Parameters.Add(new SqlParameter( "@GZMonth ", SqlDbType.Char, 2));
try
{
cmd1.Parameters[ "@GZYear "].Value = (String)Session[ "GZYear "];
cmd1.Parameters[ "@GZMonth "].Value = (String)Session[ "GZMonth "];
cmd1.ExecuteNonQuery();
cmd2.Parameters[ "@GZYear "].Value = (String)Session[ "GZYear "];
cmd2.Parameters[ "@GZMonth "].Value = (String)Session[ "GZMonth "];
cmd2.ExecuteNonQuery();
tran.Commit();
showMsg( "生成汇总数据成功,事务已提交! ");