vb.net 当源文件路径更改时,如何保持与我的项目的数据库连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17996907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I retain database connectivity, with my project, when the source file path changes?
提问by aarviii
I am using a Microsoft Access Databasein my project; saved to the bin folder. What can I do, to ensure connectivity to that database, when the file path changes?
我在我的项目中使用Microsoft Access 数据库;保存到 bin 文件夹。当文件路径更改时,我该怎么做才能确保与该数据库的连接?
Imports System.Data.OleDb Public Class Form3
Dim con As New OleDb.OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As New DataSet
Dim str1 As String
导入 System.Data.OleDb 公共类 Form3
Dim con 作为新的 OleDb.OleDbConnection
Dim da As OleDbDataAdapter
Dim ds 作为新数据集
将 str1 调暗为字符串
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\AARVIII\Documents\DATABASE\MP1.accdb"
回答by Steve
Your connection string locates your database in a fixed position valid only on your PC.
A simple workaround is to use the |DataDirectory|substitution string.
您的连接字符串将您的数据库定位在仅在您的 PC 上有效的固定位置。
一个简单的解决方法是使用|DataDirectory| 替换字符串。
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=|DataDirectory|\MP1.accdb"
in this way, you can control the location of your database through code.
Usually (for a desktop application) the |DataDirectory|substitution string points the same folder where you have installed your application, but you need to have permission to write there and any kind of active database requires write permissions on its files. So this is not the best location for database files.
这样你就可以通过代码来控制你的数据库的位置了。
通常(对于桌面应用程序)|DataDirectory| 替换字符串指向您安装应用程序的同一个文件夹,但您需要有在那里写入的权限,并且任何类型的活动数据库都需要对其文件的写入权限。所以这不是数据库文件的最佳位置。
However you could change the location pointed by DataDirectory using code like this. (Of course put it BEFORE any attempt to talk to the database)
但是,您可以使用这样的代码更改 DataDirectory 指向的位置。(当然把它放在任何尝试与数据库交谈之前)
' Prepare a string pointing to a subfolder of the common application data
Dim appFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
Dim dbFolder = Path.Combine(appFolder, "MyAppFolder")
' Create the folder if it doesn't exist.
Directory.CreateDirectory(dbFolder)
' Change the substitution string kept by DataDirectory
AppDomain.CurrentDomain.SetData("DataDirectory", dbFolder)
Now the target directory for your database will be C:\programdata\myappfolder where your application has read/write permissions
现在,您的数据库的目标目录将是 C:\programdata\myappfolder,您的应用程序具有读/写权限
More info on DataDirectory
有关数据目录的更多信息
回答by Lectere
Yea, you should use:
是的,你应该使用:
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "MP1.accdb"
And have the database file in the same folder as you startup .exe...
并将数据库文件放在与启动 .exe 相同的文件夹中...
回答by nice.atma
I use this simple code and I can move the folder any where
我使用这个简单的代码,我可以将文件夹移动到任何位置
conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\myDatabase.mdb"
Make sure to save the access file in the bin folder for this connection string to work.
确保将访问文件保存在 bin 文件夹中,以便此连接字符串工作。

