使用 Vb.Net 从 MS Access 数据库视图检索行

发布于 2024-11-19 04:54:01 字数 487 浏览 2 评论 0原文

我已设法获得以下代码...

            con.ConnectionString = My.Settings.dbConnection
        Dim sqlCmd As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand()
        con.Open()
        sqlCmd.Connection = con

        Dim schemaTable As DataTable
        schemaTable = con.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Views, Nothing)

检索 Access 数据库中的视图列表,但现在我想根据选定的视图检索结果。

是否有正确的方法来执行此操作,或者我是否从为每行返回的 DataTable 中获取 SQL 语句?

I've managed to get the following code...

            con.ConnectionString = My.Settings.dbConnection
        Dim sqlCmd As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand()
        con.Open()
        sqlCmd.Connection = con

        Dim schemaTable As DataTable
        schemaTable = con.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Views, Nothing)

To retrieve a list of Views in my Access database, but now I want to retrieve the results based on a selected View.

Is there a correct method in doing this, or do I take the SQL Statement from the DataTable returned for each row?

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

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

发布评论

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

评论(1

渔村楼浪 2024-11-26 04:54:01

假设您的 Access 数据库(Database1.accdb 文件)中有 Query1(视图)。以下代码将把查询的每一行输出到控制台(用于演示目的):

    Dim con As OleDbConnection = New OleDbConnection()

    con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Database1.accdb;Persist Security Info=False;"
    Dim sqlCmd As OleDbCommand = New System.Data.OleDb.OleDbCommand()

    sqlCmd.CommandType = CommandType.StoredProcedure
    sqlCmd.CommandText = "Query1"
    sqlCmd.Connection = con

    con.Open()

    Dim reader As OleDbDataReader

    reader = sqlCmd.ExecuteReader()

    If reader.HasRows Then
        While reader.Read()
            Console.WriteLine(reader("Column1")) 'output specific column
        End While
    End If

    Console.ReadLine()

希望这有帮助

Suppose you have Query1 (View) in your Access database (Database1.accdb file). The following code will output each row of the query to the console (for demo purposes):

    Dim con As OleDbConnection = New OleDbConnection()

    con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Database1.accdb;Persist Security Info=False;"
    Dim sqlCmd As OleDbCommand = New System.Data.OleDb.OleDbCommand()

    sqlCmd.CommandType = CommandType.StoredProcedure
    sqlCmd.CommandText = "Query1"
    sqlCmd.Connection = con

    con.Open()

    Dim reader As OleDbDataReader

    reader = sqlCmd.ExecuteReader()

    If reader.HasRows Then
        While reader.Read()
            Console.WriteLine(reader("Column1")) 'output specific column
        End While
    End If

    Console.ReadLine()

Hope this helps

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