Replication Script Properties

The following properties are for use in any function in a replication script.

SourceConnection
InternalSourceConnection
SourceConnectionName
SourceTableName
TargetConnection
InternalTargetConnection
TargetConnectionName
TargetTableName
GroupName
ReplicationName

SourceConnection

Returns the connection for the source table. A reference to assembly 'System.Data.dll' is required in the Reference dialog.

Syniti DR handles both the opening and the closing of the connection, but there are certain conditions (due to the use of threads in Syniti DR) under which you need to lock the connection. If you are using more than one reader event or more than one writer event in the same replication, and those events use a connection object, that connection object needs to be synchronized to avoid the use of the same object inside different threads. To synchronize objects you can use the command SyncLock in VB.NET or lock in C# as in the following examples:

VB.NET SyncLock Example

Dim conn as IDbConnection = Nothing
Dim cmd as IDbCommand = Nothing 
Dim reader as IDataReader = Nothing 
Try 
   conn = SourceConnection 
   SyncLock(conn) 
      cmd = conn.CreateCommand 
      cmd.CommandText = "select count(*) from MARCO.dbo.EMP50" 
      reader = cmd.ExecuteReader 
      Dim iNumRecords = -1 
      If reader.Read Then 
         iNumRecords = reader.GetInt32(0) 
      End If 
      AddLog("Reader - Record in EMPNO: " & iNumRecords.ToString, 0) 
   End SyncLock 
   Catch ex As Exception 
   AddLog("Exception in Record_onAfterMapping: " & ex.ToString,0) 
   Finally 
   if (not reader is Nothing) Then 
      reader.Close 
   End If 
   if (not cmd is Nothing) Then 
      cmd.Dispose 
   End If 
End Try

C# lock Example

public override void Record_onBeforeMapping(DBMotoPublic.IRecord recSource, ref bool AbortRecord)
 {
   IDbConnection conn = null;
   IDbCommand cmd = null;
   IDataReader reader = null;
   try
   {
     conn = SourceConnection;
     lock ((conn))
     {
        cmd = conn.CreateCommand();
        cmd.CommandText = "select count(*) from HITDB.dbo.EMP50";
        reader = cmd.ExecuteReader();
        int iNumRecords = -1;
        if (reader.Read())
        {
           iNumRecords = reader.GetInt32(0);
        }
        GlobalScript.AddLog("Reader - Record in EMPNO: " + iNumRecords.ToString(), 0);
     }
   }
   catch (Exception ex)
   {
       GlobalScript.AddLog("Exception in Record_onAfterMapping: " + ex.ToString(), 0);
    }
   finally
    {
      if (((reader != null)))
      {
         reader.Close();
      }
      if (((cmd != null)))
      {
         cmd.Dispose();
      }
    }
 }

VB.NET syntax

Public SourceConnection As System.Data.IDbConnection

C# syntax

public System.Data.IDbConnection SourceConnection;

Example [VB.NET syntax]

Public Overrides Sub Record_onBeforeMapping(recSource As IRecord, ByRef AbortRecord As Boolean)
   AddLog("SC connstring = " + SourceConnection.ConnectionString ,0)
   AddLog("SC timeout = " + SourceConnection.ConnectionTimeout.ToString() ,0)
   AddLog("SC db = " + SourceConnection.Database ,0)
   AddLog("SC state = " + SourceConnection.State.ToString() ,0)
End Sub

Example [C# syntax]

public override void Record_onBeforeMapping(IRecord recSource, ref bool AbortRecord)
{
   GlobalScript.AddLog("SC connstring = " + SourceConnection.ConnectionString, 0);
GlobalScript.AddLog("SC timeout = " + SourceConnection.ConnectionTimeout.ToString(), 0);
GlobalScript.AddLog("SC db = " + SourceConnection.Database, 0); GlobalScript.AddLog("SC state = " + SourceConnection.State.ToString(), 0); }

InternalSourceConnection

Returns the .NET connection used internally by Syniti DR for accessing to the source and to the target database, unlike SourceConnection which returns as clone of the connection object used by the Replication Agent engine. This property is useful in cases where a statement must be executed using the same connection as the one used by the engine. WARNING: Use this property with great care because improper use may compromise the success of the replication. Any error generated by running a script using this connection will affect the Replication Agent and the replication process. For example if the connection is closed by the script, the engine will fail. Typically it is preferable to use the SourceConnection property.

VB.Net syntax

Public InternalSourceConnection As System.Data.IDbConnection

C# syntax

public System.Data.IDbConnection InternalSourceConnection;

Example  [VB.NET syntax]

For the replication used in this example, the source database is IBM System i, and the  target database is Microsoft SQL Server 2005. The source table contains an integer field which is mapped to a field with datatype Int Identity in SQL Server 2005. The Int Identity field is an auto number field so, without using a script such as the one below, the field in the source table which maps to the Int Identity field in the target will not replicated. Using the following script, the data in the source field will be replicated to the target field correctly.

   Public Overrides Sub Refresh_onBeforeRefresh()
     Dim conn as IDbConnection = Nothing
     Dim cmd as IDbCommand = Nothing
     Dim reader as IDataReader = Nothing
    Try
       conn = InternalTargetConnection  
       cmd = conn.CreateCommand
       cmd.CommandText = "SET IDENTITY_INSERT SAMPLE.dbo.TarEmp ON"
       reader = cmd.ExecuteReader      
    Catch ex As Exception
       AddLog("Exception in Internal Target Connection: " & ex.ToString,0)
    Finally
       if (not reader is Nothing) Then
          reader.Close
       End If
       if (not cmd is Nothing) Then
          cmd.Dispose
       End If
    End Try
  End Sub

Example  [C# syntax]

For the replication used in this example, the source database is IBM System i, and the  target database is Microsoft SQL Server 2005. The source table contains an integer field which is mapped to a field with datatype Int Identity in SQL Server 2005. The Int Identity field is an auto number field so, without using a script such as the one below, the field in the source table which maps to the Int Identity field in the target will not replicated. Using the following script, the data in the source field will be replicated to the target field correctly.

public override void Refresh_onBeforeRefresh()
{
  IDbConnection conn = null;
IDbCommand cmd = null;
IDataReader reader = null; try { conn = InternalTargetConnection; cmd = conn.CreateCommand; cmd.CommandText = "SET IDENTITY_INSERT SAMPLE.dbo.TarEmp ON"; reader = cmd.ExecuteReader; } catch (Exception ex) { GlobalScript.AddLog("Exception in Internal Target Connection: " + ex.ToString, 0); } finally { if (((reader != null))) { reader.Close(); } if (((cmd != null))) {  cmd.Dispose(); } } }

SourceConnectionName

Returns the name of the source connection as specified in Syniti DR.

VB.NET syntax

Public SourceConnectionName As String

C# syntax

public string SourceConnectionName;

SourceTableName

Returns the name of the source table used in the replication.

VB.NET syntax

Public SourceTableName As String

C# syntax

public string SourceConnectionName;

TargetConnection

Returns the connection for the Target table. A reference to assembly 'System.Data.dll' is required in the Reference dialog.

Syniti DR handles both the opening and the closing of the connection, but there are certain conditions (due to the use of threads in Syniti DR) under which you need to lock the connection.  See the SourceConnection property for more information.

VB.NET syntax

Public TargetConnection As System.Data.IDbConnection

C# syntax

public System.Data.IDbConnection TargetConnection;

Example [VB.NET syntax]

Public Overrides Sub Record_onBeforeMapping(recSource As IRecord, ByRef AbortRecord As Boolean)
   AddLog("TC connstring = " + TargetConnection.ConnectionString ,0)
   AddLog("TC timeout = " + TargetConnection.ConnectionTimeout.ToString() ,0)
   AddLog("TC db = " + TargetConnection.Database ,0)
   AddLog("TC state = " + TargetConnection.State.ToString() ,0)
End Sub

Example [C# syntax]

public override void Record_onBeforeMapping(IRecord recSource, ref bool AbortRecord)
{
   GlobalScript.AddLog("TC connstring = " + TargetConnection.ConnectionString, 0);
   GlobalScript.AddLog("TC timeout = " + TargetConnection.ConnectionTimeout.ToString(), 0);
   GlobalScript.AddLog("TC db = " + TargetConnection.Database, 0);
   GlobalScript.AddLog("TC state = " + TargetConnection.State.ToString(), 0);
}

InternalTargetConnection

Returns the .NET connection used internally by Syniti DR for accessing to the source and to the target database, unlike TargetConnection which returns as clone of the connection object used by the Replication Agent engine. This property is useful in cases where a statement must be executed using the same connection as the one used by the engine. WARNING: Use this property with great care  because improper use may compromise the success of the replication.  Any error generated by running a script using this connection will affect the Replication Agent and the replication process. For example if the connection is closed by the script, the engine will fail. Typically it is preferable to use the TargetConnection property.

VB.Net syntax

Public InternalTargetConnection As System.Data.IDbConnection

C# syntax

public System.Data.IDbConnection InternalTargetConnection;

Example

See the TargetConnection property.

TargetConnectionName

Returns the name of the target connection as specified in Syniti DR.

VB.NET syntax

Public TargetConnectionName As String

C# syntax

public string TargetConnectionName;

TargetTableName

Returns the name of the target table used in the replication.

VB.NET syntax

Public TargetTableName As String

C# syntax

public string TargetTableName;

GroupName

Returns the name, if any, of the group to which the replication belongs.

VB.NET syntax

Public GroupName As String

C# syntax

public string GroupName;

ReplicationName

Returns the name of the replication.

VB.NET syntax

Public ReplicationName As String

C# syntax

public string ReplicationName;

Related Topics
Replication Script Events

Handling Events for INSERT, UPDATE and DELETE Operations

IRecord Interface

Global Script Functions

Writing a Global Script

Writing a Replication Script

Writing Scripts with Visual Basic .NET