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

时间:2011-07-11 10:06:35

标签: vb.net ms-access-2007

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

            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语句?

1 个答案:

答案 0 :(得分:-1)

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

    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()

希望这有帮助