Link to home
Start Free TrialLog in
Avatar of hobbesk_ati
hobbesk_ati

asked on

Connectiong to a SQL DataBase using C#

Hi,
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.

ThankYou
Avatar of jeebukarthikeyan
jeebukarthikeyan
Flag of India image

hi,

have a look @ below link

http://www.connectionstrings.com/sql-server-2008

b u d d h a
Avatar of hobbesk_ati
hobbesk_ati

ASKER

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.
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
ASKER CERTIFIED SOLUTION
Avatar of drypz
drypz
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Drypz,
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"

ThankYou
>>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
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
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.