This question already has an answer here:
- Get connection string from App.config 18 answers
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?
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;
来源:https://stackoverflow.com/questions/20871776/how-to-define-a-connection-string-using-an-app-config-file-in-c-sharp