vb6.0 查询问题 为什么查询不出数据,留一条sql语句就能查出来,加上另外7条就查询不出来数据,为什么?
PrivateSubCommand1_Click()OnErrorGoTomerrorconn.CloseDimsqlAsStringDimconnStrAsString...
Private Sub Command1_Click()
On Error GoTo merror
conn.Close
Dim sql As String
Dim connStr As String
connStr = s
sql = "select * from chumenzheng where f01 like '%" & Text12.Text & "%'"
sql = "select * from chumenzheng where f02 like '%" & Text13.Text & "%'"
sql = "select * from chumenzheng where f03 like '%" & Text14.Text & "%'"
sql = "select * from chumenzheng where f04 like '%" & Text15.Text & "%'"
sql = "select * from chumenzheng where f06 like '%" & Text16.Text & "%'"
sql = "select * from chumenzheng where f08 like '%" & Text17.Text & "%'"
sql = "select * from chumenzheng where f09 like '%" & Text18.Text & "%'"
sql = "select * from chumenzheng where f10 like '%" & Text1.Text & "%'"
conn.Open strConn
conn.CursorLocation = adUseClient
rs.Open sql, conn, adOpenStatic, adLockBatchOptimistic
Set DataGrid1.DataSource = rs
DataGrid1.Refresh 展开
On Error GoTo merror
conn.Close
Dim sql As String
Dim connStr As String
connStr = s
sql = "select * from chumenzheng where f01 like '%" & Text12.Text & "%'"
sql = "select * from chumenzheng where f02 like '%" & Text13.Text & "%'"
sql = "select * from chumenzheng where f03 like '%" & Text14.Text & "%'"
sql = "select * from chumenzheng where f04 like '%" & Text15.Text & "%'"
sql = "select * from chumenzheng where f06 like '%" & Text16.Text & "%'"
sql = "select * from chumenzheng where f08 like '%" & Text17.Text & "%'"
sql = "select * from chumenzheng where f09 like '%" & Text18.Text & "%'"
sql = "select * from chumenzheng where f10 like '%" & Text1.Text & "%'"
conn.Open strConn
conn.CursorLocation = adUseClient
rs.Open sql, conn, adOpenStatic, adLockBatchOptimistic
Set DataGrid1.DataSource = rs
DataGrid1.Refresh 展开
4个回答
展开全部
楼主你好
你的sql是一个变量
当你再次为他赋值时,值就发生了改变
也就是说:当你执行的时候,sql的值是最后一次赋值的结果
如果你想得到8个执行语句合并的结果,将语句改成:
sql = "select * from chumenzheng where f01 like '%" & Text12.Text & "%' or f02 like '%" & Text13.Text & "%' or f03 like '%" & Text14.Text & "%'....................."
.............是以次类推,自己改下
你的sql是一个变量
当你再次为他赋值时,值就发生了改变
也就是说:当你执行的时候,sql的值是最后一次赋值的结果
如果你想得到8个执行语句合并的结果,将语句改成:
sql = "select * from chumenzheng where f01 like '%" & Text12.Text & "%' or f02 like '%" & Text13.Text & "%' or f03 like '%" & Text14.Text & "%'....................."
.............是以次类推,自己改下
更多追问追答
追问
就这一种解法吗,还想问一下f04 like '%" & Text15.Text & "%'"这一句改成数字型的怎么改,还有你写的语句我查询1的时候12也查询出来了,我要精确查找,谢了,答对者追加20分
追答
知道你的意思了
可以改成 convert(int,[f04])=convert(int,'" & Text15.Text & "')
函数convert(数据类型,字段名):将某个数据转换成另外一种数据类型
如:
select convert(int,'10000')+convert(int,'1000')
查询结果是11000而不是100001000
其实以上是正规写法,牵涉到数据运算的
而我看下你的例子里不牵涉到数据运算,可以直接改成:
f04 = '" & Text15.Text & "'
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
'%" & Text12.Text & "%'"
应该是这里写错了, ”%" & Text12.Text & "%“"试试
应该是这里写错了, ”%" & Text12.Text & "%“"试试
更多追问追答
追问
哥们不是,还是不好使。是不是我记录集写的有问题啊,那位高人能帮帮我谢啦,如果答对加分
追答
不太明白你的意思,加上七条后,应该也只能查到最后一条,你是想查8条语句吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这代码写的太N了!!楼主应该优化下啊。。。。。
更多追问追答
追问
真么忽略啊,帮忙改改
追答
因为你没有在问题里面描述你的需求,那我只能按我的理解,说明下
只从代码上看,你最上面的代码,其实只返回了
sql = "select * from chumenzheng where f10 like '%" & Text1.Text & "%'"
的结果,没有查询到结果,是因为数据库里没有F10=text1.text数据
如果你是要按以上8条SQL查询,然后将结果汇总出来的话,你可以试着将,以下部分:
sql = "select * from chumenzheng where f01 like '%" & Text12.Text & "%'"
sql = "select * from chumenzheng where f02 like '%" & Text13.Text & "%'"
sql = "select * from chumenzheng where f03 like '%" & Text14.Text & "%'"
sql = "select * from chumenzheng where f04 like '%" & Text15.Text & "%'"
sql = "select * from chumenzheng where f06 like '%" & Text16.Text & "%'"
sql = "select * from chumenzheng where f08 like '%" & Text17.Text & "%'"
sql = "select * from chumenzheng where f09 like '%" & Text18.Text & "%'"
sql = "select * from chumenzheng where f10 like '%" & Text1.Text & "%'"
'==================================================================
'改为
sql = "select * from chumenzheng where f01 like '%" & Text12.Text & "%'"
sql = sql+" Union All select * from chumenzheng where f02 like '%" & Text13.Text & "%'"
sql = sql+" Union All select * from chumenzheng where f03 like '%" & Text14.Text & "%'"
sql = sql+" Union All select * from chumenzheng where f04 like '%" & Text15.Text & "%'"
sql = sql+" Union All select * from chumenzheng where f06 like '%" & Text16.Text & "%'"
sql = sql+" Union All select * from chumenzheng where f08 like '%" & Text17.Text & "%'"
sql = sql+" Union All select * from chumenzheng where f09 like '%" & Text18.Text & "%'"
sql = sql+" Union All select * from chumenzheng where f10 like '%" & Text1.Text & "%'"
希望能帮到你
其实这样写,从实际情况走的话,还是会有问题,我不知道你的程序是怎么设计的,这样写会有个问题,你必须每个TEXT都必须要填值,不然就会将所有的数据查询出来,想要避免这种情况,就需要判断了。不知道你有没有这样的问题。
其实你需要把这段sql好好的规划下,写不下了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询