C# Language Connection Strings


A Connection String is a string that specifies information about a particular data source and how to go about connecting to it by storing credentials, locations, and other information.

Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;

Storing Your Connection String

Typically, a connection string will be stored within a configuration file (such as an app.config or web.config within ASP.NET applications). The following is an example of what a local connection might look like within one of these files :

   <add name="WidgetsContext" providerName="System.Data.SqlClient"  connectionString="Server=.\SQLEXPRESS;Database=Widgets;Integrated Security=True;"/> 

   <add name="WidgetsContext" providerName="System.Data.SqlClient"  connectionString="Server=.\SQLEXPRESS;Database=Widgets;Integrated Security=SSPI;"/> 

This will allow your application to access the connection string programatically through WidgetsContext. Although both Integrated Security=SSPI and Integrated Security=True perform the same function;Integrated Security=SSPI is preferred since works with both SQLClient & OleDB provider where as Integrated Security=true throws an exception when used with the OleDb provider.

Different Connections for Different Providers

Each data provider (SQL Server, MySQL, Azure, etc.) all feature their own flavor of syntax for their connection strings and expose different available properties. ConnectionStrings.com is an incredibly useful resource if you are unsure about what yours should look like.