ADO无法使用本机客户端(SQLNCLI)访问SQL Server XML类型列

我正在使用ADO和一个“本机”驱动程序(例如sqlNCLI,sqlNCLI10,sqlNCLI11)连接到sql Server(而不是旧的sqlOLEDB驱动程序).

ADO不了解本机驱动程序公开的XML sql Server数据类型:

field: ADOField;

field := recordset.Fields.Items["SomeXmlColumn"];

尝试访问field.Value会抛出EOleException:

>来源:Microsoft Cursor Engine
> ErrorCode:0x80040E21(E_ITF_0E21)
>消息:多步操作生成错误.检查每个状态值

本机客户端驱动程序(例如sqlNCLI,sqlNCLI11)将ADO的Xml数据类型呈现为

field.Type_ = 141 //???

旧版sqlOLEDB驱动程序将ADO的Xml数据类型显示adLongVarWChar,即unicode字符串:

field.Type_ = 203 //adLongVarWChar

字段中包含的VARIANT值.值是WideString (technically known as a BSTR)

tvarData(field.Value).vtype = 8 //VT_BSTR

在我看来,这是ADO(Windows 7 SP1)中的一个错误,而不是我可以解决的问题.

我该如何解决

奖金阅读

> Delphi with SQL Server: OLEDB vs. Native Client drivers

解决方法

MSDN documentation

To enable ADO to use new features of recent versions of sql Server,
some enhancements have been made to the sql Server Native Client OLE
DB provider which extends the core features of OLE DB. These
enhancements allow ADO applications to use newer sql Server features
and to consume two data types introduced in sql Server 2005: xml and
udt. These enhancements also exploit enhancements to the varchar,
nvarchar,and varbinary data types. sql Server Native Client adds the
sspROP_INIT_DATATYPECOMPATIBILITY initialization property to the
DBPROPSET_sqlSERVERDBINIT property set for use by ADO applications so
that the new data types are exposed in a way compatible with ADO. In
addition,the sql Server Native Client OLE DB provider also defines a
new connection string keyword named DataTypeCompatibility that is set
in the connection string.

因此,要在Native Client中启用新的sql Server功能,您需要在连接字符串中添加以下关键字:

Provider=sqlNCLI11
DataTypeCompatibility=80

其中DataTypeCompatibility

Specifies the mode of data type handling to use. Recognized values are “0” for provider data types and “80” for sql Server 2000 data types.

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...