script type="text/javascript" src="" /script
成都創(chuàng)新互聯(lián)公司一直秉承“誠信做人,踏實(shí)做事”的原則,不欺瞞客戶,是我們最起碼的底線! 以服務(wù)為基礎(chǔ),以質(zhì)量求生存,以技術(shù)求發(fā)展,成交一個(gè)客戶多一個(gè)朋友!為您提供網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、成都網(wǎng)頁設(shè)計(jì)、成都小程序開發(fā)、成都網(wǎng)站開發(fā)、成都網(wǎng)站制作、成都軟件開發(fā)、成都app軟件開發(fā)公司是成都本地專業(yè)的網(wǎng)站建設(shè)和網(wǎng)站設(shè)計(jì)公司,等你一起來見證!
script type="text/javascript" src="" /script
有兩個(gè)表order(orderid,ordername,totalprice), orderdetail(orderid,productid,productname,price) 已經(jīng)建立一對(duì)多
關(guān)聯(lián)(orderid). 用VB.NET建立一個(gè)窗體, 用于主表和明細(xì)表的數(shù)據(jù)錄入及更新.
在窗體上增加兩個(gè)sqldataadapter,分別選擇order和orderdetail表, 并生成數(shù)據(jù)集,在DATASET中建立關(guān)聯(lián)(orderid) 主表中:ORDERID,ORDERNAME 綁定在兩個(gè)TEXTBOX文本框中,用于接受用戶的輸入,totalprice接受明細(xì)表中price
的匯總值.
明細(xì)表綁定到一個(gè)DATAGRID控件, datasource:dsorder; datamember:order.orderorderdetail建立的關(guān)聯(lián)名稱
問題: 在主表中新增一個(gè)記錄,用戶錄入orderid和ordername后,進(jìn)入明細(xì)表的錄入,用戶錄入productid, productname, price后,提示 “明細(xì)orderID列不允許空值,要更正該值嗎?” 是什么原因?
用于連接主表的明細(xì)orderID如果輸入跟主表一樣的orderID時(shí)提示“ForeignKeyconstraint 要求在交叉表中存在子鍵值。要更正該值嗎?”
只有錄入主表中的orderid和ordername后, 先更新主表數(shù)據(jù)源,才可以編輯更新明細(xì)表,這是為什么? 請(qǐng)問,有沒有其他更好的方法, 實(shí)現(xiàn)主表和明細(xì)表的新增記錄同時(shí)更新呢? 謝謝關(guān)照! 網(wǎng)友:方宇 回復(fù):
附部分源代碼:
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 script type="text/javascript" src="" /script
script type="text/javascript" src="" /script
如果你是用SqlDataAdapter,那么你最后要加一句:
SqlDataAdapter.Update(objds,虛擬表名)
至于補(bǔ)充問題:
試試用這樣的方法:
dim
send,e
as
object
spdDetail2_CellClick(send,e)
或
spdDetail2_CellClick(nothing,nothing)
還再不行!那么你獨(dú)立寫一個(gè)方法就好了!
然后But_Clear_Click(...)調(diào)用這個(gè)方法,
其他地方需要也調(diào)用這個(gè)方法!
Const?sql?As?String?=?"INSERT?INTO?idc?(name,?tel)?VALUES?(22,11)"
Const?cnStr?As?String?=?"Data?Source=(local);Initial?Catalog=master;Integrated?Security=False;User?ID=sa;Password=123456;"
Dim?cn?As?OleDbConnection?=?New?OleDbConnection(cnStr)
Dim?cmd?As?OleDbCommand?=?New?OleDbCommand(sql)
cmd.ExecuteNonQuery()