Microsoft KB Archive/240401

= BUG: Repeatedly Opening and Closing DAO Recordset Increases Connections on SQL Server =

Article ID: 240401

Article Last Modified on 6/19/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q240401



SYMPTOMS
The number of connections opened on SQL Server increases indefinitely if a Microsoft Data Access Objects (DAO) recordset is opened and closed repeatedly when you use DAO version 3.6. This does not happen if you use DAO version 3.51. You may see this behavior if you use sqlpassthrough in which the connection string is specified in the OpenDatabase statement.



RESOLUTION
Use a Querydef to create the connection instead of creating the connection in the OpenDatabase statement.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

This bug was corrected in Microsoft Jet 4.0 Service Pack 4. For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

239114 How to obtain the latest service pack for the Microsoft Jet 4.0 Database Engine



Steps to Reproduce Behavior
 Open a new Standard EXE project in Visual Basic. Form1 is created by default. From the Project menu, choose References and then select Microsoft DAO 3.6 Data Objects.  Place two command buttons, named Command1 and Command2, on the form, and then paste the following code in form code window.

Note You must change User ID  and password  to the correct values before you run this code. Make sure that User ID has the appropriate permissions to perform this operation on the database. Dim db As Database Dim rs As Recordset Dim qd As QueryDef

Private Sub Command1_Click

'-- Insert your own connect string here. Set db = OpenDatabase("", dbDriverNoPrompt, False, _         "ODBC;DSN=sql7;UID=;PWD=;DATABASE=pubs;") Dim i As integer For i = 1 To 5 ' reptq1 exists in the pubs database Set rs = db.OpenRecordset("reptq1", _            dbOpenSnapshot, dbSQLPassThrough) rs.Close Next i End Sub

Private Sub command2_Click

'Change the following string to match your path to the .mdb. Set db = OpenDatabase("d:\vbtest\db1.mdb") on error resume next db.QueryDefs.Delete "mysql7" on error goto 0 Set qd = db.CreateQueryDef("mysql7") '-- Insert your own connect string below. With qd    .Connect = "odbc;database=pubs;uid=;pwd=<Strong Password>;dsn=sql7" .SQL = "select * from authors" .ReturnsRecords = True End With Dim i As Integer For i = 1 To 20 Set rs = qd.OpenRecordset(dbOpenDynaset) rs.Close Next i End Sub

</li> Open ISQL/W in SQL Server 6.5 or Query Analyzer in SQL Server 7.0 and run the sp_who stored procedure. Note the number of connections.</li> Run the form created in step 1 and click Command1. Note the number of connections by executing sp_who again. Repeat this process a few times and note that the number of connections continues to increase. If you get a connection failure you have probably reached your connection limit.</li> Next, click Command2 repeatedly and note the number of connections returned by sp_who.</li></ol>

<div class="references_section">