OleDb:为啥我不能用我的数据源中的完整模式和信息轻松地填充我的数据集?

Posted

技术标签:

【中文标题】OleDb:为啥我不能用我的数据源中的完整模式和信息轻松地填充我的数据集?【英文标题】:OleDb: why can't I just fill my dataset with the full schema and information from my datasource easily?OleDb:为什么我不能用我的数据源中的完整模式和信息轻松地填充我的数据集? 【发布时间】:2010-06-26 21:22:28 【问题描述】:

这看起来真的很荒谬..我错过了什么?

我有一个包含 5 个表的 access 数据库。

我想用数据库的全部内容填充数据集。

为什么它不能大致看起来像这样?

dim dConnection as new oleDbConnection(connection info)

dim dAdapter as new oledbdataadapter(SelectCommand, dConnection)
dim dSet as oleDbDataset

da.fill(ds)

相反,我必须为每个表创建一个适配器,选择表名(为什么?),填写架构,然后填写表。

这对于一个庞大的数据库来说毫无价值......所以必须有一个更简单的方法来解决这个问题?

谢谢!!!!!!

【问题讨论】:

我对 VB.NET 一无所知,但看起来您已经习惯于在一个对象中返回多个 SELECT 语句的结果。由于 Jet/ACE 一次不能执行多个 SQL 语句,所以这是不可能的。对于没有服务器进程管理与数据存储本身的交互的数据库引擎,这是可以预期的。 【参考方案1】:

参见thiscodeguru 论坛帖子-页面中的最后一个答案提供了将整个访问数据库加载到数据集中的功能:

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

【讨论】:

以上是关于OleDb:为啥我不能用我的数据源中的完整模式和信息轻松地填充我的数据集?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我不能用我的 JWK 签署这些声明?

为啥 VFP .NET OLEdb 提供程序不能在 64 位 Windows 中工作?

OleDb 数据源 - 为啥它会出现在这个随机位置? [复制]

playframework 1.2.x 如何在开发模式下每次播放重新加载时调用我的方法?

为啥 System.Data.OleDb 代码会联系 Microsoft?

为啥我的状态栏上的图标不能在不同的模式下改变?