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%'?
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%'?
if(this.textBox1.Text != "")
strSQL += " and customerid like '"+ this.textBox1.Text.Trim().Replace("'","''") +"'";
{
select * from customers }
else
{
"select * from customers where customerid like '" + text.text + "'"
}
自己把格式补充完整
String strSQL = " select * from customers“如果使用参数只需要对PARAMETERS里的对应的对象的VALUE值做一下字符串的处理就可以实现模糊查询了
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==""?"%":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+"%";