Friday, June 27, 2003

Connection String

First there was the connection then came the data. The string to many an application

I went back to the basics on this one...
the connection string is the very first things I decided I should play with.
connection strings come in so many flavors it is amazing.
In any connection string you need to specify a couple of things, the source place where the database resides, the database name itself, authorization credentials. These are the basic things you need to provide. The fun part is when you realise that each can be provided in so many different ways...

take this simple connection string

connstring = "Data Source=local; database=Northwind;user id=sa; password=;"
Lets examine this one...
Data source tells us where the data resides, which server.
Database: Tells us the name of the database
User id & password: provides the ceredentials for accessing that data.

Another connection string

connstring="Server=local; Initial Catalog=Northwind; uid=sa;pwd=;"

Now here we've replaced our initial "local" keyword with "Server", "database" with Initial Catalog, and "user id" with "uid", "password" with "pwd"
You think thats all ... wait till you see this one ...

connstring ="Addr=10.0.0.1"; Initial Catalog=Northwind;Trusted_Connection=True;"
hey what happened to the user id password... and what happened to the server name.... and would you believe it there are a couple more combinations in which you can specify the basic constructs of your connection string. So initially when people learn it one way confuse them with another and another.. Oh yes you say all the stuff about backward compatibility and more options to the developer... but to those writing articles and books out there... they should reference a particular topic "SqlConnection.ConnectionString Property" present in the MSDN documentation. This shows the numerous options out there to connect. Can you think of how many options are possible ? keep guessing or read the documentation :-)