如何避免出现此错误“不可为空的列无法更新为空”当使用ADO主从关系时?
我正在尝试使用 TADODataSet、用于主表的 TDBText 和用于详细信息表的 TDBGrid 构建主从表单(对于订单表单来说,主表包含订单标题,详细信息表包含订单项目)
主主键是身份列(自动增量)字段)
当尝试在主表中添加新记录,然后在将记录发布到主表中之前尝试在详细信息表中添加记录时,我收到此错误“不可为空的列无法更新为空”,发生这种情况是因为主表主键值仍然未知,因为我没有发布主记录,但如果我尝试相同的场景,除了在添加详细记录之前我发布了主记录,则不会出现错误。
如何解决这个问题?
我使用以下属性连接主表与详细信息表: 两个数据集都有位置: 客户端
详细信息表:
- 数据源:主表数据源
- 主记录:Id(主表的主键)
- IndexFieldNames:OrderId(详细信息表中的字段,指示此详细记录属于哪个主记录)
- 锁定类型:BatchOptimistic
请帮助我
提前致谢 亚赞·阿拉哈姆
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)
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
Details Table :
- DataSource : Master Table DataSource
- Master Records : Id (Primary key of the master table)
- IndexFieldNames : OrderId (the field in Details Table that indicates to which master record does this detail record belong to)
- Lock Type : BatchOptimistic
Please help me
Thanks in advance
Yazan Al-lahham
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,
你应该做类似的事情(伪代码):
1 - 启动交易
2 - 发布主记录
3 - 获取在 master 上插入的 id
4 - 将主ID传递给详细数据集
5 - 帖子详细记录
6 - 如果有效,则提交事务。否则,回滚事务。
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.
附带说明一下:新的 SQL Server 代号“Denali”的 CTP 将带来 SEQUENCES 功能,其工作方式与 firebird 生成器的工作方式非常接近。因此,这个任务将变得更加容易:
当您从 gui 获取开始插入的命令时,从序列中获取 ID
用它来填写主记录的PK字段
发布主记录
当您有详细记录要插入时
填写详细记录
帖子详细记录
提交交易
非常niiiice...
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:
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
Very niiiice...