VB + ADODB 查询得不到记录集

jlpdgy004 2015-10-09 06:31:15


public sub Click()
dim strSQL = "
Declare @NameList varchar(255)
Set @NameList='''测试员'''
Exec( 'Select * from BM_NestManage_Role Where RoleName in (' + @NameList + ') and LogoutFlag = 0')"

Dim ccn As ADODB.Connection
Set ccn = DB.OpenDB(DB_BM.DB_CONN)

Dim rs As ADODB.Recordset
'---------------------------------------------------------
' 得不到
Set rs = DB.QueryTable(strSQL, cn)
' -----------------------------------------------------

If rs Is Nothing Then
Exit Function
End If

If rs.RecordCount = 0 Then
Call DB.CloseQuery(rs)
Exit Function
End If
end sub

Public Function QueryTable(strSQL As String, CurCn As ADODB.Connection) As ADODB.Recordset
On Error Resume Next
Set QueryTable = Nothing

Set rs = New ADODB.Recordset
rs.Open strSQL, CurCn, adOpenKeyset, adLockReadOnly

If Err.Number <> 0 Then

MsgBox "不能查询数据库!" + Chr(10) + Chr(13) + Err.Description
Err.Clear
Debug.Print strSQL
Set rs = Nothing
Exit Function

End If

'如果记录集处于关闭状态
If rs.State = 0 Then
Set rs = Nothing
End If

Set QueryTable = rs
Debug.Print strSQL

End Function




以上QueryTable 查询如: "Select * from table Where id =1" 时没有问题,可以获得记录集
而如果字符串中有变量时,则rs.State会等于0,且不报错,获得不了记录集

测试语句:"Declare @ID int Set @ID=1 Select * from table Where Id=@ID"
...全文
153 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
of123 2015-10-10
  • 打赏
  • 举报
回复
Jet Engine 不支持有变量的 SQL 语句。 既然在字符串中固定设置 @ID=1,何不直接设置 Select * from table Where id =1? 如果使用程序中的变量,可以 "Select * from table Where id =" & ID。
jlpdgy004 2015-10-10
  • 打赏
  • 举报
回复
没有人出现过这样的问题?
jlpdgy004 2015-10-10
  • 打赏
  • 举报
回复
受教了 。 是我钻牛角尖了

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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