首先你的數(shù)據(jù)源就寫(xiě)錯(cuò)了
按需定制網(wǎng)站可以根據(jù)自己的需求進(jìn)行定制,成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站構(gòu)思過(guò)程中功能建設(shè)理應(yīng)排到主要部位公司成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站的運(yùn)用實(shí)際效果公司網(wǎng)站制作網(wǎng)站建立與制做的實(shí)際意義
strconn = "Provider =Microsoft.Jet.OLEDB.4.0;Data Source =E:\AccessDataBase\test2000.mdb"
應(yīng)為
strconn = "Provider =Microsoft.Jet.OLEDB.4.0;Data Source =E:\\AccessDataBase\\test2000.mdb"
絕對(duì)路徑是"\\",這個(gè)要記得。
代碼我就不幫你寫(xiě)了,說(shuō)下我的思路,對(duì)于數(shù)據(jù)庫(kù)的操作,你只能1步步來(lái)做,也就是在dataGridView里做了修改以后就直接在數(shù)據(jù)庫(kù)里修改,打個(gè)比方,你想在dataGridView里添加數(shù)據(jù)A再刪除數(shù)據(jù)B,步驟如下:
1.你在dataGridView里添加了數(shù)據(jù)A
2.打開(kāi)數(shù)據(jù)庫(kù)
3.在相應(yīng)表下添加A
4.關(guān)閉數(shù)據(jù)庫(kù)
5.在dataGridView里刪除數(shù)據(jù)B
6.打開(kāi)數(shù)據(jù)庫(kù)
7.在相應(yīng)表里刪除數(shù)據(jù)B
8.關(guān)閉數(shù)據(jù)庫(kù)
看起來(lái)方法比較復(fù)雜,但是數(shù)據(jù)庫(kù)的操作必須這樣做,而且,記得,打開(kāi)數(shù)據(jù)庫(kù)并操作結(jié)束后先關(guān)閉數(shù)據(jù)庫(kù)再做其他的任務(wù)。
list1.additem j 意思就是把j添加至list1列表中,這是在一個(gè)for 循環(huán)中,循環(huán)了10次,添加了10次 j,即 list1 中 有10列分別為:1,3,6,10,15,21,28,36,45,55
下面又來(lái)了一個(gè)循環(huán), list1.removeitem 刪除列,刪除的是 list1.listcount - i ,list1.listcount 就是總列數(shù)為10,減去 i ,i 是1到4,即10-1,9-2,8-3,7-4 每減掉一次,list1.listcount 就少了一條記錄。
(注意,list1.listcount-1 是最后一條記錄)
減掉了第 9,7,5,3列
剩余為 1,3,6,15,28,45
結(jié)果為:3,15,28
'ListCount返回列表框中的項(xiàng)目總數(shù)
'ListCount-1是列表框中最后一個(gè)項(xiàng)目的索引號(hào)
'虧你提醒了,我寫(xiě)錯(cuò)了,上面修改了,第一個(gè)索引為0,最后一個(gè)為9.
'也就是,問(wèn)題所問(wèn)的第二列的索引為1,第四列的索引為3...
script type="text/javascript" src="" /script
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文本框中,用于接受用戶(hù)的輸入,totalprice接受明細(xì)表中price
的匯總值.
明細(xì)表綁定到一個(gè)DATAGRID控件, datasource:dsorder; datamember:order.orderorderdetail建立的關(guān)聯(lián)名稱(chēng)
問(wèn)題: 在主表中新增一個(gè)記錄,用戶(hù)錄入orderid和ordername后,進(jìn)入明細(xì)表的錄入,用戶(hù)錄入productid, productname, price后,提示 “明細(xì)orderID列不允許空值,要更正該值嗎?” 是什么原因?
用于連接主表的明細(xì)orderID如果輸入跟主表一樣的orderID時(shí)提示“ForeignKeyconstraint 要求在交叉表中存在子鍵值。要更正該值嗎?”
只有錄入主表中的orderid和ordername后, 先更新主表數(shù)據(jù)源,才可以編輯更新明細(xì)表,這是為什么? 請(qǐng)問(wèn),有沒(méi)有其他更好的方法, 實(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
一種是用ADO.NET來(lái)執(zhí)行alter table student add english int實(shí)現(xiàn)數(shù)據(jù)庫(kù) 表的列添加
網(wǎng)頁(yè)名稱(chēng):vb.net數(shù)據(jù)的增加 vb添加數(shù)據(jù)到數(shù)據(jù)庫(kù)
分享路徑:http://muchs.cn/article26/dojhhcg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、網(wǎng)站內(nèi)鏈、標(biāo)簽優(yōu)化、ChatGPT、商城網(wǎng)站、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)