2014-03-31 164 views
2

我的訪問文件中有兩個表。我想創建它們之間的關係。下圖是我在Access中手動創建的關係。在Access中創建關係

enter image description here

不過,我想創建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.DataRelationds.Relations.Add(relation)剛剛創建的dataset的關係,但它並沒有被通過dataadapter或別的東西更新到database。我是這樣說的,還是因爲其他原因而糾正的。如果我是正確的,那麼如何更新datasetdatabase

+0

你想互操作(http://wiki.lessthandot.com/index.php/Access,_Script_and_Relationships)或ADOX(http://allenbrowne.com/func-adox.html#CreateKeyAdox)? – Fionnuala

回答

2

您可以通過從OleDb連接執行ALTER TABLE語句來創建該關係。

ALTER TABLE Books 
ADD CONSTRAINT BooksRelationship 
FOREIGN KEY (AuthorID) REFERENCES Authors (AuthorID); 
+0

你能更具體嗎?如何把它放在OleDb連接。非常感謝。 – 2342G456DI8

+0

我假設你已經有一個可用的OleDb連接。 conn.Open()是什麼'conn'? – HansUp

+0

dbProvider =「PROVIDER = Microsoft.ACE.OLEDB.12.0;」 dbSource =「Data Source =」+ Directory.GetCurrentDirectory()+「\ JustForFun.accdb」 conn.ConnectionString = dbProvider&dbSource – 2342G456DI8

1

您可以在Access中創建一個宏,在您的表之間創建關係並通過VB.NET運行它。
下面是創建在MS Access的關係的函數:

Public Function MacroCreateRelation() 

Dim db As DAO.Database 

CreateRelation("Author", "IdAuthor", _ 
          "Book", "IdAuthor") 

Set db = Nothing 
End Function 


Private Function CreateRelation(primaryTblName As String, _ 
    primaryFieldName As String, _ 
    foreignTblName As String, _ 
    foreignFieldName As String) As Boolean 
On Error GoTo ErrHandler 

Dim myDB As DAO.Database 
Dim newRelation As DAO.Relation 
Dim relatingField As DAO.Field 
Dim relationName As String 

relationName = primaryTblName + "_" + primaryFieldName + _ 
    "__" + foreignTblName + "_" + foreignFieldName 

    Set myDB = CurrentmyDB() 

    ' First create the relation 
    Set newRelation = myDB.CreateRelation(relationName, _ 
     primaryTblName, foreignTblName) 
    'field of the primary table 
    Set relatingField = newRelation.CreateField(primaryFieldName) 
    'Then the field of the the second table 
    relatingField.ForeignName = foreignFieldName 

    'now just add the field to the relation 
    newRelation.Fields.Append relatingField 

    'Last but not least add the relation to the db 
    myDB.Relations.Append newRelation 

    Set myDB = Nothing 

    return True 

Exit Function 

    ErrHandler: 
    Debug.Print Err.Description + " [ " + relationName + "]" 
    return False 

End Function 

然後你剛纔打電話從VB.NET宏。