0

How to get connection string from below config? I researched on google and also stackoverflow but can't get connection string. And I did not find configuration manager class also although I included System.Configuration.

app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections></configSections>
    <connectionStrings>
        <add name="std_8_science.Properties.Settings.science8ConnectionString"
             connectionString="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=F:\science_8.mdb"
             providerName=".NET Framework Data Provider for OLE DB"/>
    </connectionStrings>
</configuration>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Riddhi Rathod
  • 410
  • 2
  • 9
  • 1
    You have to actually add a project reference to System.Configuration to access the ConfigurationManager class. – Kevin Jul 01 '16 at 17:17
  • I use that reference but can't found ConfigurationManager class. So how can I get connectionString? – Riddhi Rathod Jul 01 '16 at 17:29

2 Answers2

0

Your string is off a bit. The below connectino string should get you back up and running

<add name="" connectionString="Data Source=.\SQLEXPRESS;Database=;Initial Catalog=;integrated security=True;" providerName="System.Data.SqlClient"/>

Then, just set the providerName to Microsoft.Jet.OLEDB.4.0

And, clearly, set Database and Initial Catalog

A really good resource is ConnectionStrings.com

Ingenioushax
  • 718
  • 5
  • 20
  • I tried this but can't work yet – Riddhi Rathod Jul 01 '16 at 17:27
  • Is there an exception that's being thrown? Could you show the new connection string you set up? – Ingenioushax Jul 01 '16 at 17:33
  • Error : 'std_8_science.Properties.Settings' does not contain a definition for 'science8ConnectionString' and no extension method 'science8ConnectionString' accepting a first argument of type 'std_8_science.Properties.Settings' could be found (are you missing a using directive or an assembly reference?) – Riddhi Rathod Jul 01 '16 at 17:40
  • and if i write statically to each page like conString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\science_8.mdb"; than it works. But I want it to write it only once in app.config. – Riddhi Rathod Jul 01 '16 at 17:41
  • You can't reference your app.config in your web.config and expect it to resolve. ConfigurationManager provides mechanisms for you to pull that information, but expecting the app.config value to replace the value in your connection string won't work. This `std_8_science.Properties.Settings.science8ConnectionString` won't work. If you want your app.config to replace the web.config values, consider setting up a web.config transform for this. – Ingenioushax Jul 01 '16 at 17:46
  • I can't found web.config in my project, I added the app.config and write the connectionstring as described above, than how to get it??? I can't found the solution to get it. Not working both ways for me. – Riddhi Rathod Jul 01 '16 at 17:57
  • Is this a console application? Probably should have started with this question. – Ingenioushax Jul 01 '16 at 18:01
0

From comments I see you are trying to read the connection string as a Setting, but it isn't.

Add a project reference to System.Configuration. Then get the string with:

var connectionString = ConfigurationManager.ConnectionStrings["std_8_science.Properties.Settings.science8ConnectionString"].ConnectionString;

Consider using a lot shorter name for your connectionstring. After all it is not a setting...

Mattias Åslund
  • 3,877
  • 2
  • 18
  • 17