aa = "select * from ceshi where " + this.DropDownList1.SelectedValue + "='" + this.TextBox1.Text.Trim() +"'";
aa = "select * from ceshi where '" + this.DropDownList1.SelectedValue + "'='" +this.TextBox1.Text.Trim() +"'";
aa = "select * from ceshi where '" + this.DropDownList1.SelectedValue + "'=''"+this.TextBox1.Text.Trim() +"''"; //数据库字段是文本类型
第一条是对的,但文本变量this.DropDownList1.SelectedValue好像要用''引起来才行,如第二句,为什么可以不引起来;但第二句查询不到,因为我的this.TextBox1.Text.Trim()只输入数字,好像也要用''引起来才行(不知对不对,但"select * from ceshi where id='1'"是行的),如第三句;但第三句执行是错的,语法错误 (操作符丢失) 在查询表达式 ''id'=''1''' 中,这是什么问题?请高手帮忙!!请分析具体一些!
aa = "select * from ceshi where '" + this.DropDownList1.SelectedValue + "'='" +this.TextBox1.Text.Trim() +"'";
aa = "select * from ceshi where '" + this.DropDownList1.SelectedValue + "'=''"+this.TextBox1.Text.Trim() +"''"; //数据库字段是文本类型
第一条是对的,但文本变量this.DropDownList1.SelectedValue好像要用''引起来才行,如第二句,为什么可以不引起来;但第二句查询不到,因为我的this.TextBox1.Text.Trim()只输入数字,好像也要用''引起来才行(不知对不对,但"select * from ceshi where id='1'"是行的),如第三句;但第三句执行是错的,语法错误 (操作符丢失) 在查询表达式 ''id'=''1''' 中,这是什么问题?请高手帮忙!!请分析具体一些!
第3行改成这样
如果是数字的话写作
aa = "select * from ceshi where "' + this.DropDownList1.SelectedValue + '"="+this.TextBox1.Text.Trim() ;
试试
[/code]"select * from ceshi where id='1'"是行的
这里的1是作为字符串处理的,不是数字
aa = "select * from ceshi where '" + this.DropDownList1.SelectedValue + "'=''"+this.TextBox1.Text.Trim() +"''"; 我试了好像不对,我是问1楼的this.DropDownList1.SelectedValue(是文本变量)没有引起来,行吗?
"select * from ceshi where" +this.DropDownList1.SelectedValue等于什么?
"select * from ceshi where id"是不是这个啊```
理解错了,应该是这样吧,楼主试试
你的第二第三种方法肯定是错误的,把where 后面的 用'引起来,那岂不是字符串和字符串比较了?
为什么第二个查不出东西?你所比较的是两个字符串,加入你想得到的效果是ID='1',因为用到了',所以说成了:(设TextBox1.Text="1")
... where 'ID'='1'
很显然,原本你的意思是数据库中ID这一字段是1的,给他选出来,但是现在成了字符串比较了,字符ID是和1不相等的,所以说加'是不对的。
第一种应该是正确的,但是如果你数据库中所查询的字段的类型是数字,那么他的值就不能被'引起来。
也就是说:如果数据库中ID是int型的,那么直接(程序中): "...where ID="+TextBox.Text.Trim();
this.DropDownList1.SelectedValue,
this.TextBox1.Text.Trim()
);如果这个字段的值是数字就不用加引号,字段最好用[]包起来