当前位置:数据库 > Access >>

VB.Net程序设计:ADO.Net读取Access表架构

程序只是记录而已,还没有完成。
请不要模仿。
Imports System.Data 
Imports System.Data.OleDb 
 
Public Class Form1 
 
    Dim ConnStr As String = String.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}; User Id=admin; Password={1};", "D:\product.MDB", "") 
    Dim conn As OleDb.OleDbConnection 
    Dim cmd As OleDb.OleDbCommand 
    Dim sql As String
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
        'cmd = New OleDbCommand(sql, conn) 
        'cmd.ExecuteNonQuery() 
        conn = New OleDb.OleDbConnection(ConnStr) 
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
        Dim dt As DataTable 
        Dim tbl As DataTable 
        Dim tbl2 As DataTable 
 
        conn.Open() 
        'tbl = conn.GetSchema() =数据库的所有架构信息。 
        '获取用户表 
        tbl = conn.GetSchema("tables", New String() {Nothing, Nothing, Nothing, "table"}) 
        'tbl = conn.GetSchema("tables") = 所有表,包括系统表。 
        tbl2 = conn.GetSchema() 
        '=不大支持的方法 
        dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"}) 
 
        Me.ListBox1.Items.Clear() 
        For i As Integer = 0 To dt.Rows.Count - 1 
            Me.ListBox1.Items.Add(dt.Rows(i)("TABLE_NAME").ToString()) 
        Next
 
        Me.DataGridView1.DataSource = tbl 
        Me.DataGridView2.DataSource = tbl2 
        conn.Close() 
 
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
        Dim tbl As DataTable 
        If String.IsNullOrEmpty(Me.TextBox1.Text) = False Then
            conn.Open() 
            tbl = conn.GetSchema("columns", New String() {Nothing, Nothing, Me.TextBox1.Text}) 
            Dim rows() As DataRow = tbl.Select("", "Ordinal_Position") 
            Me.ListBox2.Items.Clear() 
            For Each row As DataRow In rows 
                Me.ListBox2.Items.Add(row.Item("column_name")) 
            Next
            Me.DataGridView2.DataSource = tbl 
            conn.Close() 
        End If
 
    End Sub
 
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged 
        If Me.ListBox1.SelectedItems.Count > 0 Then
            Me.TextBox1.Text = Me.ListBox1.SelectedItem.ToString 
        End If
    End Sub
 
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnGetDatatable.Click 
        Dim db As New AccessHelper 
        db.connStr = ConnStr 
        If Me.TextBox1.Text > "" Then
            db.Open() 
            Me.DataGridView3.DataSource = db.GetDataView("select * from " & Me.TextBox1.Text) 
        End If
 
    End Sub
End Class

作者“程序记录集”

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,