【发布时间】:2015-05-01 17:25:17
【问题描述】:
我正在 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
【问题讨论】:
-
为什么不对所有用户使用
ODBC数据源? -
或者,如果通过网络(同一域),将其放在共享文件夹中并使用 UNC 路径。
-
谢谢!我对 Vb.net 很陌生,所以我不知道我能做到这一点。谢谢你的正确方向!
标签: database vb.net database-connection localserver