开发者

Why getting following error in SSIS script?

开发者_高级运维I have simple script :

 public void Main()
    {
        // TODO: Add your code here
        Dts.TaskResult = (int)ScriptResults.Success;


        //MYCode

        Variables varCollection = null;
     

        string DestinationRoot = varCollection("User::DestinationRoot").Value.ToString();
        int MonthStartPosition = Convert.ToInt32(varCollection("User::MonthStartPosition").Value);
        int MonthLength = Convert.ToInt32(varCollection("User::MonthLength").Value);
        int MonthValue = 0;
        string MonthNameFormat = varCollection("User::MonthNameFormat").Value.ToString();
        string FolderName = string.Empty;
        string MonthwiseDirectory = string.Empty;

        if (MonthStartPosition > 0 && MonthLength > 0)
        {
            MonthValue = Convert.ToInt32(FileName.Substring(MonthStartPosition - 1, MonthLength));
        }

        if (FileName.Length > 0 && MonthValue > 0)
        {
            FolderName = new DateTime(1, MonthValue, 1).ToString(MonthNameFormat);
        }

        MonthwiseDirectory = System.IO.Path.Combine(DestinationRoot, FolderName);

        if (!System.IO.Directory.Exists(MonthwiseDirectory))
        {
            System.IO.Directory.CreateDirectory(MonthwiseDirectory);
        }

        varCollection("User::DestinationFolder").Value = MonthwiseDirectory;

//Error 1 : also getting error here like 'varCollection' is a 'variable' but is used like a 'method'   

        Dts.TaskResult = Dts.Results.Success; //Error 2 
    }

But it gives error like :

Error 1 : 'varCollection' is a 'variable' but is used like a 'method'

Error 2: 'Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel' does not contain a definition for 'Results' and no extension method 'Results' accepting a first argument of type 'Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel' could be found (are you missing a using directive or an assembly reference?) C:\Users\AppData\Local\Temp\SSIS\dfac06a62ee9472bac783737af4957de\ScriptMain.cs 91 34 st_7c1dde41280d4efc996c50fead62bfa9


First of all, couple of things about how to use Stack Overflow.

  1. Please do not post new questions when you can just ask the person who answered your question How do I create a package that would copy all files from a given folder into a new folder? about how to do this in C#. Also, always mention the version that you are using in the questions that you post.

  2. Please do not post your questions in answer.

Anyways, here is the C# code for the VB.NET code that I posted. I have also updated the original answer to your other question How do I create a package that would copy all files from a given folder into a new folder? to include the C# code.

public void Main()
{
    Variables varCollection = null;
    Dts.VariableDispenser.LockForRead("User::SourceFilePath");
    Dts.VariableDispenser.LockForRead("User::DestinationRoot");
    Dts.VariableDispenser.LockForRead("User::MonthStartPosition");
    Dts.VariableDispenser.LockForRead("User::MonthLength");
    Dts.VariableDispenser.LockForRead("User::MonthNameFormat");
    Dts.VariableDispenser.LockForWrite("User::DestinationFolder");
    Dts.VariableDispenser.GetVariables(ref varCollection);

    string SourceFilePath = varCollection["User::SourceFilePath"].Value.ToString();
    string FileName = SourceFilePath.Substring(SourceFilePath.LastIndexOf('\\') + 1);
    string DestinationRoot = varCollection["User::DestinationRoot"].Value.ToString();
    int MonthStartPosition = Convert.ToInt32(varCollection["User::MonthStartPosition"].Value);
    int MonthLength = Convert.ToInt32(varCollection["User::MonthLength"].Value);
    int MonthValue = 0;
    string MonthNameFormat = varCollection["User::MonthNameFormat"].Value.ToString();
    string FolderName = string.Empty;
    string MonthwiseDirectory = string.Empty;

    if (MonthStartPosition > 0 && MonthLength > 0)
    {
        MonthValue = Convert.ToInt32(FileName.Substring(MonthStartPosition - 1, MonthLength));
    }

    if (FileName.Length > 0 && MonthValue > 0)
    {
        FolderName = new DateTime(1, MonthValue, 1).ToString(MonthNameFormat);
    }

    MonthwiseDirectory = System.IO.Path.Combine(DestinationRoot, FolderName);

    if (!System.IO.Directory.Exists(MonthwiseDirectory))
    {
        System.IO.Directory.CreateDirectory(MonthwiseDirectory);
    }

    varCollection["User::DestinationFolder"].Value = MonthwiseDirectory;

    Dts.TaskResult = (int)ScriptResults.Success;
}


I got the answer for Error 1 As I'm using C# need to assign value like :

Dts.Variables["DestinationFolder"].Value = MonthwiseDirectory;


The ScriptObjectModel class does not have a property called "Results" according to this MSDN page.

You'll need to post your code for a fully diagnosis.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜