1

我正在针对 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

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

4

1 回答 1

0

这是我用来从 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。

于 2012-04-05T21:41:53.457 回答