Reputation: 41
I'm building desktop application using c# , I put the connection string in the app.config file like this
<connectionStrings>
<add name="ComputerManagement"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0; Data Source=...;Initial Catalog=Computersh;Integrated Security=True"/>
</connectionStrings>
How I can call the connection string in the forms ?
Upvotes: 0
Views: 168
Reputation: 7475
You can get the connection string with ConfigurationManager
:
using System.Configuration;
var connection = ConfigurationManager.ConnectionStrings["ComputerManagement"];
But you'll still need to use something to connect to the database with it, such as SqlConnection
: http://msdn.microsoft.com/en-gb/library/system.data.sqlclient.sqlconnection.aspx
using System.Configuration;
using System.Data.SqlClient;
var connection = ConfigurationManager.ConnectionStrings["ComputerManagement"];
if (connection != null)
{
using (var sqlcon = new SqlConnection(connection.ConnectionString))
{
...
}
}
Upvotes: 1
Reputation: 45083
Using the ConfigurationManager
should suffice:
var connection = ConfigurationManager.ConnectionStrings["ComputerManagement"];
Then, check for null
, before accessing the actual string:
if (connection != null) {
var connectionString = connection.ConnectionString;
}
Upvotes: 0
Reputation: 17600
Like this:
var constring = ConfigurationManager.ConnectionStrings["ComputerManagement"].ConnectionString;
Also you have to add this using System.Configuration;
:)
Upvotes: 0
Reputation: 6265
Reference System.Configuration
and use
System.Configuration.ConfigurationManager
.ConnectionStrings["ComputerManagement"].ConnectionString
Upvotes: 0