可分為兩種,一種是取得資料庫裡面的資料
' 資料庫的路徑
Dim strDbCon As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=coolking_music.mdb"
Dim objCon As OleDbConnection = New OleDbConnection(strDbCon)
objCon.Open()
' SQL語法
Dim objCmd As OleDbCommand = New OleDbCommand("SELECT * FROM song", objCon)
Dim objDataReader As OleDbDataReader = objCmd.ExecuteReader()
Dim out As String
' 讀取singer欄位的值
While objDataReader.Read()
out += objDataReader.Item("singer")
End While
' 關閉資料庫的連結
objDataReader.Close()
objCon.Close()
Label1.Text = out
另一種是顯示在DataGridView上
' 資料庫的路徑
Dim strDbCon As String = "Provider=Microsoft.Jet.Oledb.4.0;Data source=coolking_music.mdb"
Dim objCon As OleDbConnection = New OleDbConnection(strDbCon)
objCon.Open()
' SQL語法
Dim adp1 As OleDbDataAdapter = New OleDbDataAdapter("select * from song", objCon)
' 將查詢結果放到記憶體set1上的"song"表格內
Dim set1 As DataSet = New DataSet
adp1.Fill(set1, "song")
' 將記憶體的資料集合存放到視窗畫面上的DataGridView上
DataGridView1.DataSource = set1.Tables("song")
' 關閉資料庫的連結
objCon.Close()
沒有留言:
張貼留言