Connectiong to a SQL DataBase using C#

I have an application, which needs to connect to a remote(local network) or local SQL DataBase. The machine on which this database is can have any version of SQL installed(SQL Server Express or SQL Server 2008).
How should i handle the connection strings in this case and what should they be? How do i get the SQL Server name on that machine if i know the machine name on which the database is?
Please help.

Who is Participating?
drypzConnect With a Mentor Commented:
string serverName = "MyServer";
string databaseName = "Northwind";
string userName = "myUserName";
string password = "myPassword";

string connectionString = "Data Source=" + serverName + ";Initial Catalog=" + databaseName + ";User ID=" + userName + ";Password=" + password + ";";

if you connect to sqlexpress your serverName looks like this

serverName = "MyServer\SQLEXPRESS";


have a look @ below link

b u d d h a
hobbesk_atiAuthor Commented:
I already did refer that site, but that dosent help me much. I am still unclear about how to construct the connection string and get its required parameters.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Anurag ThakurTechnical ManagerCommented:
First there is not difference in connection strings to connect to sql express or sql server 2008
no need to handle any thing differently for handling the databases

select @@SERVERNAME will give you the server name on which the database is located
hobbesk_atiAuthor Commented:
If i know the machine name on which the database is, how can i find out which SQL is installed there(SQL Express or SQL Server ).
If it is SQL Express how do i find out the SQL Instance name on that machine. Because the syntax you just mentioned for SQL Express requires "ServerName\SQLInstanceName"

Anurag ThakurTechnical ManagerCommented:
>>if you connect to sqlexpress your serverName looks like this
>>serverName = "MyServer\SQLEXPRESS";

not necessary as i can just install on the default instance rather than the named instance of SqlExpress
Anurag ThakurTechnical ManagerCommented:
you need to know this information before you actually connect to the database name through code

for finding you will have to open sql server configuration manager and under sql server 2005 services you can see SQL Server (MSSQLSERVER) or SQL Server (Instance Name)
MSSQLSERVER is the default instance and for rest all you will have to use sqlservername\instancename
hobbesk_atiAuthor Commented:
Thank You very much for all your help so far, In case I need to create the ConnectionString dynamically based on what kind of authentication user has and if user is using SQL Express or not.
I found there is a SQLConnectionStringBuilder provided in .Net.
That seems to be very helpful in dynamic creating of connection string.
However i could not find a few properties missing there.
Like InstanceName, PortNumber, Trusted_Connection.
How do i consider these extra parameters when building a connection string using SQLConnectionStringBuilder.
Thanks Very much again.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.