Private Sub frmConnectDB_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' ปรับระยะของฟอร์มให้อยู่ตรงกลางหน้าจอ
Me.Left = (Screen.PrimaryScreen.WorkingArea.Width - Me.Width) \ 2
Me.Top = (Screen.PrimaryScreen.WorkingArea.Height - Me.Height) \ 2
Call ConnectAccess()
Application.Exit()
End Sub
Public Sub ConnectAccess()
Dim strPath As String = Environment.CurrentDirectory
Dim i As Integer
Dim CountBackSlash As Integer
' พวกนี้อะไรบ้างก็ไม่รู้ ... งงกะชีวิตของ .NET มันซ่ะจริงๆ
'MsgBox(Application.ExecutablePath)
'MsgBox(Application.StartupPath)
'MsgBox(Environment.CurrentDirectory())
'MsgBox(CurDir)
'MsgBox(System.Reflection.Assembly.GetExecutingAssembly.Location)
MsgBox(System.Windows.Forms.Application.StartupPath)
' strPath= "G:\Project VB.Net\ConnectAccessNET\ConnectAccessNET\bin\Debug"
' Algorithm หรือ แนวคิดแก้ปัญหาแบบง่ายๆ
' ให้นับเครื่องหมาย BackSlash ( \ ) มาจากด้านขวามือสุด ให้ได้ 3 ตัว
' พอได้ครบ 3 ตัว ให้อ่านจากทางซ้ายไปขวา จนถึงจำนวนของ i ที่นับถอยหลังลงมา (จะรวมทั้ง \ ด้วย)
' ซึ่งก็จะทำให้ได้ค่าเป็น ...
' strPath = "G:\Project VB.Net\ConnectAccessNET\"
' ส่วนข้อมูลจะอยู่ที่ ...
' strPath = "G:\Project VB.Net\ConnectAccessNET\Data\MyDB.MDB"
For i = Len(strPath) To 1 Step -1
If Mid(strPath, i, 1) = "\" Then CountBackSlash = CountBackSlash + 1
If CountBackSlash = 3 Then
strPath = Mid(strPath, 1, i)
Exit For
End If
Next
'MsgBox(strPath)
' มันมีอีกหลายวิธีเลยน่ะครับตรึมเลย ... ลองไปคิดๆดูเอาเองล่ะกัน
' ให้ต่อท้ายด้วยโฟลเดอร์ตำแหน่งของข้อมูลที่ต้องการ คือ \Data\ไฟล์ข้อมูล MS Access.MDB
Dim strConn As String = _
"Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source = " & strPath & "Data\MyDB.mdb"
' เปิดการเชื่อมต่อไฟล์ฐานข้อมูล
Dim ConnDB As OleDb.OleDbConnection = New OleDb.OleDbConnection(strConn)
ConnDB.Open()
MsgBox("การเชื่อมต่อ MS Access - " & ConnDB.State.ToString)
'ConnDB.Close()
End Sub
|