在在Web Services中WebMethod添加了开始新事务的代码,但不管用,哪们高手帮忙解决一下?

zdr690929 2006-12-01 08:03:15
我在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");


}
...全文
247 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zdr690929 2006-12-04
  • 打赏
  • 举报
回复
to yan63
我按你的做了 还是不行 有些人说这是vs2003的bug,不知道是不是。
1、WEB服务的代码
[ WebMethod(TransactionOption=TransactionOption.RequiresNew)]
[AutoComplete]
public int DeleteAuthor(string CustomerID)
{

int cmdResult;
string deleteCmdSQL = "DELETE FROM Customers WHERE CustomerID='" +CustomerID + "'" ;
string exceptionCausingCmdSQL = "DELETE FROM NonExistingTable WHERE usename='"
+ CustomerID + "'" ;
SqlConnection sqlConn = new SqlConnection( System.Configuration.ConfigurationSettings.AppSettings["strConnection"]);

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;


}
其中连接串在web.configK上
2、调用
localhost6.TransService myser=new WebClient1.localhost6.TransService();// WebClient1.localhost5.Service5();
myser.DeleteAuthor(this.TextBox1.Text );
Response.Write("删除成功");

3、错误信息:
“/WebClient1/WebClient1”应用程序中的服务器错误。
--------------------------------------------------------------------------------

System.Web.Services.Protocols.SoapException: 服务器无法处理请求。 ---> System.Data.SqlClient.SqlException: 对象名 'NonExistingTable' 无效。 at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at TransActionService.TransService.DeleteAuthor(String CustomerID) in g:\webservicesdemo1\webservicesdemo1\webserver1\transservice.asmx.cs:line 70 --- 内部异常堆栈跟踪的结尾
很明显 是因为表不存在报错。说明事务是不起作用的
yan63 2006-12-02
  • 打赏
  • 举报
回复
使用TransationOption的话确定webserviceclass派生自ServicedComponent
并设置属性autocomplete

[Transaction(TransactionOption.RequiredNew)]
public class Class1 : ServicedComponent {
[AutoComplete]
public void dosth()
{
// do sth...
}


yan63 2006-12-02
  • 打赏
  • 举报
回复
同一个dbconnection中的事物处理可以使用
transation tran;
connection conn = new connection(constring);
tran = con.begintransation();
try
{
// do some thing
tran.commit();
}
catch (exception ex)
{
tran.rollback();
}

这样的写法,
TransactionOption用途在于确定
在于分布式应用中不同调用模块间的事物的上下文
zdr690929 2006-12-01
  • 打赏
  • 举报
回复
错误的信息:
“/WebClient1/WebClient1”应用程序中的服务器错误。
--------------------------------------------------------------------------------

System.Web.Services.Protocols.SoapException: 服务器无法处理请求。 ---> System.Data.SqlClient.SqlException: 对象名 'NonExistingTable' 无效。 at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at WebServer1.Service6.DeleteOfficer(String unitID) in g:\web services\webservicesdemo1\webservicesdemo1\webserver1\service6.asmx.cs:line 74 --- 内部异常堆栈跟踪的结尾 ---


[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/DeleteOfficer", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
行 36: public string DeleteOfficer(string unitID) {
行 37: object[] results = this.Invoke("DeleteOfficer", new object[] {
行 38: unitID});
行 39: return ((string)(results[0]));

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧