在在Web Services中WebMethod添加了开始新事务的代码,但不管用,哪们高手帮忙解决一下?
我在Web Services中,添加了事务,但不管用。第一个SQL是正确的,第二个SQL因为表不存在,应该出现错误回滚。但执行时,事务并没有用。我的第一个SQL执行了,还删除了记录。执行到第二个SQL时,把错误的信息返回给了页面上,有谁知道为什么?
全部代码如下:
1、Web Services的方法
[WebMethod(TransactionOption=TransactionOption.RequiresNew)]
public string DeleteOfficer(string unitID)
{
// //删除一个部门时,同时也删除该部门下的所有员工的信息
int cmdResult;
string deleteCmdSQL = "delete t_unit where unitID="+unitID;
string exceptionCausingCmdSQL = "DELETE NonExistingTable WHERE usename='"
+ unitID + "'" ;
SqlConnection sqlConn = new SqlConnection("database=officeYPGL;server=(local);uid=sa;pwd=");
SqlCommand deleteCmd = new SqlCommand(deleteCmdSQL,sqlConn);
SqlCommand exceptionCausingCmd = new SqlCommand(exceptionCausingCmdSQL,sqlConn);
// 以下命令执行正确
deleteCmd.Connection.Open();
cmdResult=deleteCmd.ExecuteNonQuery();
// 以下操作导致异常,Web服务为自动回滚。
cmdResult = exceptionCausingCmd.ExecuteNonQuery();
sqlConn.Close();
return cmdResult.ToString();
}//方法结束
2、调用代码:
private void Button1_Click(object sender, System.EventArgs e)
{
localhost6.Service6 myser=new WebClient1.localhost6.Service6();
this.Label2.Text="执行结果:"+myser.DeleteOfficer(this.TextBox1.Text);
Console.WriteLine("a");
}