嗨!!各位讀者大家好,今天我們就繼續討論我們的程式碼!!
這段程式碼主要的功能是要做模糊搜尋!!這是一種在資料庫裡常見的搜索模式!!
protected void Button6_Click(object sender, EventArgs e)
{
if (AcoutAndPasswordchecknum == 1)
{
int checknum = 0;
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
string basestr = "select * from strtable where 1=1";
conn.Open();
SqlCommand cmd = new SqlCommand();
if (Textbox5.Text != "")
{
basestr += "and([str_id]like '%'+ @input_id +'%')";
cmd.Parameters.AddWithValue("input_id", Textbox5.Text);
}
if (Textbox6.Text != "")
{
basestr += "and([str_password]like '%'+ @input_password +'%')";
cmd.Parameters.AddWithValue("input_password", Textbox6.Text);
}
cmd.CommandText = basestr;
cmd.Connection = conn;
SqlDataReader dr = cmd.ExecuteReader();
GridView2.DataSource = dr;
GridView2.DataBind();
cmd.Cancel();
dr.Close();
conn.Close();
GridView2.Visible = true;
Response.Write("<script>window.alert(\"查詢成功!\");</script>");
}
else
{
Response.Write("<script>window.alert(\"您輸入的帳密錯誤\");</script>");
}
//Response.Write("just chekck");
} //push to check that page_load had been proceed
大家可以注意到裡面有一個比較特別的地方!!就是我們這裡的if
的判斷式,其中一個是判斷id
另一個則是判斷密碼!!
if (Textbox5.Text != "")
{
basestr += "and([str_id]like '%'+ @input_id +'%')";
cmd.Parameters.AddWithValue("input_id", Textbox5.Text);
}
if (Textbox6.Text != "")
{
basestr += "and([str_password]like '%'+ @input_password +'%')";
cmd.Parameters.AddWithValue("input_password", Textbox6.Text);
}