String strConnection=ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection cn=new SqlConnection(strConnection); string name1 = TextBox1.Text;
string name2 = Dp1.SelectedValue; SqlDataAdapter da = new SqlDataAdapter("select id,sysdate,name,lianxi,wentilist,wenti,ipaddress,chuli from login where name like '%"+name1+"%'and wentilist='"+name2+"'",cn);
DataSet ds=new DataSet();
da.Fill(ds,"login");
DataGrid1.DataSource=ds.Tables["login"].DefaultView;
DataGrid1.DataBind();上面这个代码能实现  
name1+name2  查找
单独name2查找
不能实现name1+name2中的"全部"查找.<asp:ListItem Value="全部">全部</asp:ListItem> 这个全部在数据库没有
下面这个代码能实现:
name1+name2中的"全部"查找   <asp:ListItem Value="全部">全部</asp:ListItem>
单独name2查找
不能实现:name1+name2  查找
String strConnection=ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection cn=new SqlConnection(strConnection); string name = Request.QueryString["TextBox1"];
string name1 = TextBox1.Text;
string name2 = Dp1.SelectedValue;
DropDownList.Items.Add("全部");
string sqlcommand="";
if(this.TextBox1.Text.Trim().Length>0)
{
sqlcommand = "select id,sysdate,name,lianxi,wentilist,wenti,ipaddress,chuli from login where name like '%"+name1+"%'";
}
else
{
sqlcommand="select id,sysdate,name,lianxi,wentilist,wenti,ipaddress,chuli from login where name like '%"+name1+"%'and wentilist='"+name2+"'";
}
SqlDataAdapter da = new SqlDataAdapter(sqlcommand,cn); DataSet ds=new DataSet();
da.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();

解决方案 »

  1.   

    String strConnection=ConfigurationSettings.AppSettings["ConnectionString"];
    SqlConnection cn=new SqlConnection(strConnection); string name1 = TextBox1.Text;
    string name2 = Dp1.SelectedValue;
    string strSql="select id,sysdate,name,lianxi,wentilist,wenti,ipaddress,chuli from login where 1=1";
    if(name1!="")
    {
    strSql+=" and name like '%"+name1+"%'";
    }
    if(this.Dp1.SelectedIndex>0)
    {
    strSql+=" and wentilist='"+name2+"'";
    } SqlDataAdapter da = new SqlDataAdapter(strSql,cn);
    DataSet ds=new DataSet();
    da.Fill(ds,"login");
    DataGrid1.DataSource=ds.Tables["login"].DefaultView;
    DataGrid1.DataBind();