string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; using(SqlConnection conn = new SqlConnection(connStr)) { using(SqlDataAdapter adapter = new SqlDataAdapter("select * from userInfo", conn)) { DataTable da = new DataTable(); adapter.Fill(da); if (da.Rows.Count > 0) { StringBuilder sb = new StringBuilder(); foreach (DataRow row in da.Rows) { sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>", row["ID"].ToString(), row["Name"].ToString(), row["UserName"].ToString()); } //读取模板文件,替换占位符 string filePath = context.Request.MapPath("UserInfoList.html"); string fileContent = File.ReadAllText(filePath); fileContent = fileContent.Replace("要替换的占位符", sb.ToString()); context.Response.Write(fileContent); } else { context.Response.Write("暂无数据"); } } }
时间: 2024-10-27 00:18:56