我的訪問文件中有兩個表。我想創建它們之間的關係。下圖是我在Access中手動創建的關係。在Access中創建關係
不過,我想創建VB.net的關係,這裏是我的代碼:
conn.Open()
daBooks = New OleDb.OleDbDataAdapter("SELECT * FROM Books", conn)
daAuthor = New OleDb.OleDbDataAdapter("SELECT * FROM authors", conn)
daBooks.Fill(ds, "Books")
daAuthor.Fill(ds, "authors")
conn.Close()
'Set the relation
Dim parentColumn As DataColumn
parentColumn = ds.Tables("authors").Columns("AuthorID")
Dim childColumn As DataColumn = New DataColumn
Try
childColumn = ds.Tables("Books").Columns("AuthorID")
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
Dim a As String
a = ds.Tables("authors").Rows(0).Item("AuthorID")
Dim b As String
b = ds.Tables("Books").Rows(0).Item("AuthorID")
Dim relation As DataRelation = New _
System.Data.DataRelation("Books_Authors", parentColumn, childColumn)
ds.Relations.Add(relation)
RelationName.Text = relation.RelationName
'End of setting relation
Dim cb1 As New OleDb.OleDbCommandBuilder(daBooks)
Dim cb2 As New OleDb.OleDbCommandBuilder(daAuthor)
Try
daBooks.Update(ds, "books")
Catch ex As Exception
MsgBox(ex.Message)
End Try
daAuthor.Update(ds, "authors")
但是之後我跑的代碼,它不能更改數據庫。任何人都可以幫助我,這樣我就可以在VB.NET中爲兩個表創建一個新的關係。
一般來說,我認爲問題是,System.Data.DataRelation
和ds.Relations.Add(relation)
剛剛創建的dataset
的關係,但它並沒有被通過dataadapter
或別的東西更新到database
。我是這樣說的,還是因爲其他原因而糾正的。如果我是正確的,那麼如何更新dataset
到database
?
你想互操作(http://wiki.lessthandot.com/index.php/Access,_Script_and_Relationships)或ADOX(http://allenbrowne.com/func-adox.html#CreateKeyAdox)? – Fionnuala