使用ADO Master-Detail关系时,如何避免将此错误“非可空列无法更新为null”?

时间:2022-01-10 22:45:48

I am trying to build Master-Detail Form using TADODataSet, TDBText for Master Table and TDBGrid for Details Table (something smiller to Orders Form like Master Table includs the order header and Details Table includes the Order Items)

我正在尝试使用TADODataSet,主表的TDBText和详细信息表的TDBGrid来构建主详细信息表单(像主表这样的订单窗口的smiller包括订单标题,详细信息表包括订单项)

Master primary key is Identity column (autoincreament Field)

主键是标识列(自动增量字段)

When trying to add a new record in Master Table and then trying to add records in Details Table before posting the record in Master Table I get this error "non-nullable column cannot be updated to null" and this happens because the master table primary key value is still not known because I didn't post the master record but if I tried the same scenario except that before I add the details records I posted the Master record then the error doesn't appear.

当尝试在主表中添加新记录,然后尝试在主表中发布记录之前在详细信息表中添加记录时,我得到此错误“非可空列无法更新为空”,这是因为主表主键发生值仍然未知,因为我没有发布主记录,但如果我尝试相同的方案,除了在我添加详细记录之前,我发布了主记录,然后错误没有出现。

how to work around this problem?

如何解决这个问题?

I am connecting Master Table with Details Table using the following properties : Both DataSets have courser location : Client

我使用以下属性连接主表和详细信息表:两个DataSet都有courser位置:客户端

Details Table :

详细信息表:

  • DataSource : Master Table DataSource
  • DataSource:主表数据源
  • Master Records : Id (Primary key of the master table)
  • 主记录:Id(主表的主键)
  • IndexFieldNames : OrderId (the field in Details Table that indicates to which master record does this detail record belong to)
  • IndexFieldNames:OrderId(详细信息表中的字段,指示此详细记录属于哪个主记录)
  • Lock Type : BatchOptimistic
  • 锁定类型:BatchOptimistic

Please help me

请帮帮我

Thanks in advance Yazan Al-lahham

在此先感谢Yazan Al-lahham

2 个解决方案

#1


3  

Well,

好,

You should do something like that (pseudo-code):

你应该做那样的事情(伪代码):

1 - start a transaction
2 - post master record
3 - get the id inserted on master
4 - pass the master id to detail dataset
5 - post detail record
6 - If it worked, commit transaction. Otherwise, rollback transaction.

1 - 启动事务2 - 发布主记录3 - 获取插入主4的id - 将主ID传递给详细数据集5 - 发布详细记录6 - 如果有效,则提交事务。否则,回滚事务。

#2


0  

Just an side note: CTP of the new SQL Server codename 'Denali' will bring the feature of SEQUENCES, working much near of whar firebird generator works. So this task will become MUCH easier:

只是旁注:新SQL Server代号'Denali'的CTP将带来SEQUENCES的功能,工作距离whar firebird generator工作很近。因此,这项任务将变得更加容易:

When you get the command from gui to start an insert, get an ID from sequence Use it to fill the PK field of master record Post master record While you have detail records to insert Fill detail(s) record Post detail record Commit transaction

当您从gui获取命令以启动插入时,从序列中获取ID使用它来填充主记录的PK字段发布主记录当您有详细记录时要插入填充详细信息记录发布详细记录提交事务

Very niiiice...

非常niiiice ...

#1


3  

Well,

好,

You should do something like that (pseudo-code):

你应该做那样的事情(伪代码):

1 - start a transaction
2 - post master record
3 - get the id inserted on master
4 - pass the master id to detail dataset
5 - post detail record
6 - If it worked, commit transaction. Otherwise, rollback transaction.

1 - 启动事务2 - 发布主记录3 - 获取插入主4的id - 将主ID传递给详细数据集5 - 发布详细记录6 - 如果有效,则提交事务。否则,回滚事务。

#2


0  

Just an side note: CTP of the new SQL Server codename 'Denali' will bring the feature of SEQUENCES, working much near of whar firebird generator works. So this task will become MUCH easier:

只是旁注:新SQL Server代号'Denali'的CTP将带来SEQUENCES的功能,工作距离whar firebird generator工作很近。因此,这项任务将变得更加容易:

When you get the command from gui to start an insert, get an ID from sequence Use it to fill the PK field of master record Post master record While you have detail records to insert Fill detail(s) record Post detail record Commit transaction

当您从gui获取命令以启动插入时,从序列中获取ID使用它来填充主记录的PK字段发布主记录当您有详细记录时要插入填充详细信息记录发布详细记录提交事务

Very niiiice...

非常niiiice ...