简体   繁体   English

当源文件路径更改时,如何保持与项目的数据库连接?

[英]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.

相关问题 如何获取我的项目的基本目录(.vbproj 或 .sln 文件所在的路径) - How do I get the Base Directory of my Project (The path where the .vbproj or .sln file are located) 如何在整个项目中处理数据库连接 - How to handle database connectivity in entire project 如何使用应用程序复制访问数据库文件? - How do I copy an access database file with my application? 如何在已发布的vb.net项目中包含SQLite数据库? - How do I include my SQLite database in my published vb.net project? 如何在WinForm应用程序项目中获取目录的路径 - How do I get path of the Directory inside my WinForm app project 如何在VB.NET项目中将链接文件添加到My Project文件夹? - How do I add a linked file to the My Project folder in a VB.NET project? 在其他系统上安装项目时,如何使数据库正常工作 - How to get database working when i install my project in other system 选择粗体、斜体或下划线时如何保留字体样式和大小? - How Do I Retain Font Style and Size When Selecting Bold, Italic or Underline? 在窗体之间切换时如何保留窗体的控制值 - How do I retain a form's control values when flipping between forms 写入文件时,如何正确解析对路径的访问被拒绝? - When writing to a file, how do I correctly resolve access to path is denied?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM