Search code examples
c#connection-stringapp-config

c# connection string and concatenation app.config


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 ?


Solution

  • 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.