2013-03-14 91 views
0

我想不通爲什麼會這樣......ADODB .NET互操作光標

我有以下代碼:

 var connection = new Connection(); 
     connection.CursorLocation = CursorLocationEnum.adUseClient; 
     connection.ConnectionString = GetOdbcConnectionString(connectionString); 
     connection.Open(); 

     var rs = new Recordset(); 
     rs.CursorType = CursorTypeEnum.adOpenStatic; 
     rs.CursorLocation = CursorLocationEnum.adUseClient; 
     rs.LockType = LockTypeEnum.adLockBatchOptimistic; 
     // this issues the SQL SELECT * FROM Test 
     rs.Open("SELECT * FROM Test", connection); 
     rs.ActiveConnection = null; 
     rs.Close(); 
     rs = null; 

     var rs2 = new Recordset(); 
     rs2.CursorType = CursorTypeEnum.adOpenStatic; 
     rs2.CursorLocation = CursorLocationEnum.adUseClient; 
     rs2.LockType = LockTypeEnum.adLockBatchOptimistic; 

     /* this doesn't output the expected SQL...it outputs: 
declare @p1 int 
set @p1=180150003 
declare @p3 int 
set @p3=4 
declare @p4 int 
set @p4=1 
declare @p5 int 
set @p5=-1 
exec sp_cursoropen @p1 output,N'SELECT * FROM Test',@p3 output,@p4 output,@p5 output 
select @p1, @p3, @p4, @p5 

*/ 

     rs2.Open("SELECT * FROM Test", connection); 

如果我手動Marshal.ReleaseComObject(rs)打開RS2之前,第二個記錄問題與第一個相同的簡單SQL,不使用遊標。一些東西必須掛在它從第一個rs ....但我不知道是什麼,爲什麼或爲什麼它導致使用遊標,當連接和記錄集都設置了OpenStatic和UseClient。

回答

0

敲我的頭這一段時間後,終於發現了問題:

DRIVER={SQL Server}; 

需要改變,以

Provider=SQLOLEDB;