I was checking about access database connectivity with c# in social of MSDN where i found the sample connection string as follows
string ConnStr = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\abc.mdb;Jet OLEDB:Database Password=password";
Now my question is suppose i have created a form application and i put the database file abc.mdb at the same location where the .exe file resides. In that case can i write the connection string as follows?
string ConnStr = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=abc.mdb;Jet OLEDB:Database Password=password";
I was trying this with SQL database file but it was not running may be the full path is mandatory for this case. I'm i right?
If you can't use a relative path in connection string, you can generate it at runtime something like:
string connstring = string.Format(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=true", Path.Combine(Directory.GetCurrentDirectory(), "MyDatabase01.accdb"));
try, (although not tested)
string dbpath = AppDomain.CurrentDomain.BaseDirectory + "abc.mdb";
string ConnStr = String.Format(Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Jet OLEDB:Database Password=password;", dbpath)
To answer your first question, yes, having the .mdb file in the same directory will work with your second connection string.
Assuming you meant a MSSQL or MSSQL Express in the second "question," you need to specify the SQL Server instance as part of the connection string.
For ASP.NET 2.0 or higher, the database file (.mdb or .accdb) should always go into the App_Data folder. There are two reasons for this: first, App_Data is configured to prevent users from browsing to the folder and downloading a copy of your database. Second, you can take advantage of the special DataDirectory token (or substitution string) to reference the file within a connection string. DataDirectory defaults to the App_Data directory.
So your Access datasource goes like this.
<asp:AccessDataSource
ID="AccessDataSource1"
runat="server"
DataFile="~/App_Data/MyDb.mdb"
SelectCommand="Select * From MyTable">
</asp:AccessDataSource>
I have not tested this, but the DataDirectory substitution string could be used.
string ConnStr = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\abc.mdb;Jet OLEDB:Database Password=password";
Then you could do
AppDomain.CurrentDomain.SetData("DataDirectory", #"D:\MyApp");
As describe in the following KB article:
|DataDirectory| substitution string support
|DataDirectory| (enclosed in pipe symbols) is a substitution string
that indicates the database path. Therefore, you do not have to
include the full path in the code. When you include the full path in
the code, you may experience problems because the full database path
can be serialized in different locations. The |DataDirectory|
substitution string also makes it easy to share a project and to
deploy an application.
For example, if you include the full path in the code, the application
can have the following connection string.
Data Source= c:\program files\MyApp\Mydb.sdf
If you use the |DataDirectory| substitution
string, the application can have the following connection string.
Data
Source = |DataDirectory|\Mydb.sdf
To set the DataDirectory property, call the AppDomain.SetData method. If you do not set the DataDirectory
property, the following default rules are applied to access the
database folder:
For applications that are put in a folder on the
user's computer, the database folder uses the application folder.
For
applications that are running under ClickOnce, the database folder
uses the specific data folder that is created.
Related
for example, I have a connection string
connection = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=D:\WindowsFormsApplication4\bin\Debug\some.mdb");
but it works correctly only on my computer. How can I make it work correctly on all computers?
You can use
string myconnectionstring = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=\\\\" + PCName + "\\datafolder\\some.mdb";
Specify the PCNAME .
If still it creates problem then assign that database shared folder as a Mapped Drive and use something like this. for example mapped drive is Z: (\PCName\ApplicationFolder)
For more Details please refer to the link : Connecting to a database on a LAN network location
The offending code is the following:
Data Source=D:\WindowsFormsApplication4\bin\Debug\some.mdb"
Basically, you're hardcoding your connection string to a location on your computer. The use of "\bin\Debug" is particularly problematic given that this is almost certainly different than what the path will be in your actual production environment.
I'd recommend putting this in a configuration file of some kind if possible.
Always start here.
https://www.connectionstrings.com/
In your case, I think it should be something like this.
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;
Persist Security Info=False;
I'm trying to set the AttachDBFilename property of connection string in Web.config, with the absolute path of the mdf file, it works, I want to use |DataDirectory| instead of the absolute path, here is what I tried, but it does work.
AttachDbFilename=|DataDirectory|\Database.mdf;
As described here , try to check if there is a log file under the same directory with your mdf file, if there is, remove the log file, Once the database is attached, a new log file will be automatically generated based on the physical path.
I am developing a db application in C# and my current connection string is
#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Shop.accdb;Persist Security Info=False;"
How can I modify it so that the db is in the folder of the project and not in D? I mean I am planning to send the project to a friend so I don't want to include the full path but just the folder of the project.
Thank you in advance!
Change your connection string to
#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Shop.accdb;" +
"Persist Security Info=False;";
|DataDirectory| is a substitution string that (for WinForms apps) will be set by the Framework to the value of the current directory.
In code (before any Data Access) it could be changed to something to your likes with
AppDomain.CurrentDomain.SetData("DataDirectory", #"D:\temp");
See this thread on MSDN
However, keep in mind, that, if your reason to change that value arises for permissions problems, you would have the same problems storing your database in the same folder with your program (C:\program files) because that folder is also severely write restricted. The best way is to store your database in a subfolder of C:\PROGRAMDATA\<myAppDatabaseFolder>
string myFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
myFolder = Path.Combine(myFolder, "myAppDatabaseFolder);
AppDomain.CurrentDomain.SetData("DataDirectory", myFolder);
(I suppose that your setup procedure creates the MyAppDatabaseFolder so I have no check for folder existance)
So I'm trying to connect to my database at the specified location, and the connection is established as long as the db at the same location specified at DataSource field, but what if I tried to distribute my application, the file path will change and will lead to errors I want to avoid. Here is my connstring:
string connstring = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\PC1\Documents\Visual Studio 2008\Projects\Test\Test\bin\Debug\MyDatabase01.accdb;Persist Security Info=true";
Is there anyway I can define DataSource location to be at the same folder?.
There are a few things I can think of to do:
Store the database next to the application and then use a relative file path in the connection string (this makes use of a substitution string built into ADO.Net - see here for more info):
string connstring = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\MyDatabase01.accdb;Persist Security Info=true";
Store your connection string in a configuration file. This can then be changed when your application is run on a machine where the database is in a different place.
Have your application prompt for the location of the database on first use and then save that location to as settings file to use in the connection string.
If you are distributing your database with the application, option 1 is the best. If not, I would go for option 3.
If you can't use a relative path in connection string, you can generate it at runtime
something like:
string connstring = string.Format(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=true", Path.Combine(Directory.GetCurrentDirectory(), "MyDatabase01.accdb"));
I am creating a window application in C# and was thinking of setting up a password on mdb file of MS-Access so that no one can open that file other than my window application or who so ever knows password of that file.
I managed to make that file password protected but unfortunately I was not able to access that file through my application. Actually i not getting where to set the user name and password to open that file. Entering username and password in connection string is not working.
EDIT
Sorry for bit confusion
I want that file to be password protected rather than database connection.
That file should not be opened in any case. For that i managed to set password on file using ms access itself but i m not able to open that file through my application.
Edit2: "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Projects\GargTubes\dbGargTubes.mdb; User Id=""; Password="abc";"
I am using MS-Access 03
Edit 3:
Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=E:\Projects\GargTubes\dbGargTubes.mdb;
Database Password=abc;"
Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=E:\Projects\GargTubes\dbGargTubes.mdb;JET
OLEDB: Database Password=abc;
Error: Could not find installable ISAM
Try setting the database password in your connection string:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;Jet OLEDB:Database Password=MyDbPassword;
You can take a look into that connection strings samples: Connection strings for Access
Since you are talking about passing user name and password, I assume that you have protected your database by setting user rights rather than by using the "database password" option. In that case, users and groups are stored in system.mdw. Be sure to include the path to system.mdw in your connection string (Jet OLEDB:System Database=path\to\system.mdw, see Connection strings for Access for samples).
Alternatively, you use the "database password" feature. Then, the Jet OLEDB:Database Password option described in the other answers should work. To spell it out:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Projects\GargTubes\dbGargTubes.mdb;Jet OLEDB:Database Password=abc