我们如何从SQL查询格式化(分组)电子邮件?

时间:2017-09-22 10:10:22

标签: c# winforms visual-studio-2015

我有代码,我正在运行SQL查询以将变量字符串编写并将其写入Outlook。代码工作正常但是当有多个结果时我遇到了一些麻烦。每个结果都在电子邮件中一起运行......

结果:

  

感谢您的订单!

     

订单号20016721订单号20016721
       产品编号:NRE0000Part号码:PWR1812
      描述:NRE ChargeDescription:高功率USB充电器
      客户部件号:1234客户零件号:1234
      客户修订:客户修订:A
      预计发货日期:9/22/2017预计发货日期:9/22/2017

     

此致

我想要的是什么:

  

感谢您的订单!

     

订单号20016721
      零件号:NRE0000
      描述:NRE费用
      客户部件号:1234
      客户修订:A
      预期   发货日期:2017年9月22日

     

订单号20016721
      产品编号:PWR1812
      描述:高   电源USB充电器
     客户部件号:1234
     客户修订:A
  预计发货日期:9/28/2017

     

此致

这是我的代码:

 string ord = "";
        string pn = "";
        string des = "";
        string cpn = "";
        string rev = "";
        string esd = "";
        string qty = "";
        string eml = "";
        string sbj = "";

        string str = @"Data Source=my source;Initial Catalog=table;Integrated Security=True";
        SqlConnection scn;
        SqlDataAdapter da;
        DataSet ds;


        myTableAdapter.SO(_A_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 SalesOrders WHERE Ack <> 'Y'AND SalesOrder =" + MyGlobals.ord, scn);
        ds = new DataSet();da.Fill(ds, "SalesOrders");
        foreach(DataRow Row in ds.Tables["SalesOrders"].Rows)
        {
            ord = ord + "Order Number "+ Row["Sales Order"];
            pn = pn + "Part Number: " + Row["Part Number"];
            des = des + "Description: " + Row["Description"];
            cpn = cpn + "Customer Part Number: " + Row["Customer Part No"];
            rev = rev + "Customer Revision: " + Row["Customer Rev"];
            DateTime dte = DateTime.Now;               
            esd = esd + "Expected Ship Date: " + dte.ToShortDateString();
            qty = qty + "Quantity: " + Row["Quantity"];
            eml = eml +  Row["Email"];
            sbj = sbj + "Order Acknowledgement for your PO " + Row["Customer PO"];
        }


        try 
        {

            //Must add Outlook Reference Object Library

            Outlook.Application _app = new Outlook.Application();
            Outlook.MailItem mail = (Outlook.MailItem)_app.CreateItem(Outlook.OlItemType.olMailItem);

            mail.To = eml;
            mail.BCC = ";CustomerService@touchstn.com";
            mail.Subject = sbj;
            mail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
            mail.HTMLBody = "<HTML><BODY>";

            mail.HTMLBody += "Thank you for your order!" + "<br />" + "<br />" + ord + "<br />" + pn +
                              "<br />" + des + "<br />" + cpn + "<br />" + rev + "<br />" + esd + "<br />"
                               + "<br />"+ "Regards," 



            mail.Display(true);
           // mail.Send();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

提前致谢

更新:结果表格使用下面建议的代码(差不多!!)

  

感谢您的订单!
   订单号20016721部件号:   NRE0000说明:NRE ChargeCustomer部件号:客户修订:   预计发货日期:9/22/2017数量:1订货号20016721Part   编号:PWR1812说明:高功率USB充电器客户端部件   编号:客户修订:预计发货日期:9/22/2017数量:400

     

此致

2 个答案:

答案 0 :(得分:2)

您需要做的是创建一个包含订单详细信息的字符串,其中包含换行符。

请考虑以下代码。

using System.Text;
[...]

var orderListBody = new StringBuilder();
var eml = string.Empty;
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 />"); //Adding extra line break between two orders.
  eml = Row["Email"];
  sbj = "Order Acknowledgement for your PO " + Row["Customer PO"] + "<br/>";
}

HTMLBody应设置如下。

mail.To = eml;
mail.BCC = ";CustomerService@touchstn.com";
mail.Subject = sbj;
mail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
mail.HTMLBody = "<HTML><BODY>";

mail.HTMLBody += "Thank you for your order!" + "<br />" + orderListBody.ToString() + "<br />"+ "Regards," ;

这可以解决您的问题。

答案 1 :(得分:-1)

您要做的只是在循环中将多个结果附加到单个变量中。我可以从你的例子中收集到的是你有两个不同的命令,需要在outlook的html体中打印。为此,您需要为每个订单维护不同的变量(不建议)。您可以创建一组订单细节(例如ord,pn,des,cpn,rev,esd),然后使用类对象存储每个订单细节的值。 (根据零件编号区分订单。(准确地处理for循环中的代码)。

按要求编辑:

下面是一个例子。在需要的地方处理语法。 (我在回家的路上打字)

//你的课程细节将会是这样的......

class for order specifics

//你的主要逻辑所在的实际类 your actual class where your main logic is there

这种方式可能不是处理大量记录的最有效方法,但代码更清晰。希望这可以帮助。享受!