需要使用Web服务进行DML操作的Web应用程序中处理事务 [英] Need to Handle Transaction in web application, using webservice for DML operation

查看:81
本文介绍了需要使用Web服务进行DML操作的Web应用程序中处理事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照您给定的代码,假设我也有用于Delete和Update的网络方法,
而且我需要在我的Web应用程序中使用它们(WebMethods),在这里我还必须处理事务,
您可以在使用方法时提供代码示例,事务也应该得到处理.
由于我无法使用我的消费应用程序中的connection.BeginTransaction,因此becoz连接部分在webmethod中运行

例如

/应用程序消耗Web方法

As per your given code suppose I have webmethods for Delete and Update too,
And I need to consume them(WebMethods) in my web application where I also have to handle Transaction,
Can you provide with example of codes while consuming method, transaction should also gets handle.
Since I cannot use connection.BeginTransaction in my Consuming application becoz connection part is run inside webmethod

e.g.

/Application Consuming Webmethod

try
{
//Here I need to handle Transaction if any error occurred in 
//middle of sth the transaction should get rolled back
service1.InsertRow(param1, param2);
service1.DeleteRow(param3, param4);
service1.UpdateRow(param5, param6);

}
Catch
{


}




Web服务代码........




web Service code...........

[OperationContract]
public string InsertRow(string empname,string designation)
  { 
  SqlConnection dbConn = null;
  dbConn = new  SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["insightconnection"].ToString());
  
  SqlCommand cmd;
  SqlDataReader dr;
  
  dbcon.Open();
  spcmd = new SqlCommand("SP_InsertRow", spcon);
  spcmd.CommandType = CommandType.StoredProcedure;
  spcmd.Parameters.Add(new SqlParameter("@EMPName", empname));
  spcmd.Parameters.Add(new SqlParameter("@DEsignation", designation));

  dr = cmd.ExecuteReader();
  return "inserted......!!! ";
  }


存储过程代码..............


Stored Procedure code..............

create PROCEDURE [dbo].[SP_InsertRow]
 
 @EMPName varchar(max),
 @DEsignation varchar(max)
AS
BEGIN
 SET NOCOUNT ON;

  insert into EMPDetails values(@EMPName,@DEsignation)
End

推荐答案

如果我正确地理解了这个问题,基本上您可以选择以下三种选择:

1.
在存储过程中定义一个事务.如果您唯一要做的是在逻辑工作单元中调用此单个过程,则这将是可行的.

2.
利用 SqlTransaction [ System.Transactions [
If I understood the question correcly, basically you have three options:

1.
Define a transaction inside the stored procedure. This would be feasible if the only thing you do is that you call this single procedure inside a logical unit of work.

2.
Utilize SqlTransaction[^] in your code. This lets you execute multiple, separate statements from client side in a single transaction

3.
Use System.Transactions[^], especially helpful if you need to embed local resources at client side into the transaction or if you need transaction propagation.


这篇关于需要使用Web服务进行DML操作的Web应用程序中处理事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆