using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class 例4_5 : System.Web.UI.Page
{
    
    protected void cx_Click(object sender, EventArgs e)
    {
        string strConn = "server=(local);database=dbbook;User ID=sa;Pwd=sa1234";
        SqlConnection con = new SqlConnection(strConn);
        con.Open();
         string sql = "select * from book where book_name like'%" + bookname.Text + "%'";
          SqlCommand cmd = new SqlCommand(sql,con);
         SqlDataReader dr = cmd.ExecuteReader();
        try
          {
              int i;
              Response.Write("<Table border='1'><Tr align='center'>");
              for (i = 0; i < dr.FieldCount - 1; i++)
              {
                  Response.Write("<td>" + dr.GetName(i) + "</td>");
              }
              Response.Write("</tr>");
              while (dr.Read())
              {
                  Response.Write("<Tr>");
                  for (i = 0; i < dr.FieldCount - 1; i++)
                  {
                      Response.Write("<td>" + dr.GetValue(i) + "</td>");
                  }
                  Response.Write("</tr>");
              }
              Response.Write("</Table>");
          }
          catch (SqlException ex)
          {
              Response.Write(ex.ToString());
          }
          finally
          {
              dr.Close();
              con.Close();
          }
    }

}
