Imports System.Data
Imports System.Data.OleDb
Public Class Database
Public connection As New OleDbConnection
Public connect As String
Public cmd As New OleDbCommand
Public adapter As New OleDbDataAdapter
Public data As New DataSet
Public sql As String
Public Sub New()
connect = "Provider=Microsoft.Ace.OleDb.12.0;" & "Data Source=score.accdb"
connection = New OleDbConnection(connect)
connection.Open()
End Sub
End Class
Class ครับ
Code (VB.NET)
Public Class Main
' Public con As Database
Dim connection As New OleDbConnection
Dim cmd As New OleDbCommand
Dim adapter As New OleDbDataAdapter
Dim data As New DataSet
Dim sql As String
'Sub connectdatabase() 'ฟังก์ชั่นย่อยใช้ในงานเชื่อมต่อฐานข้อมูล
' Dim connect As String = "Provider=Microsoft.Ace.OleDb.12.0;" & "Data Source=score.accdb"
' connection = New OleDbConnection(connect)
' connection.Open()
'End Sub
Sub loaddata()
sql = "Select * from Student"
cmd = New OleDbCommand(sql, connection)
adapter = New OleDbDataAdapter(cmd)
data = New DataSet()
adapter.Fill(data, "Student")
End Sub
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim database As New Database
' connectdatabase()
loaddata()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Idstudent.Text = data.Tables("Student").Rows(0)("Idstudent")
connection.Close()
End Sub
End Class