无法将数据分配给客户端数据集 [英] Cannot assign data to client dataset
问题描述
我在我的Delphi应用程序中使用分配了本地数据的TClientDataSet
来比较位于2个不同数据库中的2个表之间的数据.我正在使用的东西:
I'm using TClientDataSet
assigned with local data in my Delphi application to compare data between 2 tables located in 2 different databases. Things I'm using:
-
SpPlansQuery: TADOQuery
–原始数据查询 -
PPUQuery: TADOQuery
–更改了数据查询(使用与SpPlansQuery不同的ADO连接) -
ComparisonDataSet: TClientDataSet
–仅显示两个先前查询之间的差异的数据集
SpPlansQuery: TADOQuery
– original data queryPPUQuery: TADOQuery
– changed data query (uses different ADO connection than SpPlansQuery)ComparisonDataSet: TClientDataSet
– dataset showing only differences between 2 previous queries
我正在尝试按以下方式填充ComparisonDataSet
(我大量减少了代码以突出显示问题):
I'm trying to fill ComparisonDataSet
as follows (I heavily reduced my code to highlight problem):
procedure TComparisonSpPlanForm.RefillDataSet;
const
// IMPORTANT: check that fields count in next 2 lines would be the same
FieldsStr1 = 'Article_PPU;Contractor_S_PPU;Recipient_S_PPU;OrderNum_PPU;OrderNum2_PPU;OrdN_PPU;Title_PPU;Queue_PPU;KolSht_PPU;Weight1_PPU;Material_PPU;Drawing_PPU;Graph_PPU';
FieldsStr2 = 'Title_1;Title_3;Title_4;num;inum;onum;Title;QNum;num_of;weight;Title_2;Drawing;Graph';
var
Deleted: Boolean;
FieldValues2: Variant;
FieldValues1: Variant;
begin
ComparisonDataSet.DisableControls;
// clear ComparisonDataSet
if ComparisonDataSet.Active then
ComparisonDataSet.Close;
ComparisonDataSet.CreateDataSet;
// deleted records
SpPlansQuery.First;
while not SpPlansQuery.Eof do
begin
FieldValues1 := SpPlansQuery[ReplaceStr(FieldsStr1, '_PPU', '')];
Deleted := not PPUQuery.Locate('ID', Integer(SpPlansQuery['PPONREC']), []);
if Deleted then
begin
ComparisonDataSet.Append;
// next string throws exception and this is a big problem
ComparisonDataSet[ReplaceStr(FieldsStr1, '_PPU', '')] := FieldValues1;
ComparisonDataSet.Post;
end;
SpPlansQuery.Next;
end;
ComparisonDataSet.First;
ComparisonDataSet.EnableControls;
end;
据您所知,当我尝试将值分配给ComparisonDataSet['ARTICLE']
时,ComparisonDataSet包含名为ARTICLE
和ARTICLE_PPU
的字段,并且出现异常消息无法将(Null)的变量类型转换为(Integer)类型". .我知道这一点是因为我尝试直接分配给该字段,但是得到了相同的结果.
As far you can guess, ComparisonDataSet contains fields named ARTICLE
and ARTICLE_PPU
and exception with message "Cannot convert variant type of (Null) into type (Integer)" occurs when I try to assign value to ComparisonDataSet['ARTICLE']
. I know this because I tried assigning directly to that field but got the same result.
ARTICLE
是一个长度为20个字符的字符串字段.
ARTICLE
is a string field with length of 20 characters.
谁能指出我如何在没有错误的情况下将值分配给TClientDataSet中的字段?
Can anyone point me how to assign values to fields in a TClientDataSet without getting errors?
如下所示,这是我的字段定义:
As requested below, this is my field definition:
在ComparisonSpPlanUnit.dfm
中:
object ComparisonDataSet: TClientDataSet
Aggregates = <>
FieldDefs = <
...
item
Name = 'ARTICLE'
DataType = ftString
Size = 20
end>
...
object ComparisonDataSetARTICLE: TStringField
DisplayLabel = #1057#1090#1072#1090#1100#1103
FieldName = 'ARTICLE'
end
...
end
在ComparisonSpPlanUnit.pas
中:
ComparisonDataSetARTICLE: TStringField;
推荐答案
在调试过程中,我发现在Data.DB
模块的源代码内部以及TStringField.SetVarValue
过程内部都引发了异常,因此这似乎是一个错误.但是我错了:更深入的调试强调了在自动计算字段的计算过程中会引发异常.
During debug I found that exception is raised deep inside source code of Data.DB
module, inside TStringField.SetVarValue
procedure, so it seemed a bug. But I was wrong: deeper debugging highlighted that exception is raised during calculation of auto-calculated fields.
所以我不得不更改其他功能:
So I had to change my other function:
procedure TComparisonSpPlanForm.ComparisonDataSetCalcFields(DataSet: TDataSet);
begin
// comparing to Null is essential!
if DataSet['Oper'] <> Null then
case DataSet['Oper'] of
0: DataSet['OperStr'] := 'insert';
1: DataSet['OperStr'] := 'update';
2: DataSet['OperStr'] := 'delete';
else
DataSet['OperStr'] := 'other';
end
else
DataSet['OperStr'] := 'other';
end;
它现在可以工作.
这篇关于无法将数据分配给客户端数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!