2015-04-03 42 views
0

我正在琢磨一些由ADO自動生成的不需要的元數據請求。該行爲在用SQLOLEDB替換默認MSDASQL提供程序後開始。我也嘗試過SQLOLEDB,行爲似乎是一樣的。奇怪的ADO行爲在VB6中生成不需要的NO_BROWSETABLE /設置fmtonly查詢

從IDE中重現問題已被證明是困難的,因爲它似乎是隨機發生的。更令人置疑的是,這些查詢似乎要求先前打開(然後關閉)記錄集的元數據。

回答

1

我在XP機器上使用VB6 SP6中的ADO 2.8處理了一些與不需要的NO_BROWSETABLE/fmtonly/1 = 2查詢相關的奇怪行爲。

以下代碼將重現該行爲。

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 

我已經使用三個不同的提供者測試了上面的代碼。這裏是SQL Server Profiler結果。

提供者1(MSDASQL)

select 1 
go 
select 2 
go 

提供商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 

提供者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 

結論:

如果你不指定一個連接然後打開,ADO將在繼續之前向服務器請求關於PREVIOUS查詢的元數據。

rst.Open "select 2", oConn 

如上所述指定oConn將消除元數據請求,並且SQL Server Profiler將爲所有三個提供程序產生相同的事件。