Reputation: 123
I am making C# windows form application - is connected to database, so I need connection with it.
The "problem" is that my application should work on any PC.
But, as we know, every PC has different SQL connection string. It would be a little bit unpractical to change connection string in code every time when someone runs application.
So, my question is, is there a way to avoid this?
Upvotes: 0
Views: 2022
Reputation:
This is a example to connect to the same database from all PC
//Geting Connection to database
static SqlConnection GetConnection()
{
return new SqlConnection(@"Data Source=.\SQLEXPRESS; Initial Catalog=Northwind; Integrated Security=SSPI");
}
Upvotes: 0
Reputation: 4911
Usually, your connection strings goes in a xml .config file :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Test" connectionString="Data Source=SERVER;Initial Catalog=DATABASE;User Id=USER;Passwor=PASSWORD" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
And you get them using the ConfigurationManager :
var connectionString = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
You will still need to update the .config file on each PC, but not recompile the source.
Upvotes: 2