how to define a connection string using an app.config file in C# [duplicate]

戏子无情 提交于 2019-11-29 11:07:50

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.

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
 }

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.

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!