user3740362
user3740362

Reputation:

C# SQL Server database connection without native application

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;";

Upvotes: 2

Views: 57

Answers (1)

Craig W.
Craig W.

Reputation: 18175

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?

Upvotes: 1

Related Questions