|
สำหรับ VB6 ในการเชื่อมต่อกับฐานข้อมูล MSSQL SERVER เราเปลี่ยนแค่ Connection String หรือ Provider เท่านั้นครับ ที่เหลือในการ Insert/Update หรือ Delete ก็ใช้โค้ดเหมือนการใช้งานกับ MS ACCESS ได้เลย แต่อาจจะมีปรับแต่งเล็กน้อยในเรื่อง Query ครับ ...
โค้ดในการเชื่อมต่อกับ MSSQL Server ด้วย VB6 (การ Reference ใช้ ADODB 2.80 เหมือนใช้กับ MS Access)
- Option Explicit
- Dim ConnDB As New ADODB.Connection
- Private Sub Form_Load()
- On Error GoTo ErrorHandler
- ' Center screen with coding
- Me.Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2
-
- ' Open a connection.
- Set ConnDB = New ADODB.Connection
- ConnDB.ConnectionString = _
- " Provider=SQLOLEDB.1;" & _
- " Data Source=Thongkorn-PC\G2GNET2017; " & _
- " Initial Catalog=nwind; " & _
- " User ID=; " & _
- " Password=; " & _
- " Trusted_Connection=yes; "
- ConnDB.Properties("Prompt") = adPromptAlways
- ConnDB.Open
-
- If ConnDB.State = adStateOpen Then
- ConnDB.Close
- Set ConnDB = Nothing
- End If
- MsgBox "คุณสามารถเชื่อมต่อฐานข้อมูล MS SQL.", vbOKOnly + vbInformation, "รายงานสถานะ"
-
- ExitProc:
- Exit Sub
- ErrorHandler:
- Select Case Err.Number
- ' ดัก Trap Error ของการกดปุ่ม Cancel
- Case -2147217842
- End
-
- Case Else
- MsgBox "Open Database Error : " & vbCrLf & Err.Number & " " & Err.Description
- End
- End Select
- End Sub
คัดลอกไปที่คลิปบอร์ด
|
-
|