以下程序描述如何建立使用自動交易的 Web 服務方法。如果 Web 服務方法參與交易時發生例外狀況,ASP.NET 會自動中止此交易。相同地,如果沒有發生例外狀況,交易便會自動認可。
建立使用自動交易的 Web 服務
匯入 System.WebServices 和 System.EnterpriseServices 命名空間。必要時,您也可以匯入其他命名空間 (例如 System.Data 和 System.Data.SqlClient)。
<%@ WebService Language="VB" Class="Orders" %> <%@ assembly name="System.EnterpriseServices" %> Imports System.Web.Services Imports System.EnterpriseServices<%@ WebService Language="C#" Class="Orders" %> <%@ assembly name="System.EnterpriseServices" %> using System.Web.Services; using System.EnterpriseServices;定義衍生自 WebService 類別的類別。例如,以下程式碼定義了衍生自 WebService 類別且名為 Orders 的類別。
Public Class Orders Inherits WebService End Classpublic class Orders : WebService { }對必須自動參與交易的每個 Web 方法套用 WebMethodAttribute 屬性,並設定交易選項。例如在以下程式碼中,WebMethod 屬性會套用至
DeleteAuthor方法,且 TransactionOption 屬性設為 TransactionOption.RequiresNew。<WebMethod(TransactionOption := TransactionOption.RequiresNew)> _ Public Function DeleteAuthor(lastName As String) As Integer ' Perform the required database task. End Function[ WebMethod(TransactionOption=TransactionOption.RequiresNew)] public int DeleteAuthor(string lastName) { // Perform the required database task. }
範例
<%@ WebService Language="VB" Class="Orders" %>
<%@ assembly name="System.EnterpriseServices" %>
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services
Imports System.Web.Util
Imports System.EnterpriseServices
Public Class Orders
Inherits WebService
<WebMethod(TransactionOption := TransactionOption.RequiresNew)> _
Public Function DeleteAuthor(lastName As String) As Integer
Dim deleteCmd As [String] = "DELETE FROM authors2 where au_lname='"
& lastName & "'"
Dim sqlConn As New SqlConnection("Integrated Security=SSPI;database=pubs;server=myserver")
Dim myCommand As New SqlCommand(deleteCmd, sqlConn)
' If a Web service method is participating in a transaction and
' an exception occurs, ASP.NET automatically aborts the transaction.
' Likewise, if no exception occurs, then the transaction is
' automatically committed.
myCommand.Connection.Open()
Return myCommand.ExecuteNonQuery()
End Function
End Class
<%@ WebService Language="C#" Class="Orders" %>
<%@ assembly name="System.EnterpriseServices" %>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using System.Web.Util;
using System.EnterpriseServices;
public class Orders : WebService
{
[ WebMethod(TransactionOption=TransactionOption.RequiresNew)]
public int DeleteAuthor(string lastName)
{
String deleteCmd = "DELETE FROM authors2
where au_lname='" + lastName + "'" ;
SqlConnection sqlConn = new SqlConnection("Integrated Security=SSPI;database=pubs;server=myserver");
SqlCommand myCommand = new SqlCommand(deleteCmd,sqlConn);
// If a Web service method is participating in a transaction and an
// exception occurs, ASP.NET automatically aborts the transaction.
// Likewise, if no exception occurs, then the transaction is
// automatically committed.
myCommand.Connection.Open();
return myCommand.ExecuteNonQuery();
}
}
請參閱
概念
.gif)
Copyright © 2007 by Microsoft Corporation. All rights reserved.