查詢頁面 (Search)

private void Page_Load(object sender, System.EventArgs e)

         {

              // 在此處放置用戶代碼以初始化頁面

              if(!Page.IsPostBack)

              {

              showAll();

              }

         }

 

         #region Web 窗體設計器生成的代碼

         override protected void OnInit(EventArgs e)

         {

              //

              // CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。

              //

              InitializeComponent();

              base.OnInit(e);

         }

        

         /// <summary>

         /// 設計器支持所需的方法 - 不要使用代碼編輯器修改

         /// 此方法的內容。

         /// </summary>

         private void InitializeComponent()

         {   

              this.Button1.Click += new System.EventHandler(this.Button1_Click);

              this.Load += new System.EventHandler(this.Page_Load);

 

         }

         #endregion

 

         private void Button1_Click(object sender, System.EventArgs e)

         {

        

              string keyw = this.txtKey.Text.Trim();

             

 

              //關鍵字不空執行搜索

              if(keyw !="")

              {

                   SqlConnection conn = new SqlConnection();

                  

                   conn.ConnectionString =

                       "Data Source=(local);user id=sa;password=;initial catalog=zc";

                  

                   //按姓名搜索

                   SqlCommand cmd = new SqlCommand();

                   cmd.CommandText =

                       "Select ID, UserName,Userjg from user_info where UserName like '%"+ keyw +"%'";

                                  

                   cmd.Connection = conn;

 

                   //打開連接,執行操作,返回閱讀器

                   conn.Open();

                   SqlDataReader reader = cmd.ExecuteReader();

 

                   //綁定數據

                   this.myGrid.DataSource = reader;

                   this.myGrid.DataBind();

 

                   reader.Close();

                   conn.Close();

              }

              else

              {

                   showAll();

              }

         }

 

         void showAll()

         {

              SqlConnection conn = new SqlConnection();

              conn.ConnectionString =

                   "Data Source=(local);user id=sa;password=;initial catalog=zc";

             

              SqlCommand cmd = new SqlCommand();

              cmd.CommandText =

                   "Select ID,UserName,Userjg from user_info";

              //按姓名搜索

             

              cmd.Connection = conn;

 

              //打開連接,執行操作,返回閱讀器

              conn.Open();

              SqlDataReader reader = cmd.ExecuteReader();

 

              //綁定數據

              this.myGrid.DataSource = reader;

              this.myGrid.DataBind();

 

              reader.Close();

              conn.Close();

         }

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章