动态将数据库中的内容导出
查询Student中的所有学生记录,并在table中显示。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace webdemo1
{
public partial class select : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string constr = "data source=.;initial catalog=TestSchool;user id=sa;password=7777777";
using (SqlConnection conn = new SqlConnection(constr))
{
conn.Open();
string sql = "select * from TblScore";//利用占位符
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.BorderStyle = BorderStyle.Solid;
tc.Text = reader.GetValue(0).ToString();
tr.Cells.Add(tc);
tc = new TableCell();
tc.BorderStyle = BorderStyle.Solid;
tc.Text = reader.GetValue(1).ToString();
tr.Cells.Add(tc);
tc = new TableCell();
tc.BorderStyle = BorderStyle.Solid;
tc.Text = reader.GetValue(2).ToString();
tr.Cells.Add(tc);
Table1.Rows.Add(tr);
}
}
}
cmd.ExecuteReader();//insert,delete,update用正合适
}
}
}
}
}