我做一个人事系统,可以用编号,姓名等对数据库进行查询,在用编号进行查询时,select * from students where state=true and numbers= & val(txtnumber.text)是可以的,但是我用姓名查找时,总是不行。select * from students where state=true and names= & txtname.text。我总检查不出错在哪里,你们帮我看一下,如何以文本为条件查找数据啊。
调试欢乐多
要把文本用'括起来
If optbh.Value = True And txtbh.Text <> "" Then '按编号查找是可以的。
sqlstr = "select * from laogai where gbjdq=true and gbh=" & txtbh.Text
ElseIf optxm.Value = True And txtxm.Text <> "" Then '按姓名查找不可以。
sqlstr = "select * from laogai where gbjdq=true and gxm='" & txtxm.Text & "'"
end if
//调试看看判断条件满足吗?
sqlstr="select * from laogai where gbjdq=true and gxm=杨洋"这个查找语句有问题吗?
就对了字符串两边都加上 ' 号
Select * From laogai
Where gbjdq=true And gxm='"& Trim(txtxm.Text) &"'"
cSql = "select * from students where state=true and " _
& IIf(Trim(txtnumber.Text) <> "", "numbers= " + Trim(txtnumber.Text), " 0=0 ")
cSql = "select * from students where state=true and " _
& IIf(Trim(txtname.Text) <> "", " names= '" + Trim(txtname.Text) + "'", " 0=0 ")
sqlstr="select * from students where [state]=true and [names]='" & txtname.text & "'"
不过我是这样写的:
sqlstr="select * from students where [state]=true" and "[names]='" & txtname.text & "'""
楼主你试试看!