gpt4 book ai didi

mysql - VB6 ADO 断开连接的记录集不返回任何记录

转载 作者:行者123 更新时间:2023-11-30 23:32:09 25 4
gpt4 key购买 nike

我正在针对 MySQL 数据库创建、打开然后断开记录集。这对一个查询有效,但对数据库中存在行的另一查询返回 0 条记录。在有效的地方,我也可以从记录集中删除记录。

返回记录的查询:

sql = "select convert(v.wonotes using UTF8) as WonData, v.wo_cat_id, v.id As wo_desc_id, v.line_no as line_no, " & _
" v.wo_id as wo_id, v.prop_id as prop_id, convert(v.description using UTF8) as description, v.cat_id as cat_id, " & _
" v.completion_date as completion_date from vw_property_wo_desc v " & _
" where v.wo_cat_id= 6 and **(v.wo_status = 'completed' or v.wo_status = 'approved')** " & _
" and v.wonotes is not null and v.wonotes<> '' "

不返回记录的查询:

sql = "select convert(v.wonotes using UTF8) as WonData, v.wo_cat_id, v.id As wo_desc_id, v.line_no as line_no, " & _
" v.wo_id as wo_id, v.prop_id as prop_id, convert(v.description using UTF8) as description, v.cat_id as cat_id, " & _
" v.completion_date as completion_date from vw_property_wo_desc v " & _
" where v.wo_cat_id= 6 and **v.wo_status = 'unassigned'** " & _
" and v.wonotes is not null and v.wonotes<> '' "

没有其他变化。

如果我将游标类型更改为 adOpenDynamic,查询将返回记录,但我无法断开连接。这只是为了证明数据库中有满足该查询的记录。

断开连接记录集的代码:

With rsToUse

If .State = adStateOpen Then .Close

.ActiveConnection = GetConnection
.Source = sql
.CursorLocation = adUseClient
.CursorType = adOpenForwardOnly
.LockType = adLockBatchOptimistic
.Open

If .EOF Then
.Close
Exit Function
End If

.ActiveConnection = Nothing

End With

我的想法用完了,请帮忙。

最佳答案

这是我用来从 SQL Server 数据库中获取断开连接的记录集的代码。我怀疑它也适用于 MySQL 数据库(当然,连接字符串除外)。

Public Function GetRecordset(ByVal SQL As String) As ADODB.Recordset

Dim DB As ADODB.Connection
Dim RS As ADODB.Recordset

Set DB = CreateObject("ADODB.Connection")
DB.ConnectionString = globalConnectionString
DB.CursorLocation = adUseClient
DB.CommandTimeout = 0
Call DB.Open

Set RS = CreateObject("ADODB.Recordset")
RS.CursorLocation = adUseClient
Call RS.Open(SQL, DB, adOpenForwardOnly, adLockReadOnly)
Set RS.ActiveConnection = Nothing
Set GetRecordset = RS
Set RS = Nothing
DB.Close
Set DB = Nothing

End Function

这个确切的代码已经投入生产至少 5 年了,没有任何问题。我鼓励您尝试一下。

我认为使用断开连接的记录集的神奇组合是确保连接对象的 CursorLocation 设置为 UseClient,并且记录集对象为 ForwardOnly 和 LockReadOnly。

关于mysql - VB6 ADO 断开连接的记录集不返回任何记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10032268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com