1

Where to add the connection string in a C# project? Like this string?

<connectionStrings>
    <add name="strConn" 
         connectionString="Data Source=abc;Password=pass;User ID=user" 
         providerName="Oracle.DataAccess.Client">
    </add>
</connectionStrings>

And how can I call it from my program.cs file?

4
  • What kind of project: ASP.NET? Winforms? WPF? SIlverlight? COnsole app?? Commented Nov 21, 2011 at 15:23
  • @marc_s - Its a console app. And i dont see any app.config file in my project? Commented Nov 21, 2011 at 15:27
  • 2
    My crystal ball foresaw that this was a console app. Commented Nov 21, 2011 at 15:28
  • I just selected the XML and hit the 'code' button. What XML was missing then? Commented Nov 21, 2011 at 15:40

3 Answers 3

4

This will be added in the configuration file. If it is an ASP.NET application, then this would be the web.config file. If it is a Winforms/Console application this would be the app.config file.

To call it from the application, you'd have to use the System.Configuration namespace like so:

using System.Configuration;

string YourConnectionString = 
    ConfigurationManager.ConnectionStrings["yourConnStringName"].ConnectionString;

Where "yourConnStringName" is the name of your connectionString in your config file.

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

2 Comments

I have console app. But i dont see any app.config file in the project? Do i need to add it?
Yes, you need to add that. Add items and you'll see configuration file.
2

The connection string like the one you posted is placed in a configuration file called app.config and whenever you want to get a connectionstring you can get it like:

string strConn = ConfigurationManager.ConnectionStrings["strConn"].ConnectionString;

see this

1 Comment

The connection string COULD be stored in appSettings, but the OP seems to want to use the connectionStrings section.
0

Add it to your app.config under <configuration> and to call it from your program.cs use:

ConfigurationSettings.AppSettings["strConn"]

Comments

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.