基础问题,点拨一下

sskset 2005-06-27 10:03:13
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%'?
...全文
138 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
silverseven7 2005-06-27
  • 打赏
  • 举报
回复
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+"%";
zeusvenus 2005-06-27
  • 打赏
  • 举报
回复
先定义个变量字符串,把输入的字符赋予它,然后做个if判断按是否空来写不同的语句啊,有什么问题吗?
Ivony 2005-06-27
  • 打赏
  • 举报
回复
问题1:
sp.Value = this.textBox1.Text;
=>
sp.Value = this.textBox1.Text==""?"%":this.textBox1.Text;

问题2:
sp.Value = this.textBox1.Text+"%";
9sun888 2005-06-27
  • 打赏
  • 举报
回复
楼主:

改成sp.Value = this.textBox1.Text + "%";后,能同时满足你上面所提的2个要求!
9sun888 2005-06-27
  • 打赏
  • 举报
回复
楼主:

你不能把sp.Value = this.textBox1.Text;改成这一句:
sp.Value = this.textBox1.Text + "%";
dqjia 2005-06-27
  • 打赏
  • 举报
回复
like '%%'即显示所有记录
seekg 2005-06-27
  • 打赏
  • 举报
回复
tongshangshang
wxwx110 2005-06-27
  • 打赏
  • 举报
回复
如果textBox1.Text为空那么将做无条件查询相当于
String strSQL = " select * from customers“



如果使用参数只需要对PARAMETERS里的对应的对象的VALUE值做一下字符串的处理就可以实现模糊查询了
xinbin1122 2005-06-27
  • 打赏
  • 举报
回复
if text=""
{
select * from customers

}
else
{
"select * from customers where customerid like '" + text.text + "'"
}
自己把格式补充完整
RedErick 2005-06-27
  • 打赏
  • 举报
回复
String strSQL = " select * from customers where 1=1";
if(this.textBox1.Text != "")
strSQL += " and customerid like '"+ this.textBox1.Text.Trim().Replace("'","''") +"'";
epimetheus 2005-06-27
  • 打赏
  • 举报
回复
别那样写,如果Text文本框为空,就不要那个条件。如果不为空再要那个条件
zhanglizhao 2005-06-27
  • 打赏
  • 举报
回复
没明白楼主的意思
我不懂电脑 2005-06-27
  • 打赏
  • 举报
回复
问题1:只能根据textBox1 的值形成sql语句。
sskset 2005-06-27
  • 打赏
  • 举报
回复
明白了

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧