Connection string to localhost with mssql express

This one is a short one, but I’ve been asked this a lot, so here it is:

How to connect to a mssql server usually the one installed with visual studio 2010/2008.

The shortest answer:

"Data Source=.\SQLEXPRESS;Initial Catalog=[your database name];Integrated Security=True;Pooling=False"

I’ve got this from many users who tried “localhost” “.” or even nothing at all. True, there were some times that actually worked, But not in the recent versions of visual studio. Also, creating a database is now being done through the server explorer:

And you will obviously still going to need the connection string.

*You might want to check this for connection code.

Using mysql and .NET

Creating a connection String:

String connectionString = String.Format("Data Source={0};Database={1};
User ID={2};Password={3}", host, DBCatalog, username, password);

creating a connection:

mainConnection = new MySqlConnection(connectionString);

opening a connection:


Execute non query (one that does not return any records):

String theQuery ="select....";
DbCommand cmd = Connection.CreateCommand();
cmd.CommandText = theQuery;

Returning a simple reader:

String theQuery ="select....";
DbCommand cmd = Connection.CreateCommand();
cmd.CommandText = theQuery;   

Returning a full-pledge record set:

DbDataAdapter dap=new MySqlDataAdapter(theQuery, mainConnection);
DataSet ds = new DataSet();