Connection Strings Explained

Connection strings can be a bit obscure and are often not defined consistently. This article straightens things out and sheds some light on how connection strings are used to connect an application to a data source.
Written by: Sebastian Affakes

When your application connects to a database or a data file you let ADO or ADO.Net utilize a provider to do the job for you. The connection string contains the information that the provider need to know to be able to establish a connection to the database or the data file.

Example of layers when connecting software to data
Connection strings explained

Because there are different providers and each providers have multiple ways to make a connection there are many different ways to write a connection string. It's like the address when sending a regular mail. Depending on the origin and destination and who is going to make the transport you need to write down the address in different ways.

For example; the provider needs the address to the server (or the path to the data file) to connect to. This parameter is often named "Server" or "Data Source". The value specified for this key in the connection string is passed on to the provider and this is how its possible for the provider to know where to connect.

There are some basic rules on how to format the information needed for the connection to take place. The way we represent the configuration values inside of the connection string. As it is a string, there are no data types, it's all string values but they have to be correctly coded so the drivers can read it properly.

Keystones

  • A connection string consists of a series of keyword - value pairs separated by semicolons (;)
  • The equal sign (=) connects each keyword and its value
  • Example: Key1=Value1;Key2=Value2;Key3=Value3;
  • The information in the connection string is passed on to the provider

Okey, I get it, but where are those connection strings located?

The connection string is really just a string inside your application. There is no ODBC DSN admin kind of program for connection strings. You just write your connection string and pass it on to your connection object. The property is named ConnectionString or is passed through the Open function of your connection object.

Summary

There isn't any magic when working with connection strings. The basic idea is quite simple. What often cause problems is when values are improperly set. Sometimes the connection string will work in the development environment, then when moved to production, the application fails. To take some extra minutes and think about each property and read about each of them on these pages will most likely pay off in terms of development time, availability and performance.

There are some important rules that is good to know about when working with connection strings, check them out here »

Connect to