特定查询导致ADO Recordset因E_FAIL而失败

时间:2012-05-09 19:45:04

标签: sql-server-2005 ado recordset

我正在尝试使用ADO对SQL Server 2005运行(两个)查询:

SELECT
   sp.nt_username AS NTUsername,
   sp.hostname AS HostName
FROM sys.dm_tran_locks tl
   INNER JOIN master.dbo.sysprocesses sp
   ON tl.request_session_id = sp.spid
WHERE tl.resource_type = N'APPLICATION'
AND tl.resource_database_id = (
       SELECT dbid
       FROM master.dbo.sysprocesses
       WHERE spid = @@spid)

和SQL Server 2000兼容版本:

SELECT
   sp.nt_username AS NTUsername,
   sp.hostname AS HostName
FROM master.dbo.syslockinfo sli
   INNER JOIN master.dbo.sysprocesses sp
   ON sli.req_spid = sp.spid
WHERE rsc_type = 10 --10=Appliction
AND rsc_dbid = (
   SELECT dbid
   FROM master.dbo.sysprocesses
   WHERE spid = @@spid)

查询执行罚款:

IRecordset rs =new Recordset();
rs.CursorLocation = adUseClient; //the default for a Recordset is adUseServer (Connection.Execute's default is adUseClient)
rs.CursorType := adOpenForwardOnly; //the default
rs.Open(szQuery, conn.ConnectionObject,
       adOpenForwardOnly, //CursorType
       adLockReadOnly, //LockType
       adCmdText);

然后 检查 状态BOFEOF

if (rs.EOF) then

触发异常:

  • HRESULT:0x80004005(未指定错误)
  • 错误消息:“数据提供者或其他服务返回了E_FAIL状态”

导致ADO失败的查询是什么?


注意:确实 匹配的行:

enter image description here

我在连接字符串中使用的providerProvider=SQLOLEDB

Bonus Chatter:

  

的sysprocesses

  nt_username nchar(128)
  hostname    nchar(128)

并查询两者 - 但一次一个 - 有效,例如:

SELECT
   sp.nt_username AS NTUsername

SELECT
   sp.hostname AS HostName

的工作。查询两者都失败了。

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。 It's a known issue with SQL Server 2005 and ADO

即使nt_usernamehostname已经 nchar(128),您仍需要手动将其转换为nchar(128)

SELECT
   CAST(sp.nt_username AS nchar(128)) AS NTUsername,
   CAST(sp.hostname AS nchar(128)) AS HostName
FROM sys.dm_tran_locks tl
   INNER JOIN master.dbo.sysprocesses sp
   ON tl.request_session_id = sp.spid
WHERE tl.resource_type = N'APPLICATION'
AND tl.resource_database_id = (
       SELECT dbid
       FROM master.dbo.sysprocesses
       WHERE spid = @@spid)

SQL Server 2005和ADO也存在已知错误,并且在查询中使用@@spid;你做不了。幸运的是,我可以使用DB_ID()

SELECT
   CAST(sp.nt_username AS nchar(128)) AS NTUsername,
   CAST(sp.hostname AS nchar(128)) AS HostName
FROM sys.dm_tran_locks tl
   INNER JOIN master.dbo.sysprocesses sp
   ON tl.request_session_id = sp.spid
WHERE tl.resource_type = N'APPLICATION'
AND tl.resource_database_id = DB_ID()

八达冰。

SQL Server 2005错误。

它可能仍然存在于SQL Server 2008,SQL Server 2008 R2,SQL Server 2012