[英]How do I retain database connectivity, with my project, when the source file path changes?
I am using a Microsoft Access Database in my project; 我在我的项目中使用Microsoft Access数据库 。 saved to the bin folder.
保存到bin文件夹。 What can I do, to ensure connectivity to that database, when the file path changes?
当文件路径更改时,我该怎么做以确保与该数据库的连接?
Imports System.Data.OleDb Public Class Form3
导入System.Data.OleDb公共类Form3
Dim con As New OleDb.OleDbConnection
Dim con作为新的OleDb.OleDbConnection
Dim da As OleDbDataAdapter
Dim da As OleDbDataAdapter
Dim ds As New DataSet
将Dim ds作为新数据集
Dim str1 As String
Dim str1作为字符串
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\AARVIII\Documents\DATABASE\MP1.accdb"
Your connection string locates your database in a fixed position valid only on your PC. 连接字符串将数据库定位在仅在PC上有效的固定位置。
A simple workaround is to use the |DataDirectory| 一个简单的解决方法是使用| DataDirectory | substitution string.
替换字符串。
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| 通常(对于桌面应用程序) | 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.
因此,这不是数据库文件的最佳位置。
However you could change the location pointed by DataDirectory using code like this. 但是,您可以使用以下代码更改DataDirectory指向的位置。 (Of course put it BEFORE any attempt to talk to the database)
(当然,在尝试与数据库对话之前,先将其放置)
' 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 有关DataDirectory的更多信息
Where is DataDirectory DataDirectory在哪里
DataDirectory where is documented 记录的DataDirectory
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的文件夹放在同一文件夹中...
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文件夹中,此连接字符串才能起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.