VB.Netでmdbファイルへ接続
環境:VB.Net2008
ツールボックスからButton1を追加しておく
C:\SampleDBにSampleDB.mdbを用意し
Test_Tableテーブル作っておく
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oleCon As New OleDb.OleDbConnection()
Dim oleDataAd As OleDb.OleDbDataAdapter
Dim dtSet As New DataSet()
Dim dtTable As DataTable
Dim strSql As String
'接続文字列を作成
oleCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source= C:\SampleDB\SampleDB.mdb;" _
& "Persist Security Info=False"
'SQL文を作成
strSql = "Select * from Test_Table"
'レコードを取得
oleDataAd = New OleDb.OleDbDataAdapter(strSql, oleCon)
oleDataAd.Fill(dtSet, "Test_Table")
'データセットからテーブルを取得
dtTable = dtSet.Tables("Test_Table")
'結果の表示
Dim iRowCount As Integer
Dim iColCount As Integer
For iRowCount = 0 To dtTable.Rows.Count - 1
For iColCount = 0 To dtTable.Columns.Count - 1
Debug.Write(dtTable.Rows(iRowCount)(iColCount) & ControlChars.Tab)
Next
Debug.Write(ControlChars.NewLine)
Next
End Sub