简体   繁体   English

Delphi 7和Delphi XE4中Null和Empty字符串之间的区别

[英]Difference between Null and Empty String in Delphi 7 and Delphi XE4

I have a following firebird dataset: 我有以下火鸟数据集:

ds1 : TpFIBDataset;

DFM file: DFM文件:

object ds1ID: TFIBIntegerField
 FieldName = 'ID'
end
object ds1FIELD_VALUE: TFIBStringField
  FieldName = 'FIELD_VALUE'
  Size = 250
end

In my Firebird database: ID is integer field and FIELD_VALUE is varchar field. 在我的Firebird数据库中:ID是整数字段,而FIELD_VALUE是varchar字段。

Delphi 7: Data is being inserted like this Delphi 7:像这样插入数据

ds1.InsertRecord([123, anyValue]);

here anyValue : variant; 在这里anyValue : variant;

If anyValue = null, null is getting inserted into the database which is the required functionality. 如果anyValue = null,则将null插入到数据库中,这是必需的功能。

In Delphi XE4 , I am doing same like this: 在Delphi XE4中 ,我这样做是一样的:

ds1.Insert;
ds1.FieldByName('ID').AsInteger := 123;
ds1.FieldByName('FIELD_VALUE').AsString := anyValue;
ds1.Post;

I get error: could not convert variant of type (null) into type (OleStr) 我收到错误消息: could not convert variant of type (null) into type (OleStr)

When I try like this 当我这样尝试

ds1.Insert;
ds1.FieldByName('ID').AsInteger := 123;
ds1.FieldByName('FIELD_VALUE').AsString := VarToStr(anyValue);
ds1.Post;

Empty string is inserted into the database, but I need null there. 空字符串已插入数据库,但在那里我需要为null。 What and where should I make change to accomplish this task. 我应该在什么地方进行更改以完成此任务。

You need use 您需要使用

ds1.FieldByName('FIELD_VALUE').Value := anyValue;

instead of 代替

ds1.FieldByName('FIELD_VALUE').AsString := anyValue;

There is some difference in this delphi versions. 此delphi版本存在一些差异。 Starting from delphi2006 there is another memory manager that works more precise. 从delphi2006开始,还有另一个工作更精确的内存管理器。 In example in delphi 7 and lower you can assign NULL to string or integer value. 在delphi 7及更低版本的示例中,您可以将NULL分配给字符串或整数值。 And comparision NULL with 0 will return true. 并且比较NULL与0将返回true。 In greater versions you can't do this. 在更高版本中,您无法执行此操作。 Null is not equal 0 or empty string. Null不等于0或为空字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM