3

I have the following connection string through which i connect to an Access database(.mdb) located in a sub-folder inside the root-folder of my application :

 OleDbConnection con = new OledbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory() + "Data\rctts.mdb;Jet OLEDB:Database Password=mypassword;")

My question is, how do i put the connection string in the app.config file? Generally, i use :

 <connectionStrings>
    <add name="Test" connectionString="Data Source=.;Initial Catalog=OmidPayamak;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

The issue i am facing is that in my connection string, i am doing some concatenations and also using System.AppDomain.CurrentDomain.BaseDirectory() to set the Data Source... How do i do the same concatenation in app.config ? Is it possible to do so ?

2
  • That looks like VB.Net concatenation, not C#. Commented May 30, 2018 at 14:20
  • @juharr , now it looks like c# :) Commented May 30, 2018 at 14:22

2 Answers 2

3

You could try the following:

connectionString="Data Source=|DataDirectory|\rctts.mdb;Initial Catalog=OmidPayamak;Integrated Security=True"

and then try to set the value of DataDirectory as below:

var currentDomain = AppDomain.CurrentDomain;
var basePath = currentDomain.BaseDirectory;
currentDomain.SetData("DataDirectory", basePath+"\Data");

at the corresponding startup file of your application.

Sign up to request clarification or add additional context in comments.

14 Comments

don't mind me asking, i don't see the app.config part here?
The app config part is the first one. In the app config you would just replace "Data Source=..." with the above.
can you explain what |Data Directory| means here? is it working like a variable which is expecting values?
@zackraiyan Exactly ! It is variable that can be set trough AppDomain. Please check this blogs.msdn.microsoft.com/smartclientdata/2005/08/26/…
@Christos , understood
|
3

Although configuration APIs offer no facilities to manipulate connection strings for you, you could place connection string "template" into configuration, and do the rest of manipulation in your code using string.Format:

Config:

<connectionStrings>
    <add name="Test" connectionString="Data Source={0}Data\rctts.mdb;Initial Catalog=OmidPayamak;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

C# code:

string conStr = string.Format(
    ConfigurationManager.ConnectionStrings["Test"].ConnectionString
,   System.AppDomain.CurrentDomain.BaseDirectory()
);

2 Comments

i am getting a warning : Runtime error may occur when converting 'ConnectionStringSetting' to 'IFormatProvider' , should i be worried?
@zackraiyan That's right - I forgot to add .ConnectionString. That's what happens when I do not try to compile things locally. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.