问题
I am strugling with some unwanted metadata requests automatically generated by ADO. The behavior started after replacing the default MSDASQL provider with SQLOLEDB. I have also tried the SQLOLEDB and the behavior seems to be the same.
Reproducing the problem from the IDE has proved to be difficult, because it seems to be happening kinda random. Whats even more confising is that these queries seems to request metadata for previously opened (and then closed) recordsets.
回答1:
I have invastigated some strange behavior related to the unwanted NO_BROWSETABLE / fmtonly / 1=2 queries using ADO 2.8 in VB6 SP6 on a XP machine.
The following code will reproduce the behavior.
Private Sub Form_Load()
Dim oConn As ADODB.Connection
Set oConn = New ADODB.Connection
'oConn.Open "DRIVER=SQL Server;SERVER=***;UID=***;password=***;APP=***"
'oConn.Open "Provider=SQLOLEDB;SERVER=***;UID=***;password=***;APP=***"
'oConn.Open "Provider=SQLNCLI10;Server=***;UID=***;PWD=***;APP=***"
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
' Execute the first query
rst.Open "select 1", oConn
rst.Close
' Execute the second query without specifying a connection
rst.Open "select 2"
End Sub
I have tested the code above using three different providers. Here are the SQL Server Profiler results.
Provider 1 (MSDASQL)
select 1
go
select 2
go
Provider 2 (SQLOLEDB)
select 1
go
-- This comes after executing the second query. Notice the query between FMTONLY ON/OFF is the FIRST query.
SET NO_BROWSETABLE ON
go
SET FMTONLY ON select 1 SET FMTONLY OFF
go
SET NO_BROWSETABLE OFF
go
select 2
go
Provider 3 (SQLNCLI10)
select 1
go
-- This comes after executing the second query. The behavior is the same as for SQLOLEDB, except that 'where 1=2' is appended to the query.
SET NO_BROWSETABLE ON
go
set fmtonly on select 1 where 1=2 set fmtonly off
go
SET NO_BROWSETABLE OFF
go
select 2
go
Conclusion:
If you don't specify a connection in the second rst.Open then ADO will request the server for metadata regarding the PREVIOUS query before continuing.
rst.Open "select 2", oConn
Specifying oConn as above will eliminate the metadata request and the SQL Server Profiler will yield the same events for all three providers.
来源:https://stackoverflow.com/questions/29430665/strange-ado-behavior-generating-unwanted-no-browsetable-set-fmtonly-queries-in