2011-07-10 52 views
0

我有3個不同的表訪問數據庫,我想將整個數據庫加載到數據集中,這樣我就可以在不加載數據庫時間的情況下處理數據。 所有數據集工作的例子展示瞭如何使用「.fill僞」將整個數據庫檢索到數據集中

例如得到數據庫的一部分:

OleDbCommand CommandObject = new OleDbCommand ("Select * from employee"); 

OleDbAdapter myDataAdapter = new OleDbAdapter (null, con); 

    myDataAdapter.SelectCommand = CommandObject; 
    myDataAdapter.Fill (myDataSet, "EmployeeData"); 

只能從員工這個例子負載,但我怎麼能etrieve的所有表一次進入數據集?

在XML比如有命令加載所有到DataSet中的文件:「dataset.ReadXml」

我怎樣才能achive它在Access數據庫?

感謝所有幫助

Baaroz

回答

0

你應該只調用不同SelectCommands的OleDbDataAdapter.Fill方法,並傳遞同一數據集內,但不同的表名。在這種情況下,你的dataSet將包含不同的填充表。

1
Protected Function getDataSetAndFill(ByRef connection As OleDb.OleDbConnection, 
    Optional ByVal isExportSchema As Boolean = True) As DataSet 

    Dim myDataSet As New DataSet 
    Dim myCommand As New OleDb.OleDbCommand 
    Dim myAdapter As New OleDb.OleDbDataAdapter 

    myCommand.Connection = connection 

    'Get Database Tables 
    Dim tables As DataTable = connection.GetOleDbSchemaTable(_ 
     System.Data.OleDb.OleDbSchemaGuid.Tables, _ 
     New Object() {Nothing, Nothing, Nothing, "TABLE"}) 

    'iterate through all tables 
    Dim table As DataRow 
    For Each table In tables.Rows 

     'get current table's name 
     Dim tableName As String = table("TABLE_NAME") 

     Dim strSQL = "SELECT * FROM " & "[" & tableName & "]" 

     Dim adapter1 As New OleDb.OleDbDataAdapter(New OleDb.OleDbCommand(strSQL, connection)) 
     adapter1.FillSchema(myDataSet, SchemaType.Source, tableName) 

     'Fill the table in the dataset 
     myCommand.CommandText = strSQL 
     myAdapter.SelectCommand = myCommand 
     myAdapter.Fill(myDataSet, tableName) 
    Next 

    '''''''''''''''''''''''''''''''''''''' 
    '''' Add relationships to dataset '''' 
    '''''''''''''''''''''''''''''''''''''' 

    'First, get relationships names from database (as well as parent table and child table names) 
    Dim namesQuery As String = "SELECT DISTINCT szRelationship, szReferencedObject, szObject " & _ 
           "FROM MSysRelationships" 
    Dim namesCommand As New System.Data.OleDb.OleDbCommand(namesQuery, connection) 
    Dim namesAdapter As New System.Data.OleDb.OleDbDataAdapter(namesCommand) 
    Dim namesDataTable As New DataTable 
    namesAdapter.Fill(namesDataTable) 

    'Now, get MSysRelationship from database 
    Dim relationsQuery As String = "SELECT * FROM MSysRelationships" 
    Dim command As New System.Data.OleDb.OleDbCommand(relationsQuery, connection) 
    Dim adapter As New System.Data.OleDb.OleDbDataAdapter(command) 
    Dim relationsDataTable As New DataTable 
    adapter.Fill(relationsDataTable) 

    Dim relationsView As DataView = relationsDataTable.DefaultView 
    Dim relationName As String 
    Dim parentTableName As String 
    Dim childTablename As String 
    Dim row As DataRow 

    For Each relation As DataRow In namesDataTable.Rows 
     relationName = relation("szRelationship") 
     parentTableName = relation("szReferencedObject") 
     childTablename = relation("szObject") 

     'Keep only the record of the current relationship 
     relationsView.RowFilter = "szRelationship = '" & relationName & "'" 

     'Declare two arrays for parent and child columns arguments 
     Dim parentColumns(relationsView.Count - 1) As DataColumn 
     Dim childColumns(relationsView.Count - 1) As DataColumn 

     For i As Integer = 0 To relationsView.Count - 1 
      parentColumns(i) = myDataSet.Tables(parentTableName). _ 
          Columns(relationsView.Item(i)("szReferencedColumn")) 
      childColumns(i) = myDataSet.Tables(childTablename). _ 
          Columns(relationsView.Item(i)("szColumn")) 
     Next 

     Dim newRelation As New DataRelation(relationName, parentColumns, childColumns, False) 
     myDataSet.Relations.Add(newRelation) 
    Next 

    If isExportSchema Then 
     Dim schemaName = GetXmlSchemaFileName() 
     If File.Exists(schemaName) Then File.SetAttributes(schemaName, FileAttributes.Normal) 
     myDataSet.WriteXmlSchema(schemaName) 
    End If 
    Return myDataSet 
End Function