Quantcast
Channel: SQL Server Integration Services forum
Viewing all articles
Browse latest Browse all 24688

trying to copy objects between Databases using C# script with STOCK:TransferSqlServerObjectsTask - problem, not working

$
0
0

hi,

i have found this solution described else where using VB.

but we use C# on our packages and for some reason it does not work.

surely there is something minor that i have missed when testing this script.

certainly, this crowd of experts will spot the problem swiftly ..

while running the package i have found that it tries to find the table under the schema 'dbo' - even though i have defined the schema that i want to use (variable: Schemas).  the profiler shows the following SQL being used:

exec sp_executesql N'SELECT
SCHEMA_NAME(tbl.schema_id) AS [Schema],
tbl.name AS [Name],
tbl.object_id AS [ID]
FROM
sys.tables AS tbl
WHERE
(tbl.name=@_msparam_0 and SCHEMA_NAME(tbl.schema_id)=@_msparam_1)',N'@_msparam_0 nvarchar(4000),@_msparam_1 nvarchar(4000)',@_msparam_0=N'reference_table_2move',@_msparam_1=N'dbo'

below is the script that i am testing.  when i have it working i will use parameters to pass dynamic values within a loop.

#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Collections.Specialized;
#endregion

namespace ST_15980c2e6828423b85d8a12bb1acb870
{
	[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
	public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
	{
		public void Main()
		{
        /*
         * Public Class ScriptMain
         * Original solution proposed by "MohitGupta" as seen in
         * http://social.msdn.microsoft.com/Forums/en-US/sqlintegrationservices/thread/956b79ef-0f28-4288-b0c8-081dc6216a8e
         * this code is from there, and just fills in the missing details
		 * 
		 * and it has being republished here: http://www.bidn.com/blogs/stirone/bidn-blog/273/a-generic-table-refresh-script
         * 
         * Public Sub Main()
         */
        // get table name
            String mvObjName = (String)Dts.Variables["User::mvObj_name"].Value;

            // create child package and xfer task
            Package Child = new Package();
            Executable MoveTable = Child.Executables.Add("STOCK:TransferSqlServerObjectsTask");
            TaskHost MoveTableTask = (TaskHost) MoveTable;

            // set some properties
            MoveTableTask.Properties["CopyAllObjects"].SetValue(MoveTableTask, false);
            MoveTableTask.Properties["CopyAllTables"].SetValue(MoveTableTask, false);
            MoveTableTask.Properties["DropObjectsFirst"].SetValue(MoveTableTask, false);
            MoveTableTask.Properties["CopySchema"].SetValue(MoveTableTask, false);
            MoveTableTask.Properties["CopyData"].SetValue(MoveTableTask, true);

            // set table name
            StringCollection Tables = new StringCollection();
            Tables.Add(mvObjName);
            MoveTableTask.Properties["TablesList"].SetValue(MoveTableTask, Tables);


            // setting schemas, as well
            StringCollection Schemas = new StringCollection();
            Schemas.Add("DIM");

            // needs to set CopyAllSchemas => false 
            // as per docs - http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.dts.tasks.transfersqlserverobjectstask.transfersqlserverobjectstask.aspx
            MoveTableTask.Properties["CopyAllSchemas"].SetValue(MoveTableTask, false);
            MoveTableTask.Properties["SchemasList"].SetValue(MoveTableTask, Schemas);

            
            // set up connections
            ConnectionManager cmSrc;
            ConnectionManager cmDest;

            cmSrc = Child.Connections.Add("SMOServer");
            cmSrc.ConnectionString = "SqlServerName=DEV_SRVR;UseWindowsAuthentication=True;UserName=;";
            cmSrc.Name = "SRC_SRVR";

            cmDest = Child.Connections.Add("SMOServer");
            cmDest.ConnectionString = "SqlServerName=DEV_SRVR;UseWindowsAuthentication=True;UserName=;";
            cmDest.Name = "DEST_SVR";

            MoveTableTask.Properties["SourceConnection"].SetValue(MoveTableTask, "SRC_SRVR");
            MoveTableTask.Properties["SourceDatabase"].SetValue(MoveTableTask, "OURREF_DB");
            MoveTableTask.Properties["DestinationConnection"].SetValue(MoveTableTask, "DEST_SVR");
            MoveTableTask.Properties["DestinationDatabase"].SetValue(MoveTableTask, "TESTBED_PATTERNS");

            // execute package and dispose
            Child.Execute();
            Child.Dispose();
			
			Dts.TaskResult = (int)ScriptResults.Success;
		}

        #region ScriptResults declaration
        /// <summary>
        /// This enum provides a convenient shorthand within the scope of this class for setting the
        /// result of the script.
        /// 
        /// This code was generated automatically.
        /// </summary>
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion


	}
}

thanks for looking into this question.

with regards,

Nicolas


data warehouse | data migration | ETL/ELT | UK | http://www.brainpowered.net/cms


Viewing all articles
Browse latest Browse all 24688

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>