本文介绍了通过提供2个文本框和2个下拉列表输入从数据库中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public partial class search : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLDbConnection"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fill_ddlPhone();
fill_ddlEmail();
}
}
private void fill_ddlPhone()
{
try
{
string sql = "SELECT * FROM registration";
ddlPhone.Items.Clear();
ddlPhone.AppendDataBoundItems = true;
ddlPhone.Items.Add(new ListItem("Phone", "-1"));
ddlPhone.DataSource = cmscon.getRows(sql);
ddlPhone.DataTextField = "Phone";
ddlPhone.DataValueField = "Phone";
ddlPhone.DataBind();
ddlPhone.SelectedValue = "-1";
}
catch (Exception ex)
{
}
}
private void fill_ddlEmail()
{
try
{
string sql = "SELECT * FROM registration";
ddlEmail.Items.Clear();
ddlEmail.AppendDataBoundItems = true;
ddlEmail.Items.Add(new ListItem("E-Mail", "-1"));
ddlEmail.DataSource = cmscon.getRows(sql);
ddlEmail.DataTextField = "Email";
ddlEmail.DataValueField = "Email";
ddlEmail.DataBind();
ddlEmail.SelectedValue = "-1";
}
catch (Exception ex)
{
}
}
protected void btnSearch_Click(object sender, EventArgs e)
{
String str = "select * from registration where(FirstName like '%' + @search + '%'AND StudentID LIKE '%' + @StId + '%' )";
SqlCommand cmdSearch = new SqlCommand(str, con);
cmdSearch.Parameters.Add("@search", SqlDbType.VarChar).Value = txtContactFirstName.Text;
cmdSearch.Parameters.Add("@StId", SqlDbType.VarChar).Value = txtContactID.Text;
con.Open();
cmdSearch.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmdSearch;
DataSet ds = new DataSet();
DataTable dt = new DataTable();
da.Fill(ds, "FirstName");
da.Fill(ds, "StudentID");
gvSearch.DataSource = ds;
gvSearch.DataBind();
con.Close();
}
}
推荐答案
string str = "select * from registration where FirstName like @search AND StudentID LIKE @StId";
添加参数值时将其设置如下
when you add parameter values set it as below
cmdSearch.Parameters.Add("@search", SqlDbType.VarChar).Value = "%" +txtContactFirstName.Text + "%";
cmdSearch.Parameters.Add("@StId", SqlDbType.VarChar).Value = "%" +txtContactID.Text + "%";
String str = "select * from registration where FirstName like '%@search%' AND StudentID LIKE '%@StId%' )";
这篇关于通过提供2个文本框和2个下拉列表输入从数据库中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!