public partial class WebForm1 : System.Web.UI.Page
{
static string strconn = "Data Source=localhost;database=BOOKS;integrated security=SSPI";
static SqlConnection conn = new SqlConnection(strconn);
static SqlCommand sqlcmd;
static SqlDataReader SqlReader;
protected void Page_Load(object sender, EventArgs e)
{
sqlcmd = conn.CreateCommand();
conn.Open();
sqlcmd.CommandText = "SELECT * FROM ip";
SqlReader = sqlcmd.ExecuteReader();
Response.Write("<Table Border='1'><Tr align='Center'>");
for (int i = 0; i < SqlReader.FieldCount; i++)
{
Response.Write("<Td>" + SqlReader.GetName(i) + "</Td>"); //显示列名
}
Response.Write("</Tr>");
while (SqlReader.Read())
{
for(int i=0;i<SqlReader.FieldCount;i++)
{
Response.Write("<Td>"+SqlReader.GetValue(i)+"</Td>");//显示表中的数据
}
Response.Write("<Tr>");
}
conn.Close();
}