Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
hi ever one i have project in c# and access 2010 ... in my project i have some windows forms in each form i use . . . <code>OleDbConnection db_con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\HUNAR\Desktop\Bashi Nawxoyy\Bashi Nawxoyy\bin\Debug\db_file.accdb;Jet OLEDB:Database Password=RTm$&9sdfZXC;Persist Security Info=True");</code> ... to link to the database file that named db_file.accdb.

1-  i want to configure db connection in app.config file and call it in each form

2- if i create setup file(project_name.exe) for my project , how i make .exe file be able connect to db_file.accdb in any machine that i install the exe file on it ie. attach db file with setup file and after installation connect to db file directly???
Posted

 
Share this answer
 
v2
Comments
hunar.abubakr 22-Aug-13 3:39am    
sorry thatraja but i don't understand this exaclty i read the explain in link but i don't understand it?
thatraja 22-Aug-13 3:53am    
Check my updated answer
thatraja 22-Aug-13 4:09am    
that'll work in app.config too
thatraja 22-Aug-13 4:26am    
It's been long time since I have worked with MS access. I think \ is missing there.

<add name="AccessCon" connectionstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Test.mdb;" providername="System.Data.OleDb"></add>

And let me know with full details(exception message) after tried this.
hunar.abubakr 22-Aug-13 4:37am    
ok i do this it gives me error like image in this link http://s24.postimg.org/yt2mh1dc5/Capture.png
also i want to say my db is .accdb not mdb (access 2010)
Add this code on App.config file

XML
<connectionStrings>
  <add connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\projecty bashi nawxoyy\Bashi Nawxoyy\Bashi Nawxoyy\db_file.accdb" name="connectionString"/>
</connectionStrings>



and call connection string on your .cs class

C#
string conString = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ToString();
db_con = new OleDbConnection(conString);


Thanks & Regard
Sham :)
 
Share this answer
 
Comments
hunar.abubakr 22-Aug-13 3:38am    
ok Shambhoo kumar thank you for your answer .... i do this and it's work well ... but know i want to make the "<add connectionstring="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\projecty bashi nawxoyy\Bashi Nawxoyy\Bashi Nawxoyy\db_file.accdb" name="connectionString">" a relative path .. i mean i want to make it work on any machine without need to change the path (D:\projecty bashi nawxoyy\Bashi Nawxoyy\Bashi Nawxoyy\db_file.accdb)??? is this possible exaclty i want to do this for my .exe file?
Shambhoo kumar 22-Aug-13 5:36am    
Just u paste your database file inside your application Bin-Debuge folder and use this connection string

<connectionstrings>
<add connectionstring="Provider=Microsoft.ACE.OLEDB.12.0;Data Source = |DataDirectory|\Mydb.accdb" name="connectionString">
CSS
If you want to put Access Data Base inside your C# application, then you should do this.
First of all create any Access data base like here "PirFahimDataBase". Now in your Visual Studio go to the menu and do this

1: Click Data
2: Add New Data Base
3: Click Next
4: Click New Connection
5: Now change the Data Source by clicking Change and select Microsoft Access data base files
6: Click Browse for selecting your created data base

Now take inside in your project. You will have an access data base file in your project. After all that go to your .cs file and give a path to your data base connection string

   System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
            conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
        @"Data source=PirFahimDataBase.accdb";

Here, lets give an attention you have no extra path for your "Data Source" , because your Access Data Base file is in your project now. So just put the name of your Data Base File.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900