访问Int变量时SSIS发生异常

时间:2019-02-06 19:47:03

标签: c# sql-server ssis etl script-task

我正在尝试创建一个脚本,该脚本将动态生成我需要的文件夹。

脚本使用FolderNameOrderID变量来生成要创建的文件夹名称。我将它们添加到脚本的只读变量中。

enter image description here

FolderName一切正常,只有当我尝试访问OrderID变量时,事情才会崩溃。我在下面收到了令人讨厌的Exception错误。

    public void Main()
    {
        //  Output File Expression
        //     "" + @[User::FolderName]+"\\Folder_"+ (DT_WSTR,20) @[User::T1_ID]  +"\\SalesOrderHeader_T1_"+ (DT_WSTR,20) @[User::T1_ID]+ ".txt"

        String folderName = (string)Dts.Variables["User::FolderName"].Value;
        String OrderIDStr = (string)Dts.Variables["User::OrderID"].Value;
        System.Windows.Forms.MessageBox.Show(OrderIDStr, "OrderID Var");

        folderName = string.Concat(folderName, "\\Folder_");
        System.Windows.Forms.MessageBox.Show(folderName, "Folder Name");

        try
        {
            // Determine whether the directory exists.
            if (Directory.Exists(folderName))
            {
                Console.WriteLine("That path exists already.");
                System.Windows.Forms.MessageBox.Show("That path exists already.", "WARNING");
                return;
            }

            // Try to create the directory.
                DirectoryInfo di = Directory.CreateDirectory(folderName);
                Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(folderName));
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
        finally { }
    }

我在包级别将值设置为1,并将其强制转换为String,所以我不确定问题出在哪里。

enter image description here

文本在框中被截断,下面是所有内容:

  在System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object)处在System.RuntimeMethodHandle.InvokeMethod(对象目标,Object []参数,签名sig,布尔值构造函数)处的

      obj,Object []参数,Object []参数)在System.Reflection.RuntimeMethodInfo.Invoke       (对象obj,BindingFlags invokeAttr,活页夹活页夹,Object []参数,CultureInfo       文化)位于System.RuntimeType.InvokeMember(字符串名称,BindingFlags       bindingFlags,活页夹活页夹,对象目标,提供的Object [] Args,ParameterModifier []       修饰符,CultureInfo文化,Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()处的名为Params的String []

1 个答案:

答案 0 :(得分:1)

尝试使用.ToString()方法,而不要投射(string)

String OrderIDStr = Dts.Variables["User::OrderID"].Value.ToString();

基于此article,不允许从intstring进行显式强制转换。

参考