[VB6] การเชื่อมต่อฐานข้อมูล MySQL Server ผ่าน ODBC และทำการแสดงผลข้อมูล
http://www.g2gnet.com/webboard/images/vb6/mysql/mysqlvb6.pngการเขียนโปรแกรมด้วย VB6 เพื่อเชื่อมต่อฐานข้อมูล MySQL Server จะต้องทำผ่าน ODBC (Open DataBase Connectivity) ซึ่งจัดว่าเป็นการติดต่อข้อมูลที่โบราณที่สุด ช้าที่สุด หากเปรียบเทียบกับ DAO (Data Access Objects) และ ADO (ActiveX Data Objects) แต่นั่นมันคือทฤษฎี หรือคำนิยามที่กล่าวถึงกันเมื่อ 20 ปีก่อนนู้นนนนน เพราะในทุกวันนี้คอมพิวเตอร์สามารถทำงานและประมวลผลได้เร็วกว่าอดีตเยอะแยะมากมาย ... แอดมินจะอธิบายคร่าวๆให้สำหรับท่านที่ไม่เคยใช้ MySQL มาก่อนน่ะครับ คือเราต้องติดตั้ง MySQL Server เข้าไปก่อน (สายเว็บก็จะมี Xampp หรือตัวอื่นๆ) แต่สำหรับสาย Win App. แอดมินแนะนำให้ใช้ MySQL Installer ตัวเดียวไปเลย เพื่อทำการติดตั้งตัว MySQL Server และ Connector/ODBC ซึ่งมันเป็นตัวพูดคุยสื่อสารกันระหว่าง VB6 และ MySQL Server สำหรับ MySQL Workbench จะเป็นตัวคอยจัดการฐานข้อมูลของเราอีกที สำหรับรายละเอียดคงต้องไปหาศึกษาเองล่ะกันครับ ... (การสร้าง Schema และการรันสคริปท์ (SQL) ใน MySQL ด้วย MySQL Workbench)
การใช้ MySQL Installer เพื่อทำการติดตั้ง MySQL Server, MySQL Workbench และ Connector/ODBC ที่เราจะใช้สำหรับ VB6 (คลิ๊กดาวน์โหลดไฟล์ติดตั้งได้ที่นี่)
http://www.g2gnet.com/webboard/images/vb6/mysql/mysqlinstaller.png
อนึ่ง!!! แอดมินเลือกใช้ MySQL Server 5.7 เพราะมีปัญหาในการ Config กับ MySQL Server เวอร์ชั่น 8 ครับผม
หน้าตาของ MySQL Workbench (ดาวน์โหลดไฟล์ Sakila.sql ได้ที่นี่)
http://www.g2gnet.com/webboard/images/vb6/mysql/mysqlsakila.png
ขั้นตอนของการตั้งค่า DSN (Data Source Name) ... โดยเครื่องแอดมินลงทั้ง ODBC ขนาด 32/64 บิท
- กรณี 32 บิท ไปที่ Run พิมพ์คำสั่ง %Windir%\SysWOW64\odbcad32.exe
- กรณี 64 บิท ไปที่ Run พิมพ์คำสั่ง %Windir%\System32\odbcad32.exe
เลือก Add ...
http://www.g2gnet.com/webboard/images/vb6/mysql/mysqladddsn.png
เลือก MySQL ODBC 5.3 Unicode Driver ...
http://www.g2gnet.com/webboard/images/vb6/mysql/mysqlodbc.png
กรอกข้อมูลรายละเอียดในการเชื่อมต่อ MySQL Server ผ่านทาง ODBC และลองกดปุ่ม Test ...
http://www.g2gnet.com/webboard/images/vb6/mysql/mysqldsn.png
(สำหรับ DataBase มันจะแสดงรายการขึ้นมาก็ต่อเมื่อคุณได้ทำการสร้างเอาไว้ใน MySQL Server เรียบร้อยแล้วครับ)
เมื่อกดปุ่ม OK ก็จะแสดง DSN ตัวใหม่ให้เห็น ...
http://www.g2gnet.com/webboard/images/vb6/mysql/mysqladddsnend.png
มาดูโค้ดกันเถอะ ... ' / -----------------------------------------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: https://www.facebook.com/g2gnet (For Thailand only)
' / Facebook: https://www.facebook.com/commonindy (Worldwide)
' / MORE: http://www.g2gnet.com/webboard
' /
' / Purpose: Sample VB6 connect MySQL DataBase with ODBC.
' / Microsoft Visual Basic 6.0 Service Pack 6
' /
' / This is open source code under @Copyleft by Thongkorn Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / -----------------------------------------------------------------------------------------------
Option Explicit
Dim ConnDB As ADODB.Connection
Dim RS As ADODB.Recordset
Dim Statement As String
' / -----------------------------------------------------------------------------------------------
Private Sub Form_Load()
' / -----------------------------------------------------------------------------------------------
' Center screen with coding
Me.Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2
' Initial ListView control with run time (no design time)
Call SetupListView
'// Connect MySQL via ODBC
'ConnectMySQL(Server, DataBase Name, Username, Password)
'// อย่าลืมใส่ชื่อ Username และ Password ด้วย
Call ConnectMySQL("localhost", "sakila", "Username", "Password")
End Sub
' / -----------------------------------------------------------------------------------------------
Private Sub ConnectMySQL(Server As String, DB As String, Username As String, Password As String)
' / -----------------------------------------------------------------------------------------------
On Error GoTo ErrHandler
'// Open a Connection.
Set ConnDB = New ADODB.Connection
ConnDB.ConnectionString = _
" DRIVER = {MySQL ODBC 5.3 Driver};" & _
" SERVER = " & Server & ";" & _
" DATA SOURCE = " & DB & ";" & _
" USER = " & Username & ";" & _
" PASSWORD = " & Password & ";" & _
" OPTION=3;"
ConnDB.Open
Exit Sub
ErrHandler:
MsgBox "Error : " & Err.Number & " " & Err.Description
End
End Sub
' / -----------------------------------------------------------------------------------------------
Private Sub cmdListData_Click()
' / -----------------------------------------------------------------------------------------------
On Error GoTo ErrHandler
'/ ตัดการเชื่อมต่อ RecordSet เดิมทิ้งออกไป
Set RS = New ADODB.Recordset
'/ หรือสั้นๆ
'Set RS = New Recordset
Statement = "Select * From Customer ORDER BY customer_id "
RS.CursorLocation = adUseClient
RS.Open Statement, ConnDB, adOpenForwardOnly, adLockReadOnly, adCmdText
'MsgBox RS.RecordCount
Dim LV As ListItem
lvwData.ListItems.Clear
' Populate the ListView with the data
Dim i As Long
For i = 0 To RS.RecordCount - 1
' Index = 0 it's customer_id but hidden this field
Set LV = lvwData.ListItems.Add(, , RS("customer_id"))
LV.SubItems(1) = "" & RS("first_name")
LV.SubItems(2) = "" & RS("last_name")
LV.SubItems(3) = "" & RS("email")
' next record
RS.MoveNext
Next
RS.Close: Set RS = Nothing
'ConnDB.Close: Set ConnDB = Nothing
Exit Sub
ErrHandler:
MsgBox "Error " & Err.Number & " (" & Err.Description & ")"
End Sub
Public Sub CloseDataBase()
' ตรวจสอบว่ามีการเชื่อมโยง - ConnDB ข้อมูลหรือไม่
If ConnDB.State = adStateOpen Then
ConnDB.Close
Set ConnDB = Nothing
End If
End Sub
' Initial ListView
Sub SetupListView()
With Me.lvwData
' Coding with Run Time
.View = lvwReport
.Arrange = lvwNone
.LabelEdit = lvwManual
.BorderStyle = ccFixedSingle
.Appearance = cc3D
.HideColumnHeaders = False
.HideSelection = False
.LabelWrap = False
.MultiSelect = False
.Enabled = True
.AllowColumnReorder = True
.Checkboxes = False
.FlatScrollBar = False
.FullRowSelect = True
.GridLines = True
.HotTracking = False
.HoverSelection = False
.Sorted = False
.SortKey = 0
'.SortOrder=lvwAscending
' Add header
.ColumnHeaders.Add , , "CustomerID", .Width = 0
.ColumnHeaders.Add , , "Firstname", .Width \ 3, lvwColumnLeft
.ColumnHeaders.Add , , "Lastname", .Width \ 3 - 200
.ColumnHeaders.Add , , "Email", .Width \ 3 - 200
End With
End Sub
Private Sub Form_Resize()
On Error Resume Next
Me.lvwData.Move 0, 0, Me.ScaleWidth - Me.cmdListData.Width - 150, Me.ScaleHeight - 60
Me.cmdListData.Left = Me.lvwData.Width + 60
Me.cmdExit.Move Me.cmdListData.Left
'//
With Me.lvwData
.ColumnHeaders(1).Width = 0
.ColumnHeaders(2).Width = .Width \ 3
.ColumnHeaders(3).Width = .Width \ 3 - 200
.ColumnHeaders(4).Width = .Width \ 3 - 200
End With
End Sub
' / Double click mouse for get Primary Key
Private Sub lvwData_DblClick()
If lvwData.ListItems.Count <= 0 Then Exit Sub
MsgBox "Primary Key is : " & lvwData.SelectedItem.Text
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call CloseDataBase
Set frmConnectMySQL = Nothing
End
End Sub
Private Sub cmdExit_Click()
Unload Me
End Sub
ดาวน์โหลดโค้ดต้นฉบับแบบเต็ม VB6 ได้ที่นี่ ...
หน้า:
[1]