我正在 vb.net 中构建一个 5 人将共享使用的数据输入程序,但我在设置正确的数据库连接时遇到了问题。它会做一些基本的事情,比如:拉起库存单位,保存工作,加载工作操作。
我使用的数据库是 Access 数据库 (.mdb)。该数据库将位于本地服务器驱动器中(我的位于 Z 驱动器中),连接字符串如下所示
Provider=Microsoft.Jet.OLEDB.4.0;Data Source="Z:\Jimmy's Files\Quality Enclosures.mdb"
这在我的计算机上运行良好,但问题是它在我同事的计算机上不起作用。
d (\dc-qenclosures) (Z:) 是我本地服务器驱动器的位置,但在我同事的计算机上,它设置为 d (\dc-qenclosures) (Q:)。
因此,每当我在同事的计算机上打开程序时,它都会给我一个错误,说数据库Provider=Microsoft.Jet.OLEDB.4.0;Data Source="Z:\Jimmy's Files\Quality Enclosures.mdb"
不存在(这是有道理的,因为它不在他的计算机上的 Z: 下)
我知道如何使用OpenFileDialog
来浏览 mbd 文件。但如何将其设置为新的数据库连接?我想创建一个属性菜单来设置数据库位置。
这是我到目前为止浏览数据库文件的代码
Private Sub RadButton6_Click(sender As Object, e As EventArgs) Handles RadButton6.Click
Dim myStream As Stream = Nothing
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "mdb files (*.mdb)|*.mdb|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = openFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
myConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & openFileDialog1.FileName
con.ConnectionString = myConString
con.Open()
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
' Check this again, since we need to make sure we didn't throw an exception on open.
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
End If
End Sub