将整个数据库检索到数据集中

时间:2011-07-10 19:19:36

标签: asp.net dataset

我有3个不同的表访问数据库,我想将整个数据库加载到数据集中,这样我就可以在不加载db serval时间的情况下处理数据。 使用数据集的所有示例都显示了如何使用“.fill”

获取数据库的一部分

例如:

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

 OleDbAdapter myDataAdapter = new OleDbAdapter (null, con);

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

此示例仅从员工加载,但如何将所有表一次性导入数据集?

例如,在xml中有命令将所有文档加载到数据集:“dataset.ReadXml”

我如何在访问数据库中实现它?

感谢您的帮助

Baaroz

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您应该使用不同的SelectCommands调用OleDbDataAdapter.Fill方法,并传递相同的DataSet但内部不同的表名。在这种情况下,您的dataSet将包含不同的填充表。