在Access中创建关系

时间:2014-03-31 11:06:13

标签: vb.net ms-access dataset ms-access-2007 relationship

我的访问文件中有两个表。我想在他们之间建立关系。下图是我在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创建关系,但尚未更新为database到{{ 1}}或其他东西。我这么说是正确的还是因为其他原因。如果我没错,那么如何将dataadapter更新为dataset

2 个答案:

答案 0 :(得分:2)

您可以通过执行OleDb连接中的ALTER TABLE语句来创建该关系。

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

答案 1 :(得分: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调用宏。