4

Currently i manually define my connection string in my C# code:

string ConnectionString = "Data Source=C;Initial Catalog=tickets;Integrated Security=True";
SqlConnection Conn = new SqlConnection(ConnectionString);
Conn.Open();

In my project i have an app.config file and i can see that it has a connection string. It looks like this:

<?xml version="1.0"?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
    <add name="ticketNotification.Properties.Settings.ticketsConnectionString"
        connectionString="Data Source=C;Initial Catalog=tickets;Integrated Security=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

How can i define the connection string based on the app.config file in the same folder as my application?

PriceCheaperton
  • 5,071
  • 17
  • 52
  • 94

4 Answers4

9

Modify your application with this

App.config file

<?xml version="1.0"?>
  <configuration>
    <configSections>
    </configSections>
    <connectionStrings>
         <add name="ticketNotification.Properties.Settings.ticketsConnectionString"
              connectionString="Data Source=C;Initial Catalog=tickets;Integrated Security=True" providerName="System.Data.SqlClient" />
    </connectionStrings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
  </configuration>

and here is your code behind file. You need to use System.Configuration Namespace. System.Configuration namespace contains ConfigurationManager class which is used to get connection string from app.config/web.config files.

As this is a windows application you have App.config file where you have defined your connection string. To get that connectionstring defined in the configuration file you need to use below line of codes inside your required eventhandler

Add this namespace to your code behind (ex: Form1.cs) file

using System.Configuration;

And here inside the eventhandler add/modify these codes

  string myConnectionString = ConfigurationManager.ConnectionStrings["ticketNotification.Properties.Settings.ticketsConnectionString"].ConnectionString;

  using(SqlConnection Conn = new SqlConnection(myConnectionString))
  {
     Conn.Open();
     //Define the SQL query need to be executed as a string

    //Create your command object

    //Create Dataset,Dataadapter if required

    //add parameters to your command object - if required

   //Execute your command

   //Display success message
 }
Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
7

To get connection strings from the app.config file you use the class ConfigurationManager located in the System.Configuration namespace.

To get the connection string by name and create a connection based on it, you can use the following code:

SqlConnection conn = new SqlConnection(
      ConfigurationManager.ConnectionStrings[
         "ticketNotification.Properties.Settings.ticketsConnectionString"]
             .ConnectionString);

You can read more about this here (MSDN Documentation): http://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx

Ensure your project holds a reference to System.Configuration, otherwise ConfigurationManager will not be available in the System.Configuration namespace.

Regards.

João Pinho
  • 3,725
  • 1
  • 19
  • 29
5

Firstly add a reference to System.Configuration by right-clicking References and selecting Add reference. Then use

Using System.Configuration

// within the class here

ConfigurationManager.ConnectionStrings["ticketNotification.Properties.Settings.ticketsConnectionString"].ConnectionString;

Obviously you will need to put in the name of your connection string in the brackets.

Andrew
  • 2,315
  • 3
  • 27
  • 42
0

You need to add a reference to System.Configuration to your project. You will then be able to add the using System.Configuration statement and get the connection string by name using:

string connectionString = ConfigurationManager.ConnectionStrings[<name>].ConnectionString;
field_b
  • 688
  • 5
  • 21
  • The connection is called "ticketsConnectionString" is that what i put for the name? with the angled brakets? – PriceCheaperton Jan 01 '14 at 18:48
  • Yes. It is a string so enclose it in quotations. Remove the angled brackets. Ex: "connectionStringName" – field_b Jan 01 '14 at 19:04
  • Probably you have .Net 4.5 Client Profile in Project properties and that' why, you are not able to add the reference to System.configuration. If so, please change the Project Properties settings to point to .Net 4.5 ("Client" word shouldn't be there). You can go to Project Settings --> Application tab --> look for Target Framework ( it should be set to .Net Framework 4.5 – James Jan 01 '14 at 19:13
  • @James: I believe that got sorted already. The reference needed to be made. – field_b Jan 01 '14 at 19:20