String sqlConnection = "Data Source=(local);Initial Catalog=northwind;User Id=sa;Password=keshan;";
String strSQL = " select * from customers where customerid like @input ";
SqlConnection conn = new SqlConnection(sqlConnection);
conn.Open();
SqlCommand cmd = new SqlCommand(strSQL,conn);
SqlParameter sp = new SqlParameter("@input",SqlDbType.VarChar,20);
sp.Value = this.textBox1.Text;
cmd.Parameters.Add(sp);问题1:String strSQL = " select * from customers where customerid like @input ";
的话,如果 textBox1 的值是空 那么运行的时候就 没有数据,我想,如果textBox1 的值是空那么显示全部数据?以前做项目没有使用参数,想问的是如果使用参数的话大家平常是怎么处理的?问题2:比如说this.textBox1.Text 的值为 aaa
怎么控制sql语句为 select * from customers where customerid like 'aaa%'?

解决方案 »

  1.   

    问题1:只能根据textBox1 的值形成sql语句。
      

  2.   

    别那样写,如果Text文本框为空,就不要那个条件。如果不为空再要那个条件
      

  3.   

    String strSQL = " select * from customers where 1=1";
    if(this.textBox1.Text != "")
       strSQL += " and customerid like '"+ this.textBox1.Text.Trim().Replace("'","''") +"'";
      

  4.   

    if text=""
    {
    select * from customers }
    else
    {
    "select * from customers where customerid like '" + text.text + "'"
    }
    自己把格式补充完整
      

  5.   

    如果textBox1.Text为空那么将做无条件查询相当于
    String strSQL = " select * from customers“如果使用参数只需要对PARAMETERS里的对应的对象的VALUE值做一下字符串的处理就可以实现模糊查询了
      

  6.   

    楼主:你不能把sp.Value = this.textBox1.Text;改成这一句:
    sp.Value = this.textBox1.Text + "%";
      

  7.   

    楼主:改成sp.Value = this.textBox1.Text + "%";后,能同时满足你上面所提的2个要求!
      

  8.   

    问题1:
    sp.Value = this.textBox1.Text;
    =>
    sp.Value = this.textBox1.Text==""?"%":this.textBox1.Text;问题2:
    sp.Value = this.textBox1.Text+"%";
      

  9.   

    sp.Value = this.textBox1.Text;
    =>
    sp.Value = this.textBox1.Text==""?"%":this.textBox1.Text;问题2:
    sp.Value = this.textBox1.Text+"%";
    sp.Value = this.textBox1.Text;
    =>
    sp.Value = this.textBox1.Text==""?"%":this.textBox1.Text;问题2:
    sp.Value = this.textBox1.Text+"%";
    sp.Value = this.textBox1.Text;
    =>
    sp.Value = this.textBox1.Text==""?"%":this.textBox1.Text;问题2:
    sp.Value = this.textBox1.Text+"%";
    sp.Value = this.textBox1.Text;
    =>
    sp.Value = this.textBox1.Text==""?"%":this.textBox1.Text;问题2:
    sp.Value = this.textBox1.Text+"%";