OleDb:为什么我不能轻松地使用数据源中的完整架构和信息填充数据集?

发布于 2024-09-07 03:43:38 字数 417 浏览 3 评论 0原文

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

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

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

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

dim dConnection as new oleDbConnection(connection info)

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

da.fill(ds)

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

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

谢谢!!!!!!

This seems really ridiculous.. what am I missing?

I have an access database with 5 tables.

I want to fill a dataset with the entire contents of the database.

Why can't it roughly look like this?:

dim dConnection as new oleDbConnection(connection info)

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

da.fill(ds)

Instead I'm having to create an adapter for EVERY table, tab the table name (why?), fill the schema, and then fill the table.

This would be worthless for a huge database... so there must be a simpler way to go about this?

Thanks!!!!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我纯我任性 2024-09-14 03:43:38

请参阅 codeguru 论坛帖子 - 页面中的最后一个答案提供了用于将整个 Access 数据库加载到数据集中的函数:

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

See this codeguru forum post - the last answer in the page provides a function for loading the whole access database into a dataset:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文