script type="text/javascript" src="" /script
堅(jiān)守“ 做人真誠 · 做事靠譜 · 口碑至上 · 高效敬業(yè) ”的價(jià)值觀,專業(yè)網(wǎng)站建設(shè)服務(wù)10余年為成都成都廣告推廣小微創(chuàng)業(yè)公司專業(yè)提供企業(yè)網(wǎng)站建設(shè)營銷網(wǎng)站建設(shè)商城網(wǎng)站建設(shè)手機(jī)網(wǎng)站建設(shè)小程序網(wǎng)站建設(shè)網(wǎng)站改版,從內(nèi)容策劃、視覺設(shè)計(jì)、底層架構(gòu)、網(wǎng)頁布局、功能開發(fā)迭代于一體的高端網(wǎng)站建設(shè)服務(wù)。
script type="text/javascript" src="" /script
有兩個(gè)表order(orderid,ordername,totalprice), orderdetail(orderid,productid,productname,price) 已經(jīng)建立一對多
關(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ì)表,這是為什么? 請問,有沒有其他更好的方法, 實(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
Dim da As New DataTable
Dim myda As OleDb.OleDbDataAdapter("SELECT * FROM 表“, oconnection)
myda.Fill(da)
姓名=da.Rows(0)(0) ‘張三
姓名=da.Rows(1)(0) ‘李四
姓名=da.Rows(2)(0) ‘王五
姓名=da.Rows(3)(0) ‘趙六
姓名=da.Rows(4)(0) ‘劉琦
這個(gè)不一定要用數(shù)組,直接用table就可以操作了。
處理數(shù)據(jù)行(DataRow)
Windows窗體中的數(shù)據(jù)綁定列表框和組合框很節(jié)省時(shí)間 典型的代碼如下(假定已經(jīng)建立了SqlDataAdapter或者其它部件獲取數(shù)據(jù))
Dim ds As New DataSet() SqlDataAdapter Fill(ds Customers ) ListBox DataSource = ds Tables( Customers ) ListBox DisplayMember = CompanyName ListBox ValueMember = CustomerID
在這種情況下 代碼使用Northwind數(shù)據(jù)庫的顧客記錄工作 DisplayMember屬性設(shè)置為你希望用戶在列表框中看到的記錄字段 它是customers表的CompanyName 通常ValueMember屬性設(shè)置為數(shù)據(jù)表中的一個(gè)鍵字段 對于customer來說是CustomerID 一旦用戶選擇了列表框中的一行 很容易使用列表框的SelectedValue屬性獲得鍵字段
MsgBox(ListBox SelectedValue)
但是有可能需要一個(gè)與被選擇項(xiàng)相關(guān)的整個(gè)數(shù)據(jù)行對象的引用 例如 如果被選擇的行需要被刪除 就不知道鍵了 你需要一個(gè)數(shù)據(jù)行的引用以使用Delete方法
典型的Visual Basic開發(fā)者通常這樣想 我已經(jīng)得到了該行的鍵了 我將編寫一些邏輯來查找使用該鍵的行 這樣可以實(shí)現(xiàn) 但是有更好的實(shí)現(xiàn)方法 可以使用一行代碼獲取與列表框中選項(xiàng)關(guān)聯(lián)的數(shù)據(jù)行
Dim dr As DataRow = CType(ListBox SelectedItem DataRowView) Row
通常該邏輯不會(huì)憑直覺出現(xiàn) 即使對經(jīng)驗(yàn)豐富的開發(fā)者 為了解釋這是怎樣實(shí)現(xiàn)的 我把上面的一行拆成幾行 下面的代碼與上面代碼的功能相同
Dim drv As DataRowView drv = CType(ListBox SelectedItem DataRowView) Dim dr As DataRow dr = drv Row
DataRowView類是數(shù)據(jù)行的包裝 它被多個(gè)Windows窗體控件使用 它使得顯示與控件中的數(shù)據(jù)行相關(guān)的數(shù)據(jù)更加容易 當(dāng)列表框被數(shù)據(jù)綁定到數(shù)據(jù)表時(shí)(假定列表框中的有些行當(dāng)前被選定了) 列表框的SelectedItem屬性保存了一個(gè)DataRowView對象
這意味著我們能把列表框的SelectedItem屬性轉(zhuǎn)換到DataRowView對象 這就是上面代碼中的第二行實(shí)現(xiàn)的 接著DataRowView暴露一個(gè)Row屬性 它指向被包裝的數(shù)據(jù)行 上面的代碼聲明了一個(gè)數(shù)據(jù)行并設(shè)置了Row屬性
轉(zhuǎn)換對象的類型以訪問它的接口的技術(shù)在Visual Basic 中不是經(jīng)常使用 但是在Visual Basic NET中這是經(jīng)常的 有了上面的例子后 大多數(shù)有經(jīng)驗(yàn)的開發(fā)者迅速跟上了這種技術(shù)
數(shù)據(jù)行的引用(dr)可用于用任何方式維護(hù)行 訪問數(shù)據(jù)行中的任何特定字段是可行的 行中的數(shù)據(jù)可以被改變 能使數(shù)據(jù)行的Delete方法把該行標(biāo)識(shí)為刪除 或者從數(shù)據(jù)表的行集合中刪除該行 下面的代碼標(biāo)識(shí)刪除了一行
dr Delete()
lishixinzhi/Article/program/net/201311/12974