我正在尝试发送一封包含SSIS的电子邮件,其中包含我从SQL查询中获得的结果。我使用脚本任务来创建我的电子邮件消息,我对这个文件的格式很不满意。
我的第一列中的变量长度不一样,我试图找到最大的长度,所以我可以有一个很好的格式化列(邮件上的长度相等)
我正在尝试这样的事情......(请注意,我可以在不格式化的情况下发送邮件)
public void Main()
{
Variables varCollection = null;
string header = string.Empty;
string message = string.Empty;
Dts.VariableDispenser.LockForWrite("User::emailmessage4");
Dts.VariableDispenser.LockForWrite("User::Variable1");
Dts.VariableDispenser.LockForWrite("User::Variable2");
Dts.VariableDispenser.GetVariables(ref varCollection);
//////TEST Extract longest variable lenght
string compilation = string.Empty;
string strmaxlenght = string.Empty;
compilation = string.Format("{0}",varCollection["User::Variable1"].Value);
int veriflenght = 0;
int maxlenght = 0;
veriflenght = compilation.Length;
if (veriflenght > maxlenght)
{
maxlenght = -100 + veriflenght;
}
//strmaxlenght = maxlenght.ToString();
//MessageBox.Show(strmaxlenght);
//////end TEST
//Set the header message for the query result
if (varCollection["User::emailmessage4"].Value == string.Empty)
{
header += string.Format("{0," + maxlenght + "} | {1,-10} \n", "blabla1", "blabla2");
varCollection["User::emailmessage4"].Value = header;
}
//Format the query result with tab delimiters
message = string.Format("{0," + maxlenght + "} | {1,-25} \n",
varCollection["User::Variable1"].Value,
varCollection["User::Variable2"].Value;
varCollection["User::emailmessage4"].Value = varCollection["User::emailmessage4"].Value + message;
Dts.TaskResult = (int)ScriptResults.Success;
}
}
所以,我的问题是我得到每个variable1的maxlength值而不是所有variable1中的实际maxlength。
我也尝试了ForEach循环,总是出现某种二进制错误。