2020年5月8日 星期五

VB.NET 連結Access並顯示在DataGridView(DataTable) [Connect Access with DataGridView]


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Imports System.Data

Imports System.Data.OleDb


Public Class Form1


    Dim str As String

    Dim conn As New OleDb.OleDbConnection

    Dim ds As New DataTable

    Dim adp As New OleDbDataAdapter

    Dim cmd As New OleDbCommand


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\DataBase.mdb;Jet OLEDB:Database Password=8AFE2B7801749F02C649"

        conn = New OleDbConnection(str) '創建連線物件

        Try

            conn.Open() '打開連接

            MessageBox.Show("已經正確建立連接!", "連接正確")

        Catch ex As Exception

            MessageBox.Show(ex.Message, "連接錯誤")

            Exit Sub

        End Try


        adp = New OleDbDataAdapter("select * from Bank", conn)

        adp.Fill(ds)

        DataGridView1.DataSource = ds

        conn.Close()

    End Sub

End Class

沒有留言:

張貼留言