首页 > 编程 > .NET > 正文

一对多关联的数据库在VB.NET中如何新增记录

2024-07-10 13:01:34
字体:
来源:转载
供稿:网友

最大的网站源码资源下载站,

有两个表order(orderid,ordername,totalprice), orderdetail(orderid,productid,productname,price) 已经建立一对多
关联(orderid). 用vb.net建立一个窗体, 用于主表和明细表的数据录入及更新.
在窗体上增加两个sqldataadapter,分别选择order和orderdetail表, 并生成数据集,在dataset中建立关联(orderid)

主表中:orderid,ordername 绑定在两个textbox文本框中,用于接受用户的输入,totalprice接受明细表中price
的汇总值.
明细表绑定到一个datagrid控件, datasource:dsorder; datamember:order.orderorderdetail建立的关联名称

问题:

在主表中新增一个记录,用户录入orderid和ordername后,进入明细表的录入,用户录入productid, productname, price后,提示
“明细orderid列不允许空值,要更正该值吗?” 是什么原因?
用于连接主表的明细orderid如果输入跟主表一样的orderid时提示“foreignkeyconstraint 要求在交叉表中存在子键值。要更正该值吗?”

只有录入主表中的orderid和ordername后, 先更新主表数据源,才可以编辑更新明细表,这是为什么?

请问,有没有其他更好的方法, 实现主表和明细表的新增记录同时更新呢?

谢谢关照!
网友:方宇

回复:http://[email protected]
http://[email protected]

附部分源代码:
public class fmorders
inherits system.windows.forms.form
dim withevents dtorderdetails as new datatable()
dim bmorders as bindingmanagerbase
dim bmorderdetails as bindingmanagerbase
dim orderdetailshaschange as boolean
dim orderid as datacolumn
dim ordername as datacolumn
dim subtotal as datacolumn

private sub bnfirst_click(byval sender as system.object, byval e as system.eventargs) handles bnfirst.click
bmorders.position = 0
end sub
private sub bnprior_click(byval sender as system.object, byval e as system.eventargs) handles bnprior.click
bmorders.position -= 1
end sub
private sub bnnext_click(byval sender as system.object, byval e as system.eventargs) handles bnnext.click
bmorders.position += 1
end sub
private sub bnlast_click(byval sender as system.object, byval e as system.eventargs) handles bnlast.click
bmorders.position = bmorders.count - 1
end sub

private sub bnadd_click(byval sender as system.object, byval e as system.eventargs) handles bnadd.click
try
bmorders.endcurrentedit()
bmorders.addnew()

catch err as system.systemexception
messagebox.show(err.tostring)
end try
end sub
private sub bnload_click(byval sender as system.object, byval e as system.eventargs) handles bnload.click
filldata()
end sub

private sub bnupdate_click(byval sender as system.object, byval e as system.eventargs) handles bnupdate.click
dim dstestorderschange as dataset

bmorderdetails.endcurrentedit()
bmorders.endcurrentedit()
if not dstestorders.haschanges(datarowstate.deleted) then
try
daorders.update(dstestorders, "orders")
daorderdetails.update(dstestorders, "orderdetails")
dstestorders.acceptchanges()
catch err as system.systemexception
dstestorders.rejectchanges()
messagebox.show(err.tostring)
throw
end try
else
try
daorderdetails.update(dstestorders, "orderdetails")
daorders.update(dstestorders, "orders")
dstestorders.acceptchanges()
catch err as system.systemexception
dstestorders.rejectchanges()
messagebox.show(err.tostring)
throw
end try
end if
end sub

private sub fmorders_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
filldata()
dtorderdetails = dstestorders.tables("orderdetails")
bmorders = me.bindingcontext(dstestorders, "orders")
bmorderdetails = me.bindingcontext(dstestorders, "orders.ordersorderdetails")
orderdetailshaschange = true
subtotal = dstestorders.tables("orders").columns("subtotal")
subtotal.defaultvalue = 0
price = dstestorders.tables("orderdetails").columns("price")
price.defaultvalue = 0
end sub

private sub filldata()
dstestorders.enforceconstraints = false
daorders.fill(dstestorders)
daorderdetails.fill(dstestorders)
dstestorders.enforceconstraints = true
end sub
private sub dtorderdetails_columnchanged(byval sender as object, byval e as system.data.datacolumnchangeeventargs) _
handles dtorderdetails.columnchanged

dim drorders as datarow
dim drorderdetails as datarow
dim isubtotal as integer
try
if orderdetailshaschange then
orderdetailshaschange = false
drorders = dstestorders.tables("orders").rows(bmorders.position)
isubtotal = 0
for each drorderdetails in drorders.getchildrows("ordersorderdetails")


isubtotal = isubtotal + drorderdetails("price")
next
drorders.beginedit()
drorders("subtotal") = isubtotal
drorders.endedit()
end if
finally
orderdetailshaschange = true
end try
end sub







private sub bndelete_click(byval sender as system.object, byval e as system.eventargs) handles bndelete.click
if bmorders.count > 0 then
bmorders.removeat(bmorders.position)
end if


end sub

private sub bncancel_click(byval sender as system.object, byval e as system.eventargs) handles bncancel.click
bmorders.cancelcurrentedit()

end sub
end class
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表