2

I am looking for a suggestion, how can I connect database without writing the connection string in the native application.

I am using this but looking for another so that, when I rename database, the software will connect automatically.

static string conStr = @"server=myServerName;Integrated Security=true;Connection Timeout=5;Database=myDatabaseName;";
0

1 Answer 1

1

You can put the connection string in the configuration file.

<connectionStrings>
    <add name="MyDatabase" connectionString="server=myServerName;Integrated Security=true;Connection Timeout=5;Database=myDatabaseName;" providerName="System.Data.SqlClient" />
</connectionStrings>

Then to retrieve the string you want you use:

ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString

Asking for it to connect automatically even if you rename the database isn't possible. Think about it, a single server can host hundreds of databases. How would it know which one to connect to?

Sign up to request clarification or add additional context in comments.

1 Comment

A project reference to the System.Configuration assembly is needed to use this method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.