ผมยกตัวอย่าง DataBase ของ MS Access ให้ดูครับ เผื่อคุณจะเข้าใจได้ไม่ยากนัก
(ในชีวิตจริงผมไม่ค่อยได้ใช้งานครับ MS Access)
Code (VB.NET)
Public Class yourClass
Public Shared DBPassword As String = "212224236"
Public Shared DBName As String = "yourFolder/yourDB.mdb"
Shared ConnStr As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + DBName & ";Jet OLEDB:Database Password=" & DBPassword & ""
Sub test()
Dim dt = DBSelect(100)
Dim dr() = DTSelect("สมชาย", dt)
For Each x As DataRow In dr
MsgBox(x.Item("ColumnNameFirstName"))
Next
End Sub
Public Shared Function DBSelect(ByVal nRows As Integer) As DataTable
Dim ret As New DataTable()
Using cn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConnStr)
Using da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter("Select TOP " & nRows & ", * From yourTable", cn)
da.Fill(ret)
End Using
End Using
Return ret
End Function
Public Shared Function DTSelect(ByVal strFind As String, ByVal dt As DataTable) As System.Data.DataRow()
Dim dr() = dt.Select("ColumnNameFirstName Like '" & strFind & "%'", Nothing)
Return dr
End Function
End Class