主题:帮忙看看这个事务 为什么不行了 谢谢
string conn="server=(local);database=test;user=sa;password=";
SqlConnection myconn=new SqlConnection(conn);
string comm1="insert into stuid(id,name,sex,addr) values (@id,@name,@sex,@addr)";
string comm2="insert into course(id,course,score) values (@id,@course,@score)";
SqlCommand mycomm=new SqlCommand();
mycomm.Connection=myconn;
myconn.Open();
SqlTransaction tx=myconn.BeginTransaction();
mycomm.Transaction=tx;
try
{
mycomm.Parameters.Add("@id",SqlDbType.Int,4,"id");
mycomm.Parameters.Add("@name",SqlDbType.NChar,10,"name");
mycomm.Parameters.Add("@sex",SqlDbType.Int,4,"sex");
mycomm.Parameters.Add("@addr",SqlDbType.NChar,20,"addr");
mycomm.Parameters["@id"].Value=textBox_ID.Text;
mycomm.Parameters["@name"].Value=textBox_Name.Text;
mycomm.Parameters["@sex"].Value=textBox_Sex.Text;
mycomm.Parameters["@addr"].Value=textBox_Addr.Text;
mycomm.CommandText=comm1;
mycomm.ExecuteNonQuery();
//
mycomm.Parameters.Add("@id",SqlDbType.Int,4,"id");
mycomm.Parameters.Add("@course",SqlDbType.NChar,10,"course");
mycomm.Parameters.Add("@score",SqlDbType.Int,4,"score");
mycomm.Parameters["@id"].Value=textBox_ID.Text;
mycomm.Parameters["@course"].Value=textBox_Course.Text;
mycomm.Parameters["@score"].Value=textBox_Score.Text;
mycomm.CommandText=comm2;
mycomm.ExecuteNonQuery();
tx.Commit();
MessageBox.Show("ok");
}
catch(Exception eee)
{
tx.Rollback();
MessageBox.Show("error");
}
finally
{
myconn.Close();
}
SqlConnection myconn=new SqlConnection(conn);
string comm1="insert into stuid(id,name,sex,addr) values (@id,@name,@sex,@addr)";
string comm2="insert into course(id,course,score) values (@id,@course,@score)";
SqlCommand mycomm=new SqlCommand();
mycomm.Connection=myconn;
myconn.Open();
SqlTransaction tx=myconn.BeginTransaction();
mycomm.Transaction=tx;
try
{
mycomm.Parameters.Add("@id",SqlDbType.Int,4,"id");
mycomm.Parameters.Add("@name",SqlDbType.NChar,10,"name");
mycomm.Parameters.Add("@sex",SqlDbType.Int,4,"sex");
mycomm.Parameters.Add("@addr",SqlDbType.NChar,20,"addr");
mycomm.Parameters["@id"].Value=textBox_ID.Text;
mycomm.Parameters["@name"].Value=textBox_Name.Text;
mycomm.Parameters["@sex"].Value=textBox_Sex.Text;
mycomm.Parameters["@addr"].Value=textBox_Addr.Text;
mycomm.CommandText=comm1;
mycomm.ExecuteNonQuery();
//
mycomm.Parameters.Add("@id",SqlDbType.Int,4,"id");
mycomm.Parameters.Add("@course",SqlDbType.NChar,10,"course");
mycomm.Parameters.Add("@score",SqlDbType.Int,4,"score");
mycomm.Parameters["@id"].Value=textBox_ID.Text;
mycomm.Parameters["@course"].Value=textBox_Course.Text;
mycomm.Parameters["@score"].Value=textBox_Score.Text;
mycomm.CommandText=comm2;
mycomm.ExecuteNonQuery();
tx.Commit();
MessageBox.Show("ok");
}
catch(Exception eee)
{
tx.Rollback();
MessageBox.Show("error");
}
finally
{
myconn.Close();
}