我正在尝试用SSIS中的对象变量填充数据表,但我遇到了问题。第一次通过脚本组件时,数据表会被填充,但是当下一行通过脚本时,代码不会填充数据集,并且整个脚本无法正常工作。有什么帮助解决这个问题?
ScriptComponent Code
Main.cs
/// <summary>
/// This method is called once for every row that passes through the component from Input0.
///
/// Example of reading a value from a column in the the row:
/// string zipCode = Row.ZipCode
///
/// Example of writing a value to a column in the row:
/// Row.ZipCode = zipCode
/// </summary>
/// <param name="Row">The row that is currently passing through the component</param>
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
OleDbDataAdapter sc = new OleDbDataAdapter();
DataTable dt = new DataTable();
sc.Fill(dt, Variables.SpecialCharacter); //Problem occurs here, Fill method doesn't fill after first row is processed by the script.
List<StringValidation> listObj = new List<StringValidation>();
foreach (DataRow row in dt.Rows)
{
object[] array = row.ItemArray;
StringValidation varObj = new StringValidation();
varObj.SourceCharacter = array[0].ToString();
varObj.TargetCode = array[1].ToString();
listObj.Add(varObj);
}
StringValidation obj = new StringValidation();
Row.ADDRL1 = obj.RemoveInvalidCharacter(Row.ADDRL1, listObj, "ADDRL1");
Row.ADDRL2 = obj.RemoveInvalidCharacter(Row.ADDRL2, listObj, "ADDRL2");
}
StringValidation.cs
public class StringValidation
{
public string SourceCharacter { get; set; }
public string TargetCode { get; set; }
public string RemoveInvalidCharacter(string text, List<StringValidation> listObj, string ColumnName)
{
foreach (StringValidation obj in listObj)
{
if (text.Contains(obj.SourceCharacter))
{
text = text.Replace(obj.SourceCharacter, obj.TargetCode);
}
}
return text;
}
}
答案 0 :(得分:1)
将只需运行一次的代码移至PreExecute()
尝试将DataTable填充移动到PreExecute()方法中。它只需要加载一次,因此如果你这样做,多次加载引起的任何问题都应该消失。请参阅下面的代码(和评论)。
/* Turn adapter and table into member variables */
OleDbDataAdapter sc = new OleDbDataAdapter();
DataTable dt = new DataTable();
public override void PreExecute()
{
/* Fill data table in PreExecute(). Only needs to be loaded once. If it only works the first time this should fix it. */
sc.Fill(dt, Variables.SpecialCharacter);
}
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
List<StringValidation> listObj = new List<StringValidation>();
foreach (DataRow row in dt.Rows)
{
object[] array = row.ItemArray;
StringValidation varObj = new StringValidation();
varObj.SourceCharacter = array[0].ToString();
varObj.TargetCode = array[1].ToString();
listObj.Add(varObj);
}
StringValidation obj = new StringValidation();
Row.ADDRL1 = obj.RemoveInvalidCharacter(Row.ADDRL1, listObj, "ADDRL1");
Row.ADDRL2 = obj.RemoveInvalidCharacter(Row.ADDRL2, listObj, "ADDRL2");
}
并尝试使用此功能来提高性能和更清晰的代码
如果将所有内容移到PreExecute()部分(见下文),您可能会获得更好的性能。我还使用Linq来简化创建List()对象。
/* Don't forget to add using System.Linq; at the top of the page. */
List<StringValidation> stringValidations;
public override void PreExecute()
{
OleDbDataAdapter sc = new OleDbDataAdapter();
DataTable dt = new DataTable();
sc.Fill(dt, Variables.SpecialCharacter);
stringValidations = (from scRow in dt.Rows
select new StringValidation
{
SourceCharacter = scRow[0].ToString(),
TargetCode = scRow[1].ToString()
}).ToList();
}
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
List<StringValidation> listObj = new List<StringValidation>();
StringValidation obj = new StringValidation();
Row.ADDRL1 = obj.RemoveInvalidCharacter(Row.ADDRL1, stringValidations, "ADDRL1");
Row.ADDRL2 = obj.RemoveInvalidCharacter(Row.ADDRL2, stringValidations, "ADDRL2");
}