--- title: "Replication Script Properties" slug: "replication-script-properties" updated: 2024-03-28T17:43:27Z published: 2024-03-28T17:43:27Z canonical: "skthelp.syniti.com/replication-script-properties" --- > ## Documentation Index > Fetch the complete documentation index at: https://skthelp.syniti.com/llms.txt > Use this file to discover all available pages before exploring further. # Replication Script Properties The following properties are for use in any function in a [replication script](/v1/docs/writing-scripts). ## SourceConnection Returns the connection for the source table. A reference to assembly 'System.Data.dll' is required in the Reference dialog. Syniti Replicate handles both the opening and the closing of the connection, but there are certain conditions (due to the use of threads in Syniti Replicate) under which you need to lock the connection. If you are using more than one [reader event](/v1/docs/managing-reader-and-writer-events-in-a-script) or more than one [writer event](/v1/docs/managing-reader-and-writer-events-in-a-script) 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 ```plaintext 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 ```plaintext 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 ```plaintext Public SourceConnection As System.Data.IDbConnection ``` ### C# syntax ```plaintext public System.Data.IDbConnection SourceConnection; ``` ### Example [VB.NET syntax] ```plaintext 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] ```plaintext 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 Replicate 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](/v1/docs/replication-script-properties#targetconnection) property. ### VB.Net syntax ```plaintext Public InternalSourceConnection As System.Data.IDbConnection ``` ### C# syntax ```plaintext 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. ```plaintext    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. ```plaintext 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 Replicate. ### VB.NET syntax ```plaintext Public SourceConnectionName As String ``` ### C# syntax ```plaintext public string SourceConnectionName; ``` ## SourceTableName Returns the name of the source table used in the replication. ### VB.NET syntax ```plaintext Public SourceTableName As String ``` ### C# syntax ```plaintext 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 Replicate handles both the opening and the closing of the connection, but there are certain conditions (due to the use of threads in Syniti Replicate) under which you need to lock the connection. See the [SourceConnection](/v1/docs/replication-script-properties#targetconnection) property for more information. ### VB.NET syntax ```plaintext Public TargetConnection As System.Data.IDbConnection ``` ### C# syntax ```plaintext public System.Data.IDbConnection TargetConnection; ``` ### Example [VB.NET syntax] ```plaintext 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] ```plaintext 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 Replicate 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](/v1/docs/replication-script-properties#targetconnection) property. ### VB.Net syntax ```plaintext Public InternalTargetConnection As System.Data.IDbConnection ``` ### C# syntax ```plaintext public System.Data.IDbConnection InternalTargetConnection; ``` ### Example See the [TargetConnection](/v1/docs/replication-script-properties#targetconnection) property. ## TargetConnectionName Returns the name of the target connection as specified in Syniti Replicate. ### VB.NET syntax ```plaintext Public TargetConnectionName As String ``` ### C# syntax ```plaintext public string TargetConnectionName; ``` ## TargetTableName Returns the name of the target table used in the replication. ### VB.NET syntax ```plaintext Public TargetTableName As String ``` ### C# syntax ```plaintext public string TargetTableName; ``` ## GroupName Returns the name, if any, of the group to which the replication belongs. ### VB.NET syntax ```plaintext Public GroupName As String ``` ### C# syntax ```plaintext public string GroupName; ``` ## ReplicationName Returns the name of the replication. ### VB.NET syntax ```plaintext Public ReplicationName As String ``` ### C# syntax ```plaintext public string ReplicationName; ``` **Related Topics** [Replication Script Events](/v1/docs/replication-script-events) [Handling Events for INSERT, UPDATE and DELETE Operations](/v1/docs/handling-events-for-insert-update-and-delete-operations) [IRecord Interface](/v1/docs/irecord-interface) [Global Script Functions](/v1/docs/global-script-functions) [Writing a Global Script](/v1/docs/writing-a-global-script) [Writing a Replication Script](/v1/docs/writing-a-replication-script) [Writing Scripts with Visual Basic .NET](/v1/docs/writing-scripts-in-syniti-replicate)