Cast data type problem.Please help!

1. My Script Task use an C# assembly which provide SQL Connection data type as a public property.

SSIS Global variable as  Object data type accept it with no problem.

Dts.Variables("SSISConnection").Value = DTX55AppDefaults.GetSSISConParam()
- - - - - - - - - -
2.  Inside my Script Task I  declare local variable

                 Dim oSSISConnection As SqlClient.SqlConnection

  and try to make assignment like this

 oSSISConnection = CType(CObj(Dts.Variables("SSISConnection")), SqlClient.SqlConnection)
- - - - - - - -
3. When I execute my package it give me  "DTS Script Task: Runtime Error"
"Unable to cast object of type 'Microsoft.SqlServer.DTS.Runtime.Variable' to type 'System.Data.SqlClient.SQLConnection'.
- - - - - - -

Any ideas how-to resolve this problem will be greately appreciated.

Thank you in advance,
Vitaliy











Answer this question

Cast data type problem.Please help!

  • microsci

    Thank you for reply.

    1. Finally I figured it out.

    The code should be the following:
    ----------------------------------------------------------------------
    Option Strict Off

    Imports DTXValidation
    . . . . . .

    Public Sub Main()
     - - - - -
     Dim oSSISConnection As Object
     - - - - - 
     oSSISConnection = DTS.Variables("SSISConnection").Value()
     - - - - -
     DTXValidation.ValidateByPrefixes(oSSISConnection)
     
     Dts.TaskResult = Dts.Success
    --------------------------------------------------------------------
    2. The reason for custom assembly to generate connection is that:

    I need connections which is dynamically picked-up (by custom assembly) from machine.config file.
    I did not find a way to use ConnectionManager for that because it require to set-up pre-defined connection string into SSIS environment.
    Please correct me if I missed some points here.

    Thank you,
    Vitaliy



  • yanivpinhas

    Changing the connection string "dynamically" has been addressed in a couple of ways.

    The first are Configurations. Indeed one of the options is an Xml file. The whole idea is to allow you to store settings outside of the package and apply them immediately prior to execution, even better really than during execution.

    The other is the ability of the execution tools to actually set the connection string when they load the package, and before it is executed. Have a look at the Connections page in DTExcUI for example. There is similar functionality for scheduled jobs through the SQL Agent sub-system or just using DTEXEC in a CmdExec step.

    Machine.config is a nice place, but I would stick with the SSIS supported Configurations, as I see that as more than sufficient, and just less hassle to implement The choice however is yours.



  • CB812

    Dts.Variables("SSISConnection").Value

  • Vachan

     Cim Ryan wrote:
    Dts.Variables("SSISConnection").Value


    Just to be absolutely certain...

    Dts.Variables("SSISConnection").Value.ToString


  • Ceres

    No, Dts.Variables("SSISConnection").Value is all you want, as the variable value is a SqlConnection type judging by the cast, not a (connection) string .

    This does beg the question, why use the custom assembly to generate the connection Would it not be better to use the ADO.NET connection which can return a SqlConnection from AcquireConnection, this way you stick within the SSIS paradigm of using connection managers to handle all resource pointers, in other words all connections.

  • Cast data type problem.Please help!