protected void Button1_Click(object sender, EventArgs e)
{
//动态循环构造表格!还要数据绑定
using(OleDbConnection con=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("~/App_Data/poll.mdb")))
{
con.Open();
using (OleDbCommand cmd=new OleDbCommand ("select * from poll;select * from option2",con))//读取两个表的内容
{
using (OleDbDataReader da=cmd.ExecuteReader (CommandBehavior.CloseConnection))//如果关闭reader对象,管理的con也关闭
{
if(da.HasRows )//判断记录是否为空
{
do{
System.Text.StringBuilder htmlstr = new System.Text.StringBuilder();
htmlstr.Append("<table boder='1' cellPadding='5' cellSpacing='0' style='font-size:;9pt;font:'宋体''>");//表格开始
htmlstr.Append("<tr style='background-color=#f0f0f0'>");//表头开始
for (int i = 0; i < da.FieldCount; i++)
{
htmlstr.Append(string.Format("<td><strong>{0}</strong></td>", da.GetName(i)));//构造表头
}
htmlstr.Append("</tr>");//表头开始
while (da.Read())
{
htmlstr.Append("<tr>");//记录开始
for (int i = 0; i < da.FieldCount; i++)
{
if (!da.IsDBNull(i))
htmlstr.Append(string.Format("<td>{0}</td>", da.GetValue(i)));//构造循环记录
}
htmlstr.Append("</tr>");//记录行结束
}
htmlstr.Append("</table>");//表格结束
Response.Write(htmlstr);
}while(da.NextResult);//读取下一个表格的内容
}
}
}
}
}