I'm working on a C# project with Visual Studio 2015 Express where I have an Access database that I want incorporate into a project, because, I give the project to a friend, I have to place the Access database into a different folder path, so I want make a folder into project. I don't know the folder's path.
The following sets up a connection for a .accdb database one folder below the application executable. This will not work with a ClickOnce install or if using TableAdapters.
You need to change Databases to a folder that exists and change the database name to match your database,
Hope this helps.
// This translates to: AppFolder\Databases\Database1.accdb
// AppFolder: Location of the executable
// DataBases: an existing folder below AppFolder
var databasePathName = Path.Combine(Application.StartupPath,"Databases", "Database1.accdb");
var Builder = new OleDbConnectionStringBuilder()
{
Provider = "Microsoft.ACE.OLEDB.12.0",
DataSource = databasePathName
};
using (OleDbConnection cn = new OleDbConnection() { ConnectionString = Builder.ConnectionString })
{
cn.Open();
// do work
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.