我有两个包含两个具有相同列名的表的对象。 我需要使用脚本tak in ssis在邮件正文中显示这些表值。 邮件应该包含第一行的第一行和第二行的第二行。
下面写的查询仅显示单个表值。
oleDA.Fill(Obj_Duration, Dts.Variables["User::Obj_Duration"].Value);
foreach (DataColumn col in Obj_Duration.Columns)
{
Mail = Mail + "<th bgcolor='#7A378B' align='center'><font color='#FFFFFF'>" + col.ColumnName + "</font></th>";
}
foreach (DataRow row in Obj_Duration.Rows )
{
sMail = sMail + "<tr>";
foreach (DataColumn col in Obj_Duration.Columns)
{
sMail = sMail + "<td align = 'center'>" + row[col.Ordinal].ToString() + "</td>";
}
sMail = sMail + "</tr>";
Mail = Mail + sMail;
sMail = "";
Mail = Mail + "<br>";
}
sMail = "";
Mail = Mail + "</table>";
Mail = Mail + "<br>";
需要更改此查询。
答案 0 :(得分:0)
将第一个表作为foreeach循环遍历,第二个表只使用第一个表行的索引位置来测试并查看是否存在行并从中检索信息而不是尝试将该行作为每个循环< / p>
所以在你的第一个循环中,只需添加如下内容:
int i = Obj_Duration.Rows.IndexOf(row);
if (AlternatingDataTableName.Rows.Count - 1 >= i)
{
DataRow alternatingRow = AlternatingDataTableName.Rows[i];
//Start TR
foreach (DataColumn col in AlternatingDataTableName.Columns )
{
//Start TD
//alternatingRow[col.Ordinal].ToString()
//End TD
}
// end TR
}
只需按照你想要的方式构建字符串我会添加一些注释来暗示你可能想要做什么。并将datatable变量的名称更改为第二个Table变量。为了确保你知道我建议的地方,你的整个代码看起来像是:
oleDA.Fill(Obj_Duration, Dts.Variables["User::Obj_Duration"].Value);
foreach (DataColumn col in Obj_Duration.Columns)
{
Mail = Mail + "<th bgcolor='#7A378B' align='center'><font color='#FFFFFF'>" + col.ColumnName + "</font></th>";
}
foreach (DataRow row in Obj_Duration.Rows )
{
sMail = sMail + "<tr>";
foreach (DataColumn col in Obj_Duration.Columns)
{
sMail = sMail + "<td align = 'center'>" + row[col.Ordinal].ToString() + "</td>";
}
sMail = sMail + "</tr>";
Mail = Mail + sMail;
sMail = "";
Mail = Mail + "<br>";
int i = Obj_Duration.Rows.IndexOf(row);
if (AlternatingDataTableName.Rows.Count - 1 >= i)
{
DataRow alternatingRow = AlternatingDataTableName.Rows[i];
//Start TR
foreach (DataColumn col in AlternatingDataTableName.Columns )
{
//Start TD
//alternatingRow[col.Ordinal].ToString()
//End TD
}
// end TR
}
}
sMail = "";
Mail = Mail + "</table>";
Mail = Mail + "<br>";
}