LazyLady LazyLady - 10 months ago 28
SQL Question

Is there a way to set SQL connection string automatically?

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?


Usually, your connection strings goes in a xml .config file :

<?xml version="1.0" encoding="utf-8" ?>
        <add name="Test" connectionString="Data Source=SERVER;Initial Catalog=DATABASE;User Id=USER;Passwor=PASSWORD" providerName="System.Data.SqlClient" />

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.