OleDB:无法绑定DBTYPE_WSTR参数 - 得到DB_E_UNSUPPORTEDCONVERSION错误

时间:2011-06-26 08:27:30

标签: delphi api oledb parameter-passing

以下是OpenSource库的上下文:

  • 我直接从非托管代码(Delphi)调用OleDB库;
  • 我通过创建IAccessorICommandText OleDB实例来绑定参数;
  • 我对DBTYPE_I8DBTYPE_DATE;
  • 等简单类型没有任何问题
  • 我想将文本参数绑定为DBTYPE_WSTR类型,即始终为Unicode。

问题是,对于DBTYPE_WSTR类型,我在调用Command.Execute时收到OLEDB错误80040E1D(DB_E_UNSUPPORTEDCONVERSION)错误,“不支持请求转换”。

当然,我尝试使用或不使用DBTYPE_BYREF数据布局(即在下面的代码中设置FIELDTYPE2OLEDB[ftUTF8]=DBTYPE_WSTR or DBTYPE_BYREF,并更改布局):同样的问题......它适用于{{1}但不是DBTYPE_WSTR

但是如果我将参数类型从DBTYPE_STR更改为DBTYPE_WSTR(即在下面的代码中设置DBTYPE_STR),则该命令执行没有任何问题。但是我想使用FIELDTYPE2OLEDB[ftUTF8]=DBTYPE_STR or DBTYPE_BYREF wType代替,以确保因为当前的Ansi Charset而没有丢失任何字符。

事实上,我可以DBTYPE_WSTR检索任何IRowSet数据,但我无法将参数值绑定到DBTYPE_WSTR

我已连接到Microsoft SQL Server 2008 R2实例。代码在SQL请求中没有参数,或者使用int或浮点参数,但不使用TEXT参数。

以下是主要示例代码:

DBTYPE_WSTR

Query.Execute('select * from Person.Address where AddressLine1 like ?;',true,['% Drive']); 列在 AdventureWorks2008R2 参考示例数据库中定义为AddressLine1,因此映射到nvarchar(60),根据到official MSDN documentation

相应单位的完整源代码可从our source code repository获得。代码在DBTYPE_WSTR方法中,如下:

TOleDBStatement.Execute

因此我的问题是:

如何在const PARAMTYPE2OLEDB: array[TSQLDBParamInOutType] of DBPARAMIO = ( DBPARAMIO_INPUT, DBPARAMIO_OUTPUT, DBPARAMIO_INPUT or DBPARAMIO_OUTPUT); FIELDTYPE2OLEDB: array[TSQLDBFieldType] of DBTYPE = ( DBTYPE_EMPTY, DBTYPE_NULL, DBTYPE_I8, DBTYPE_R8, DBTYPE_CY, DBTYPE_DATE, DBTYPE_WSTR or DBTYPE_BYREF, DBTYPE_BYTES or DBTYPE_BYREF); (...) OleDBCheck((fSession as IDBCreateCommand). CreateCommand(nil,IID_ICommandText,ICommand(fCommand))); fCommand.SetCommandText(DBGUID_DEFAULT,pointer(Utf8DecodeToRawUnicodeUI(aSQL))); P := pointer(fParams); SetLength(fParamBindings,fParamCount); B := pointer(fParamBindings); for i := 1 to fParamCount do begin B^.iOrdinal := i; // parameter index (starting at 1) B^.eParamIO := PARAMTYPE2OLEDB[P^.VInOut]; // parameter direction B^.wType := FIELDTYPE2OLEDB[P^.VType]; // parameter data type // set additional fields case P^.VType of ftInt64, ftDouble, ftCurrency, ftDate: begin // those types match the VInt64 binary representation :) B^.cbMaxLen := sizeof(Int64); B^.dwPart := DBPART_VALUE; B^.obValue := PAnsiChar(@P^.VInt64)-pointer(fParams); end; ftUTF8, ftBlob: begin // sent as DBTYPE_BYREF mapping directly the VRawByteString content B^.dwPart := DBPART_VALUE or DBPART_LENGTH or DBPART_STATUS; B^.obValue := PAnsiChar(@P^.VRawByteString)-pointer(fParams); B^.cbMaxLen := sizeof(Pointer); Len := length(P^.VRawByteString); if P^.VType=ftUTF8 then Len := Len shr 1; // expect length in WideChar count, excluding #0 P^.VInt64 := Len; // store length in unused VInt64 property B^.obLength := PAnsiChar(@P^.VInt64)-pointer(fParams); B^.obStatus := B^.obLength+4; end; end; inc(P); inc(B); end; OleDBConnection.OleDBCheck((fCommand as IAccessor).CreateAccessor( DBACCESSOR_PARAMETERDATA,fParamCount,Pointer(fParamBindings),0, fDBParams.HACCESSOR,nil)); OleDBConnection.OleDBCheck(fCommand.Execute( nil,IID_IRowset,fDBParams,nil,@fRowSet),ParamsStatus); 中没有DB_E_UNSUPPORTEDCONVERSION错误的情况下绑定B^.wType=DBTYPE_WSTR列的nvarchar()参数。

我怀疑有一些属性设置为fCommand.ExecuteISession实例,或者设置了一些标志/选项,但我无法从MSDN文档中找到哪一个。欢迎任何帮助!

1 个答案:

答案 0 :(得分:1)

好的......经过几个小时后,我想我已经找到了。

问题不在于我的代码AFAIK,而是来自Microsoft的Microsoft SQL Server OleDB提供程序。正如官方文档所述,使用DBTYPE_WSTR只是不起作用。我不是说MS写了错误,但我真的是confused by the documentation,它在同一行清楚地说明:nvarchar DBTYPE_WSTR

我最后尝试使用DBTYPE_BSTR WideString,使用Ole DBTYPE_BSTR ...并按预期工作!

我只是猜测微软和大多数OleDB消费者只使用 ftUTF8: begin // mapping directly the WideString VText content B^.wType := DBTYPE_BSTR; // DBTYPE_WSTR just doesn't work :( B^.obValue := PAnsiChar(@P^.VText)-pointer(fParams); B^.cbMaxLen := sizeof(Pointer); end;

以下是some working code

WideString

{{1}}的好处是,如果输出或输入/输出参数(在调用存储过程时),提供程序可以调整它的大小。

相关问题