พี่ครับผมได้ลองทำตามโค้ดจากหน้านี้แล้ว https://www.thaicreate.com/asp.net/asp.net-ms-access-database-class.html
ปรากฏว่าขึ้นเออเรออ่ะครับ ในส่วนของ class
Imports System.Data
Imports System.Data.OleDb
Imports System.Configuration
Public Class database
Private con As OleDbConnection
Private cmd As OleDbCommand
Private strconstring As String
Public Sub New()
strconstring = System.Configuration.ConfigurationManager.ConnectionStrings("Chase.My.MySettings.chaseConnectionString").ConnectionString
End Sub
Public Function querydataset(ByVal strsql As String) As DataSet
Dim ds As DataSet
Dim da As New OleDbDataAdapter
con = New OleDbConnection
With con
.ConnectionString = strconstring
.Open()
End With
cmd = New OleDbCommand
With cmd
.Connection = con
.CommandType = strsql
.CommandType = CommandType.Text
End With
da.SelectCommand = cmd
da.Fill(ds)
Return ds
End Function
Public Function queryexecute(ByVal strsql As String) As Boolean
con = New OleDbConnection
With con
.ConnectionString = strconstring
.Open()
End With
Try
cmd = New OleDbCommand
With cmd
.Connection = con
.CommandType = CommandType.Text
.CommandText = strsql
End With
cmd.ExecuteNonQuery()
Return True
Catch ex As Exception
Return False
End Try
End Function
End Class
จาก Form
Imports System.Data
Imports System.Data.OleDb
Public Class Form5
Dim clsdb As New database
Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call mydataset()
End Sub
Sub mydataset()
Dim strsql As String = "select * from staff "
Dim ds As DataSet
ds = clsdb.querydataset(strsql)
Me.DataGrid1.DataSource = ds.Tables(0)
End Sub
End Class
dim sql as string = "select * from staff "
dim ds as new dataset
dim da as new oleDbDataAdapter
da=new oleDbDataAdapter(sql,con)
da.fill(ds)
datagridview1.datasource=ds.tables("s")
'*** BindData From Database **'
Dim objConn As OleDbConnection
Dim dtAdapter As OleDbDataAdapter
Dim dt As New DataTable
Dim strConnString As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mydatabase.mdb"
objConn = New OleDbConnection(strConnString)
objConn.Open()
Dim strSQL As String
strSQL = "SELECT * FROM customer WHERE 1=1 "
If strKeyWord <> "" Then
strSQL = strSQL & " AND Name Like '%" & strKeyWord & "%' "
End If
dtAdapter = New OleDbDataAdapter(strSQL, objConn)
dtAdapter.Fill(dt)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
Me.myDgv.DataSource = dt