在获取“数据对于变量而言太大”时,如何正确访问FireDAC数据集的VARCHAR(MAX)参数值。错误? [英] How to properly access a VARCHAR(MAX) parameter value of a FireDAC dataset when getting "data too large for variable" error?

查看:428
本文介绍了在获取“数据对于变量而言太大”时,如何正确访问FireDAC数据集的VARCHAR(MAX)参数值。错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的应用程序使用SQL Server 2014更新和访问数据。

Our application updates and accesses data using SQL Server 2014.

我有一个表,其中最后一列(内容)创建为 VARCHAR(MAX)

I have a table of which the last column ('Contents') is created as VARCHAR(MAX).

我们正在使用Delphi XE8,并且正在使用FireDAC TFDQuery 组件以更新此列。

We are using Delphi XE8, and are using a FireDAC TFDQuery component to update this column.

.....
FDquery.ParamByName('Contents').AsString:=Contents;
FDquery.ExecSQL;

运行此更新时,出现以下错误:

When running this update, I get the following error:


消息[FireDAC] [Phys] [ODBC] -345引发异常。数据太大,无法使用变量[CONTENTS]。最大len = [8002],实际len = [13829]提示:将TFDParam.Size设置为更大的值。

Exception raised with message [FireDAC][Phys][ODBC]-345. Data too large for variable [CONTENTS]. Max len = [8002], actual len = [13829] Hint: set the TFDParam.Size to a greater value.

'Contents '可以是长度可变的字符串。

'Contents' can be string of varying length.

浏览网络时,我发现的唯一合理的简单解决方案是按如下方式更改查询:

Browsing the web, the only reasonably simple solution that I found is changing the query as follows:

 FDquery.ParamByName('Contents').AsWideMemo:=Contents;
 FDquery.ExecSQL;     

这是可以接受的,还是应该以不同的方式处理?

Is this acceptable, or should I be handling this differently?

内容没有什么特别的,正如我提到的那样,它只是一个长字符串。

There is nothing fancy with 'Contents', as I mentioned, it is simply a long string.

推荐答案

是否可以通过AsWideMemo属性访问VARCHAR(MAX)类型的字段参数值?



不是特别如此。对于 VARCHAR(MAX) 字段参数使用 AsMemo 访问权限。这是因为您可能会将非Unicode字段的Unicode值发送到DBMS。来自 参考

Is it acceptable to access a VARCHAR(MAX) type field parameter value by the AsWideMemo property?

Not especially. For VARCHAR(MAX) field parameter use AsMemo access. It is because you could be sending to your DBMS Unicode values to a non Unicode field. From the reference:


将Unicode编码的参数值转换为DBMS支持的Unicode
字符集,并发送给DBMS。
这不取决于客户端字符集或Delphi版本。

The Unicode encoded parameter value is converted to a Unicode character set, which is supported by the DBMS, and sent to the DBMS. This does not depend on a client character set or on a Delphi version.

如果您的字段为 NVARCHAR(MAX) ,使用 AsWideMemo 访问参数值将是正确的选择。

If your field would be NVARCHAR(MAX), using AsWideMemo access to the parameter value would be the right choice.

为什么会发生这种情况?通过 As< T 访问特定参数值strong> 属性,引擎还会设置参数 DataType (如果您之前未明确进行过此操作)。在这种特殊情况下,您暗示引擎如果将参数数据类型设置为 ftWideString ftString ,只需通过 AsString 属性。

Some background to why this happens. By accessing a certain parameter value by As<T> property, the engine also sets the parameter DataType, if you don't explicitly do that before. In this particular case you hinted the engine that it's fine for you if it sets the parameter data type to ftWideString or ftString just by accessing parameter value by the AsString property.

由于数据类型映射,该参数被视为> VARCHAR [n] > NVARCHAR [n] 数据类型,包括其限制(因此

And thanks to data type mapping such parameter is treated as the VARCHAR[n] or NVARCHAR[n] data type including its limits (hence you got a string length limit error here).

类似,当您通过 AsMemo 属性,默认为 ftMemo 数据类型,它映射到> VARCHAR(MAX) > 。如您所料,
AsWideMemo 访问权限默认为 ftWideMemo > 映射到 NVARCHAR(MAX) 数据类型。如果您不想显式设置参数数据类型,而是使用此提示,请查阅手册,以了解每个使用的访问属性如何设置默认参数数据类型。

Similar, just more specific data type hint is used when you access the parameter value by AsMemo property, it defaults to ftMemo data type which maps to VARCHAR(MAX). And as you may predict, AsWideMemo access defaults to ftWideMemo which maps to NVARCHAR(MAX) data type. If you don't want to explicitly set the parameter data types but use this hinting, consult the manual to see how each used access property sets the default parameter data type.

这篇关于在获取“数据对于变量而言太大”时,如何正确访问FireDAC数据集的VARCHAR(MAX)参数值。错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆