sql语句多条件查询?
,如图所示,不知道图片能否传上去,我想实现的功能是,当文本框为空时,查询所有满足是否在线和是否允许发言的用户,当输输入用户名后按这个三个条件查询,用户名、是否在线、是否允许发言,怎样写sql语句啊,我这是一部分,有错误,
string txtUserName = this.TextBox1.Text.ToString().Trim();
string sqlText = "select name,isonline,isallowmessage from userinformation where 1=1";
if (txtUserName == "")
{
sqlText += "";
}
else if (this.DropDownList1.SelectedValue == "在线")
{
sqlText += "and UserName=@userName and IsOnline=true";
}
else if(this.DropDownList2.SelectedValue=="可以"){
sqlText += "and IsAllowMessage=true";
}
OleDbConnection con = DB.createCon();
con.Open();
DataTable table = new DataTable();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sqlText,con );
OleDbParameter para1 = new OleDbParameter("@userName", OleDbType.WChar, 50);
para1.Value = txtUserName;
dataAdapter.SelectCommand.Parameters.Add(para1);
dataAdapter.Fill(table);
this.GridView1.DataSource = table.DefaultView;
this.GridView1.DataBind();
------解决方案--------------------
string sqlText = "select name,isonline,isallowmessage from userinformation where 1=1";
if (txtUserName == "")
{
sqlText += "";
}
sqlText += this.DropDownList1.SelectedValue.Equals("在线")?"and UserName=@userName and IsOnline=true":"and UserName=@userName and IsOnline=false";
sqlText += "and UserName=@userName and IsOnline=false";
sqlText += this.DropDownList2.SelectedValue.Equals("可以")?"IsAllowMessage=true":"and IsAllowMessage=false";