我的脚本很简单:
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
}
但它给出的错误如下:
错误1:'varCollection'是'变量',但用作'方法'
错误2:'Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel'不包含'Results'的定义,也没有扩展方法'Results'接受类型为'Microsoft.SqlServer.Dts.Tasks'的第一个参数。可以找到ScriptTask.ScriptObjectModel'(您是否缺少using指令或程序集引用?)C:\ Users \ AppData \ Local \ Temp \ SSIS \ dfac06a62ee9472bac783737af4957de \ ScriptMain.cs 91 34 st_7c1dde41280d4efc996c50fead62bfa9
答案 0 :(得分:1)
我得到了错误1的答案 因为我正在使用C#需要赋值如:
Dts.Variables["DestinationFolder"].Value = MonthwiseDirectory;
答案 1 :(得分:1)
首先,关于如何使用Stack Overflow的一些事情。
如果您只是询问回答问题How do I create a package that would copy all files from a given folder into a new folder?的人如何在C#中执行此操作,请不要发布新问题。此外,始终提及您在发布的问题中使用的版本。
请不要在回答中发布您的问题。
无论如何,这里是我发布的VB.NET代码的C#代码。我还更新了您的其他问题How do I create a package that would copy all files from a given folder into a new folder?的原始答案,以包含C#代码。
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;
}
答案 2 :(得分:0)
根据this MSDN页面,ScriptObjectModel类没有名为“Results”的属性。
您需要发布代码才能进行全面诊断。