การเชื่อมต่อระบบฐานข้อมูล MS SQL Server
โดยทั่ว ๆไปเมื่อเราทดสอบการเชื่อมต่อฐานข้อมูล MS SQL Server อยู่ที่เครื่องที่เราใช้งานเอง (Local Machine) ก็มักจะไม่ค่อยพบปัญหาเท่าใดนัก แต่พอข้ามเครื่อง (Network) ทีไรก็จะเจอปัญหาทุกที่ ซึ่งส่วนใหญ่ก็น่าจะเป็นเรื่องของ Security การใช้งานโปรแกรม MS Visual basic 6 ไม่จำเป็นต้องมีฟอร์มเลยก็ได้ โดยที่เราสามารถสั่งให้ Startup object ได้เลย ที่ Sub Main ซึ่งเจ้า Sub Main ที่ว่ามานี้ โดยปกติเราจะนำไปวางไว้ที่ Module (นามสกุล .BAS)
การออกแบบโปรแกรมให้มีโครงสร้าง จะช่วยลดปัญหาลงไปได้เยอะ'พร้อมๆกับทำให้โปรแกรมเมอร์อย่างมีเวลาไปเล่นเกมส์ได้อีก การทดสอบกับเครื่อง Local Machine
'โค้ดเหล่านี้ถูกจัดเก็บใน Module
Option Explicit
Global ConnDB As New ADODB.Connection
'Global RS As New ADODB.Recordset
'Global DS As New ADODB.Recordset
'Global RstData As New ADODB.Recordset ' ใช้ในรายงาน
'Global Statement As String
'Global SQLStmt As String
'
Sub Main()
' ทดสอบการเชื่อมต่อฐานข้อมูล
Call OpenDataBase
' เปิดแล้วก็ปิด ... เพราะต้องการแค่ทดสอบ
Call CloseDataBase
End Sub
Public Sub OpenDataBase()
On Error GoTo Err_Handler
' Open a connection.
Set ConnDB = New ADODB.Connection
' จัดรูปแบบให้สวยงาม อ่านง่ายๆ
' ใช้ฐานข้อมูลตัวอย่าง NorthWind เป็นตัวทดสอบ
ConnDB.ConnectionString = _
" Provider=SQLOLEDB.1;" & _
" Integrated Security=SSPI;" & _
" Server=(local);" & _
" Initial Catalog=NorthWind;" & _
" User ID=;" & _
" Password=;"
ConnDB.Open
MsgBox "การเชื่อมต่อกับฐานข้อมูลเรียบร้อย.", vbOKOnly + vbInformation, "รายงานสถานะ"
ExitProc:
Exit Sub
Err_Handler:
MsgBox "Open Database Error : " & vbCrLf & Err.Number & " " & Err.Description
Resume
End Sub
Public Sub CloseDataBase()
' ตรวจสอบว่ามีการเชื่อมโยง - Connect ข้อมูลหรือไม่
If ConnDB.State = adStateOpen Then
ConnDB.Close
Set ConnDB = Nothing
End If
End Sub
' การทดสอบกับระบบเครือข่ายโดยใช้ MS Windows XP เป็น Server
' ก็แค่เปลี่ยนชื่อ Server ที่เราต้องการจะติดต่อกับมัน ... เท่านั้นเอง
Public Sub OpenDataBase()
On Error GoTo Err_Handler
Set ConnDB = New ADODB.Connection
ConnDB.ConnectionString = _
" Provider=SQLOLEDB.1;" & _
" Integrated Security=SSPI;" & _
" Server=Server-XP;" & _
" Initial Catalog=NorthWind;" & _
" User ID=;" & _
" Password=;"
ConnDB.Open
MsgBox "การเชื่อมต่อกับฐานข้อมูลเรียบร้อย.", vbOKOnly + vbInformation, "รายงานสถานะ"
ExitProc:
Exit Sub
Err_Handler:
MsgBox "Open Database Error : " & vbCrLf & Err.Number & " " & Err.Description
Resume
End Sub
การใช้คุณสมบัติ adPromptAlways เพื่อสร้างทางเลือกของการเชื่อมต่อฐานข้อมูล
Public Sub OpenDataBase()
On Error GoTo Err_Handler
Set ConnDB = New ADODB.Connection
ConnDB.ConnectionString = _
" Provider=SQLOLEDB.1;" & _
" Integrated Security=SSPI;" & _
" Server=Server-XP;" & _
" Initial Catalog=NorthWind;" & _
" User ID=;" & _
" Password=;"
ConnDB.Properties("Prompt") = adPromptAlways
ConnDB.Open
' MsgBox "การเชื่อมต่อกับฐานข้อมูลเรียบร้อย.", vbOKOnly + vbInformation, "รายงานสถานะ"
ExitProc:
Exit Sub
Err_Handler:
Select Case Err.Number
' ดัก Trap Error ของการกดปุ่ม Cancel
Case -2147217842
' จากนั้นสั่งให้ Resume หรือ ทำงานต่อไปได้เลย
Resume
Case Else
MsgBox "Open Database Error : " & vbCrLf & Err.Number & " " & Err.Description
Resume
End Select
End Sub
ไม่มีความคิดเห็น:
แสดงความคิดเห็น