C# 将Datatable格式化为类似于电子邮件正文的字符串

C# 将Datatable格式化为类似于电子邮件正文的字符串,c#,datatable,string-formatting,C#,Datatable,String Formatting,我做了一个小的采购申请。用户选择几个订单并单击“发送电子邮件”。使用数据库中的数据为这些特定订单创建电子邮件 我现在就是这样做的: string mailbod = "Following are Orders that need your attention: "; mailbod=od.mailbody(orderid,mailbod);//Calling the method that sets up the string public string mailbody(List&l

我做了一个小的采购申请。用户选择几个订单并单击“发送电子邮件”。使用数据库中的数据为这些特定订单创建电子邮件

我现在就是这样做的:

string mailbod = "Following are Orders that need your attention: ";  
mailbod=od.mailbody(orderid,mailbod);//Calling the method that sets up the string  

public string mailbody(List<int>oids,string mailbod)
        {
            System.IO.StringWriter sw = new System.IO.StringWriter();
            string output = "";
            using (SqlConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("TESTDB")))
            {
                StringBuilder sb = new StringBuilder("Select Distinct a.VEND_NAME,b.*  from dbo.Purch_Vendor a inner join dbo.purch_order b on a.VENDOR_ID=b.VENDOR_ID left join dbo.purch_item c on b.ORDER_ID=c.ORDER_ID Where ");
                if (oids.Count > 0)
                {
                    foreach(int x in oids)
                    {
                        sb.Append("b.ORDER_ID" + "=" +  x  + " OR ");
                    }
                    sb.Length--;
                    sb.Length--;
                    sb.Length--;

                    SqlCommand command = new SqlCommand(sb.ToString(), connection);
                    SqlDataAdapter sqlDataAdap = new SqlDataAdapter(command);
                    DataTable dtRecord = new DataTable();
                    sqlDataAdap.Fill(dtRecord);

                    foreach (DataRow x in dtRecord.Rows)
                    {
                        //' Loop through each column. '
                        for (int i=0;i<dtRecord.Columns.Count; i++)
                        {// ' Output the value of each column's data.

                            sw.Write(x[i].ToString() + ", ");
                        }
                        output = sw.ToString();
                        //' Trim off the trailing ", ", so the output looks correct. '
                        if (output.Length > 2)
                        {
                            output = output.Substring(0, output.Length - 2);
                        }
                        //' Display the row in the console window. '
                        Console.WriteLine(output);
                    }

                    return output;
                }
                else
                {
                    return "";
                }
            }
        }  
string mailbod=“以下是需要您注意的订单:”;
mailbod=od.mailbody(orderid,mailbod)//调用设置字符串的方法
公共字符串邮件体(Listoids、string mailbod)
{
System.IO.StringWriter sw=新的System.IO.StringWriter();
字符串输出=”;
使用(SqlConnection connection=new System.Data.SqlClient.SqlConnection(Helper.CnnVal(“TESTDB”))
{
StringBuilder sb=new StringBuilder(“从dbo.Purch_供应商a中选择不同的a.Vendr_名称,b.*内部连接dbo.Purch_订单b在a.Vendor_ID=b.Vendor_ID左连接dbo.Purch_项目c在b.order_ID=c.order_ID中”);
如果(oids.Count>0)
{
foreach(OID中的整数x)
{
sb.追加(“b.ORDER_ID”+“=”+x+”或“);
}
某人;
某人;
某人;
SqlCommand=newsqlcommand(sb.ToString(),connection);
SqlDataAdapter sqlDataAdap=新SqlDataAdapter(命令);
DataTable dtRecord=新DataTable();
sqlDataAdap.Fill(dtRecord);
foreach(dtRecord.Rows中的数据行x)
{
//“循环遍历每列。”
对于(int i=0;i 2)
{
output=output.Substring(0,output.Length-2);
}
//'在控制台窗口中显示该行。'
控制台写入线(输出);
}
返回输出;
}
其他的
{
返回“”;
}
}
}  
通过电子邮件发送的输出如下所示:

亚马逊,2020年2月20日12:00:00,北纬34.400号kjhgg

我希望得到的输出:
以下是需要您关注的订单:


我怎样才能让它尽可能地工作?任何受欢迎的想法

首先,将其分解,使一种方法具有一种责任。让一个方法返回datatable,然后将其传递给一个方法以获取字符串。您发送的电子邮件是否可以使用HTML格式,因为它可以很容易地格式化为表格,或者您是否仅限于使用纯文本?@RyanThomas感谢您的建议,我们将对此进行清理。我正在使用我在此处找到的电子邮件代码:。我认为它不接受HTML。这个答案是否接受HTML无关紧要。你能在电子邮件中使用HTML吗?如果是这样,那么很容易生成表格数据。如果没有,那就困难多了。然后更改代码以输出表的正确HTML(
等)以包含数据。我同意Ken的观点,只需构建一个HTML字符串并将其设置为正文。您的电子邮件类可能有一个名为UseHtml的属性或类似的属性,您可能必须将其设置为true。更好的是,您可以创建一个.html文件作为模板,然后用表中的行替换一些占位符文本。可能有一个第三方软件包做了类似的事情。