International Developer Logo Last Updated 27.08.08 at 11.48
On Sale
This months front cover, click to see the table of contents.
Subscribe
 
TUTORIALS

Managed data access inside SQL Server with ADO.NET and SQLCLR


Pablo Castro   13.02.06


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




   Previous Page  ... 11 12 13 14 15 16 17 18 19 20 ... Next Page   

HAVE YOUR SAY
This article is rated  Rate this article