C#: SQL Server Connection String

Well, what database are you using? For example, SQLServer? Oracle? MySQL?

In SQLServer, at least, the connection string is defined in the web.config, or app.config, and has syntax similar to the following:

<appSettings>
<add key="ConnectionStringName" value="AppName"/>
</appSettings>
<connectionStrings>
<add name="AppName" connectionString="Data Source=DataSourceName; Initial Catalog=DataBaseName; user id=UserID; password=Password" providerName="System.Data.SqlClient"/>
</connectionStrings>

Something like that. There’s probably something I’m missing, but without testing it out in your code, I’m blanking on what that might be.

This how you extract the connection string from your code if the connection string is properly set in the web.config:

 var connectionString = ConfigurationManager.ConnectionStrings["AppName"].ConnectionString;

Leave a Comment