我不明白为什么会这样……
我有以下代码:
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 时,它是什么、为什么或为什么会导致使用游标。