我有这个漂亮的代码,它从sql表中获取所选数据并将其添加到电子邮件正文中。它(由于堆栈溢出天才的帮助)将返回多个记录并将它们非常好地分组到电子邮件正文中。问题是 - 电子邮件字段。当然,它位于For each循环中并返回该记录的多个实例 - 这反过来又写入电子邮件中的To字段。此记录始终相同,我只想将一个实例写入我的变量(eml)。我猜这很简单 - 但我没有提出一个很好的解决方案,而不是再次执行查询。
string str = @"Data Source=srvr;Initial Catalog=db;Integrated
Security=True";
SqlConnection scn;
SqlDataAdapter da;
DataSet ds;
salesOrdersTableAdapter.SO(_TST_TWIDataSet.SalesOrders);
scn = new SqlConnection(str);
da = new SqlDataAdapter("SELECT DISTINCT DATEADD (dd, DATEDIFF(dd,0,ShipDate),0) AS ShipDate,RTRIM(SalesOrder) AS [Sales Order], RTRIM(PartNum) AS [Part Number]," +
"RTRIM(Description) AS Description,RTRIM(DueQty) AS Quantity,RTRIM(CustPartNum) AS[Customer Part No], RTRIM(CustPo) AS[Customer PO], " +
"RTRIM(CustRev) AS[Customer Rev], RTRIM(email) AS [Email] " +
"FROM tbl WHERE Ack <> 'Y'AND SalesOrder =" + MyGlobals.ord, scn);
ds = new DataSet();da.Fill(ds, "SalesOrders");
var orderListBody = new StringBuilder();
var sbj = string.Empty;
foreach (DataRow Row in ds.Tables["SalesOrders"].Rows)
{
orderListBody.Append("Order Number " + Row["Sales Order"] + "<br />");
orderListBody.Append("Part Number: " + Row["Part Number"] + "<br />");
orderListBody.Append("Description: " + Row["Description"] + "<br />");
orderListBody.Append("Customer Part Number: " + Row["Customer Part No"] + "<br />");
orderListBody.Append("Customer Revision: " + Row["Customer Rev"] + "<br />");
DateTime dte = DateTime.Now;
orderListBody.Append("Expected Ship Date: " + dte.ToShortDateString() + "<br />");
orderListBody.Append("Quantity: " + Row["Quantity"] + "<br />");
orderListBody.Append("<br />");
// eml += Row["Email"];
sbj = "Order Acknowledgement for your PO " + Row["Customer PO"];
}
提前致谢
答案 0 :(得分:1)
只需在行索引器返回的ToString()
上调用object
:
eml = Row["Email"].ToString();