ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 6232|ตอบกลับ: 0

[VB6] การเชื่อมต่อฐานข้อมูล MySQL Server ผ่าน ODBC และทำการแสดงผลข้อมูล

[คัดลอกลิงก์]

311

กระทู้

502

โพสต์

6052

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6052



การเขียนโปรแกรมด้วย 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 (คลิ๊กดาวน์โหลดไฟล์ติดตั้งได้ที่นี่)

อนึ่ง!!! แอดมินเลือกใช้ MySQL Server 5.7 เพราะมีปัญหาในการ Config กับ MySQL Server เวอร์ชั่น 8 ครับผม

หน้าตาของ MySQL Workbench (ดาวน์โหลดไฟล์ Sakila.sql ได้ที่นี่)


ขั้นตอนของการตั้งค่า DSN (Data Source Name) ... โดยเครื่องแอดมินลงทั้ง ODBC ขนาด 32/64 บิท
- กรณี 32 บิท ไปที่ Run พิมพ์คำสั่ง %Windir%\SysWOW64\odbcad32.exe
- กรณี 64 บิท ไปที่ Run พิมพ์คำสั่ง %Windir%\System32\odbcad32.exe

เลือก Add ...


เลือก MySQL ODBC 5.3 Unicode Driver ...


กรอกข้อมูลรายละเอียดในการเชื่อมต่อ MySQL Server ผ่านทาง ODBC และลองกดปุ่ม Test ...

(สำหรับ DataBase มันจะแสดงรายการขึ้นมาก็ต่อเมื่อคุณได้ทำการสร้างเอาไว้ใน MySQL Server เรียบร้อยแล้วครับ)

เมื่อกดปุ่ม OK ก็จะแสดง DSN ตัวใหม่ให้เห็น ...


มาดูโค้ดกันเถอะ ...
  1. ' / -----------------------------------------------------------------------------------------------
  2. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  3. ' / eMail : thongkorn@hotmail.com
  4. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  5. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand only)
  6. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  7. ' / MORE: http://www.g2gnet.com/webboard
  8. ' /
  9. ' / Purpose: Sample VB6 connect MySQL DataBase with ODBC.
  10. ' / Microsoft Visual Basic 6.0 Service Pack 6
  11. ' /
  12. ' / This is open source code under @Copyleft by Thongkorn Tubtimkrob.
  13. ' / You can modify and/or distribute without to inform the developer.
  14. ' / -----------------------------------------------------------------------------------------------

  15. Option Explicit
  16. Dim ConnDB As ADODB.Connection
  17. Dim RS As ADODB.Recordset
  18. Dim Statement As String

  19. ' / -----------------------------------------------------------------------------------------------
  20. Private Sub Form_Load()
  21. ' / -----------------------------------------------------------------------------------------------
  22.     ' Center screen with coding
  23.     Me.Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2
  24.     ' Initial ListView control with run time (no design time)
  25.     Call SetupListView
  26.     '// Connect MySQL via ODBC
  27.     'ConnectMySQL(Server, DataBase Name, Username, Password)
  28.     '// อย่าลืมใส่ชื่อ Username และ Password ด้วย
  29.     Call ConnectMySQL("localhost", "sakila", "Username", "Password")
  30. End Sub

  31. ' / -----------------------------------------------------------------------------------------------
  32. Private Sub ConnectMySQL(Server As String, DB As String, Username As String, Password As String)
  33. ' / -----------------------------------------------------------------------------------------------
  34.     On Error GoTo ErrHandler

  35.     '// Open a Connection.
  36.     Set ConnDB = New ADODB.Connection
  37.     ConnDB.ConnectionString = _
  38.                     " DRIVER = {MySQL ODBC 5.3 Driver};" & _
  39.                     " SERVER = " & Server & ";" & _
  40.                     " DATA SOURCE = " & DB & ";" & _
  41.                     " USER = " & Username & ";" & _
  42.                     " PASSWORD = " & Password & ";" & _
  43.                     " OPTION=3;"
  44.     ConnDB.Open
  45.     Exit Sub
  46.    
  47. ErrHandler:
  48.     MsgBox "Error : " & Err.Number & " " & Err.Description
  49.     End
  50. End Sub

  51. ' / -----------------------------------------------------------------------------------------------
  52. Private Sub cmdListData_Click()
  53. ' / -----------------------------------------------------------------------------------------------
  54.     On Error GoTo ErrHandler

  55.     '/ ตัดการเชื่อมต่อ RecordSet เดิมทิ้งออกไป
  56.     Set RS = New ADODB.Recordset
  57.     '/ หรือสั้นๆ
  58.     'Set RS = New Recordset
  59.     Statement = "Select * From Customer ORDER BY customer_id "
  60.     RS.CursorLocation = adUseClient
  61.     RS.Open Statement, ConnDB, adOpenForwardOnly, adLockReadOnly, adCmdText
  62.     'MsgBox RS.RecordCount
  63.    
  64.     Dim LV As ListItem
  65.     lvwData.ListItems.Clear
  66.     ' Populate the ListView with the data
  67.     Dim i As Long
  68.     For i = 0 To RS.RecordCount - 1
  69.         ' Index = 0 it's customer_id but hidden this field
  70.         Set LV = lvwData.ListItems.Add(, , RS("customer_id"))
  71.         LV.SubItems(1) = "" & RS("first_name")
  72.         LV.SubItems(2) = "" & RS("last_name")
  73.         LV.SubItems(3) = "" & RS("email")
  74.         ' next record
  75.         RS.MoveNext
  76.     Next

  77.     RS.Close:    Set RS = Nothing
  78.     'ConnDB.Close:    Set ConnDB = Nothing

  79.     Exit Sub

  80. ErrHandler:
  81.     MsgBox "Error " & Err.Number & " (" & Err.Description & ")"
  82. End Sub

  83. Public Sub CloseDataBase()
  84.     ' ตรวจสอบว่ามีการเชื่อมโยง - ConnDB ข้อมูลหรือไม่
  85.     If ConnDB.State = adStateOpen Then
  86.         ConnDB.Close
  87.         Set ConnDB = Nothing
  88.     End If
  89. End Sub

  90. ' Initial ListView
  91. Sub SetupListView()
  92.     With Me.lvwData
  93.         ' Coding with Run Time
  94.         .View = lvwReport
  95.         .Arrange = lvwNone
  96.         .LabelEdit = lvwManual
  97.         .BorderStyle = ccFixedSingle
  98.         .Appearance = cc3D
  99.         
  100.         .HideColumnHeaders = False
  101.         .HideSelection = False
  102.         .LabelWrap = False
  103.         .MultiSelect = False
  104.         .Enabled = True
  105.         .AllowColumnReorder = True
  106.         .Checkboxes = False
  107.         .FlatScrollBar = False
  108.         .FullRowSelect = True
  109.         .GridLines = True
  110.         .HotTracking = False
  111.         .HoverSelection = False
  112.         
  113.         .Sorted = False
  114.         .SortKey = 0
  115.         '.SortOrder=lvwAscending
  116.         
  117.         ' Add header
  118.         .ColumnHeaders.Add , , "CustomerID", .Width = 0
  119.         .ColumnHeaders.Add , , "Firstname", .Width \ 3, lvwColumnLeft
  120.         .ColumnHeaders.Add , , "Lastname", .Width \ 3 - 200
  121.         .ColumnHeaders.Add , , "Email", .Width \ 3 - 200
  122.     End With
  123.    
  124. End Sub

  125. Private Sub Form_Resize()
  126.     On Error Resume Next
  127.     Me.lvwData.Move 0, 0, Me.ScaleWidth - Me.cmdListData.Width - 150, Me.ScaleHeight - 60
  128.     Me.cmdListData.Left = Me.lvwData.Width + 60
  129.     Me.cmdExit.Move Me.cmdListData.Left
  130.     '//
  131.     With Me.lvwData
  132.         .ColumnHeaders(1).Width = 0
  133.         .ColumnHeaders(2).Width = .Width \ 3
  134.         .ColumnHeaders(3).Width = .Width \ 3 - 200
  135.         .ColumnHeaders(4).Width = .Width \ 3 - 200
  136.     End With
  137. End Sub

  138. ' / Double click mouse for get Primary Key
  139. Private Sub lvwData_DblClick()
  140.     If lvwData.ListItems.Count <= 0 Then Exit Sub
  141.     MsgBox "Primary Key is : " & lvwData.SelectedItem.Text
  142. End Sub

  143. Private Sub Form_Unload(Cancel As Integer)
  144.     Call CloseDataBase
  145.     Set frmConnectMySQL = Nothing
  146.     End
  147. End Sub

  148. Private Sub cmdExit_Click()
  149.     Unload Me
  150. End Sub
คัดลอกไปที่คลิปบอร์ด


ดาวน์โหลดโค้ดต้นฉบับแบบเต็ม VB6 ได้ที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-26 09:16 , Processed in 0.155690 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้