【发布时间】:2016-02-13 17:45:44
【问题描述】:
我有一个 HTML 模板,我有一个返回许多数据的查询。
我想填写我的 html 并通过电子邮件发送
在下面有我的代码。我想知道如何在datatable 中填写我的数据行
到目前为止我已经这样做了:
public void GetInfo()
{
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connectionStrings"].ConnectionString;
SqlConnection sqlConnection = new SqlConnection(connectionString);
string query = "select X,Y,Z,E,U,S,M " +
"from MyTable where M = 0";
DataTable dt = new DataTable();
using (SqlCommand command = new SqlCommand(query, new SqlConnection(connectionString)))
{
command.CommandTimeout = 360;
command.Connection.Open();
dt.Load(command.ExecuteReader());
command.Connection.Close();
}
List<XReportData> lvrd = new List<XReportData>();
if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
XReportData rd = new XReportData();
rd.X = row["X"].ToString();
rd.Y = row["Y"].ToString();
rd.Z = row["Z"].ToString();
rd.E = row["E"].ToString();
rd.U = row["U"].ToString();
rd.S = row["S"].ToString();
rd.M = row["M"].ToString();
lvrd.Add(rd);
}
dt.Dispose();
}
}
这是我的 HTML 模板:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled Document</title>
<style type="text/css">
To AVOID A LONG CODE I DELETED THIS PART HERE
</style>
</head>
<body>
<div class="newlayout_middlepanel_title">
<div id="contentDescriptionDiv" class="newlayout_title">
<span id="ctl00_ContentDescription_Label1">MY MESSAGE</span>
<hr width="100%" style="border: solid 1px #f8f8f8">
</div>
</div>
<div style="text-align: left" class="ResultBox">
<div>
<table cellpadding="5" cellspacing="0" border="1" style="border-collapse: collapse;" class="GridTable">
<tbody>
<tr style="color: White; background-color: #5D7B9D; font-size: Small; font-weight: bold;">
<th scope="col" class="auto-style1">Info 1</th>
<th scope="col" class="auto-style2">Info 2</th>
<th scope="col">Info 3</th>
</tr>
[righeHtml]
<tr align="center" style="color: White; background-color: #284775;">
<td colspan="6">
<div style="height: 10px;"></div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
它看起来像这样: 我的留言
_______________________________________________
| Info 1 | Info2 | Info3 |
-----------------------------------------------
| rd.x , rd.E,...| rd.Y,rd.M |rd.S,rd.Z,...|
and each row with my data
【问题讨论】:
标签: c# html datatable html-email