vb.net連接ACCESS数据库

时间:2021-03-03 12:52:53

'導入命名空間
Imports System.Data.OleDb

'定義變量
Dim Sql As String
'OleDb連線
Dim SqlAC As OleDbConnection
Dim CmdAC As OleDbCommand
Dim drAC As OleDbDataReader

'打开OleDb連線
Public Sub OpenAC()
SqlAC = New OleDbConnection(" Provider = Microsoft.Jet.OLEDB.4.0;Data Source = DISK.mdb ")
If SqlAC.State = ConnectionState.Closed Then
SqlAC.Open()
End If
CmdAC = New OleDbCommand(Sql, SqlAC)
drAC = CmdAC.ExecuteReader()
End Sub

'关闭OleDb連線
Public Sub CloseAC()
drAC.Close()
If SqlAC.State = ConnectionState.Open Then
SqlAC.Close()
End If
End Sub

Private Sub A1()
Sql = ("SELECT A1 FROM T8 ")
OpenAC()
drAC.Read()
TextBox1.Text = drAC.Item(0)
CloseAC()
End Sub