C#
using System.Data;
using System.Data.SqlClient;
using System.Transactions;
using Microsoft.SqlServer.Server;
public partial class StoredProcedures {
[Microsoft.SqlServer.Server.
SqlProcedure()]
public static void SampleSP() {
// start a transaction block
using(TransactionScope tx =
new TransactionScope()) {
// connect to the context connection
using(SqlConnection conn =
new SqlConnection("context
connection=true")) {
conn.Open();
// do some changes to the local database
}
// connect to the remote database
using(SqlConnection conn =
new SqlConnection("server=MyServer;
database=AdventureWorks;" +
"user id=MyUser;
password=MyPassword")) {
conn.Open();
// do some changes to the remote database
}
// mark the transaction as complete
tx.Complete();
}
}
}
Visual Basic .NET
Imports System.Data
Imports System.Data.SqlClient
Imports System.Transactions
Imports Microsoft.SqlServer.Server
Partial Public Class StoredProcedures
<Microsoft.SqlServer.Server.
SqlProcedure()> _
Public Shared Sub SampleSP()
' start a transaction block
Using tx As New TransactionScope()
' connect to the context connection
Using conn As New SqlConnection
("context connection=true")
conn.Open()
' do some changes to the local database
End Using
' connect to a remote server (don't hardcode the
' conn string in real code)
Using conn As New SqlConnection
("server=MyServer;
database=AdventureWorks;" & _
"user id=MyUser;
password=MyPassword")
conn.Open()
' do some changes to the remote database
End Using
' mark the transaction as completed
tx.Complete()
End Using
End Sub
End Class