Help with connection string - c#

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"));

Related

How get current working path to accessdb with c#?

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;

Get data from a database which is in a server machine

I have a winForm application which will be used by more than one persone, I want to put the database in a machine server so can everyone connect to it from this application.
I'm using SqlServer database, this database will be located in a local network.
This is the connection string I use to get data from a database located in my machine
string con = "Data Source=MSSQL1;Initial Catalog=AdventureWorks;Integrated Security=true;";
My question is what is the connection string that I should use to get data from database located in a machine server ?
Is it something like this ?
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;
Thanks.
Without any specific information nobody will be able to answer this for you, however This Resource. might help you find it for yourself.

Relative path to DB in connection string

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)

Connectivity between access database and c#

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.

how to connect to access 2007 with c#

hey,
i am new at connecting to dataBases and for some reason each time i use those following lines my program collapse:
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|\Company.accdb"
OleDbConnection con = new OleDbConnection(connectionString);
inside my debug folder i got Company.accdb access file
edit:
i am getting 'Microsoft.Ace.OLEDB12.0' provider is not registered on the local machine any idea how to solve it?
thanks in advance for your help
Two things -
This connection string rely on ACE OLEDB provider (typically comes with Office 2007 - your machine need to have this provider)
Connection string is requesting data dictionary. You probably need to use below form:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;
For password protected files, form would be Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Jet OLEDB:Database Password=MyDbPassword;
I will also suggest trying different Provider (ODBC perhaps) instead. For various connection strings for Access 2007, refer http://www.connectionstrings.com/access-2007

Categories