Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C#

Using the BizTalk SQL Adapter to synchronize data

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
26 Nov 2009CPOL1 min read 24.7K   5   2
How to use the BizTalk SQL Adapter to synchronize data by invoking a Stored Procedure.

Introduction

In my previous post, we used UPDATEGRAM to sync data. In this tutorial, we use a trigger to monitor the actions in the source database and create a new table named ACTIONHISTORY to store the actions including the action type, table name, primary key, key value, and status.

BizTalk server captured updated records by using a Stored Procedure. Then, BizTalk calls the target Stored Procedure with these parameters to update the records in the target database.

Using the code

First, we create a trigger to monitor the actions. Here we use UPDATE, for example.

SQL
Create TRIGGER [dbo].[Tri_Monitor_Customers_Update]
ON [dbo].[Customers]AFTER Update

-- Here is the statement to actually see the text of the trigger.

AS 
DECLARE @ActionType varchar(12)
DECLARE @TablePKValue varchar(12)

Set  @ActionType=2;  --perform to updated action

INSERT INTO [SqlAdapterTest].[dbo].[ActionHistory]
           ([TableName]          
           ,[ActionType]           
           ,[TablePK]           
           ,[TablePKValue]            
           ,[ActionStatus])
select 'Customers',@ActionType,'CustomerID',CustomerID,'New' from inserted

Create a Stored Procedure to Extract the Updated Records

SQL
Create PROCEDURE [dbo].[SP_Action_List_Customer]
      -- Add the parameters for the stored procedure here
@Rating as nvarchar(10)
as 
declare @ActionType as nvarchar(10) 
declare @TableName as nvarchar(10)
declare @TablePk as nvarchar(10)
declare @TablePKValue as nvarchar(10)
declare @ActionID as nvarchar(10)

BEGIN

 select top 1  @ActionID=ActionID,@ActionType=ActionType,
     @TablePKValue=TablePKValue from ActionHistory where ActionStatus='New'
     -- SET NOCOUNT ON added to prevent extra result sets from
     -- interfering with SELECT statements.if @ActionID is not null
           begin
               update ActionHistory set ActionStatus='Used' where ActionID=@ActionID
                   begin
                   if @ActionType='1'
                   SELECT @ActionType as ActionType,
                       *from dbo.Customers where CustomerID=@TablePKValue for xml auto
                   end

                   begin
                   if @ActionType='2'
                   SELECT @ActionType as ActionType,
                       *from dbo.Customers where CustomerID=@TablePKValue for xml auto
                   end

                   begin
                   if @ActionType='0'
                  
                   SELECT top 1 @ActionType as ActionType,
                       CustomerID=@TablePKValue from dbo.Customers for xml auto
                   end
            end
  else   
    /* no new customer, return an empty set */
    select @ActionType as ActionType,* from Customers where 1 = 0 for xml auto
    SET NOCOUNT ON;

END

In the target database, we create a Stored Procedure too. Here we take DELETE and INSERT actions, for example.

SQL
begin transaction 

begin try 

if @Action='0'  --Delete 

      BEGIN 
       delete from  Customers where CustomerID=@CustomerID 
       delete from  Interest  where Rating=@CustomerID--to delete othertable 
     END 

Else if @Action='1' --Insert 

BEGIN 

/*BEGIN Interest table*/ 
      IF @Rating  IS NOT NULL 

begin 
            DECLARE @TempInterestID int  --Check the item 
                  SELECT @TempInterestID = count(1) FROM [Interest] WHERE Rating=@Rating 

            if @TempInterestID=0 

            begin 
                  INSERT INTO [Interest]( 
                  [Rating],[InterestRate] 
                  )VALUES( 
                  @Rating,@InterestRate 
                  ) 
            end 
            else

So far, we have created a procedure to capture and update data. Similar to the previous post in part 1, we should create a BizTalk project first. The steps involved for this are:

  • Step 1: Create a Biztalk project in Visual Studio
  • Step 2: Add the generated items using the SQL Adapter
  • Step 3: Map the schema
  • Step 4: Configure the orchestration
  • Step 5: Rebulid and deploy the project
  • Step 6:Configure the application in the BizTalk Server 2009 Administrator Console.

The following is a screenshot of the steps:

Image 1

Step 3. Map schema

Summary

This tutorial illustrates the synchronization between databases using the BizTalk SQL Adapter. With the BizTalk SQL Adapter, we can sync multiple tables or multiple records between the DB or the ERP, and so on.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer WC
United States United States
personal website: www.liumaofan.com

Comments and Discussions

 
QuestionI need Orchestration Code of This project Pin
Mohbat Rai Birhman7-May-20 4:07
Mohbat Rai Birhman7-May-20 4:07 
QuestionBigData Pin
Ivi Kazazi25-Apr-16 4:02
Ivi Kazazi25-Apr-16 4:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.