What is the C# code to get the sql server name?

I am just learning C# and sql and wonder what I need to do to find out where the server is so I don't have to hard code it in the connection string.

The tutorial connection string starts like this:

string ConStr =@"server=(local)\NetSDK;" + ...

Obviously, not everybody is going to have his server at (local)\NetSDK.
How do I  check for the server location to dynamically build my connection string?
Complete lines of code wanted.
Who is Participating?
The ServerName property of the SERVERPROPERTY function and @@SERVERNAME return similar information. The ServerName property provides the Windows NT server and instance name that together make up the unique server instance. @@SERVERNAME provides the currently configured local server name.

ServerName property and @@SERVERNAME return the same information if the default server name at the time of installation has not been changed. The local server name can be configured by executing sp_addlinkedserver and sp_droplinkedserver.

If the local server name has been changed from the default server name at install time, then @@SERVERNAME returns the new name.

This example used the SERVERPROPERTY function in a SELECT statement to return information about the current server. This scenario is useful when there are multiple instances of SQL Server installed on a Windows NT server, and the client needs to open another connection to the same instance used by the current connection.

SELECT   CONVERT(char(20), SERVERPROPERTY('servername'))

Reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_sa-ses_3mi1.asp

Hope this solves your problem!
Getting the remote computer name where the server is running automatically is not feasible. What you will need to do is check a hard coded list of probable machine names and check if the port 1443 (Default SQL Server Port) is open. If it is so, you could connect to machinename\NetSDK provided NetSDK is the same throughout and SQL Server is running on its default port.

I could not find much on port scanning, all I could come up with was this site:

Hope this solved your problem!
rj94070Author Commented:
Thanks, mcv22, for the info. I should have been clearer in my question: I just want to get the name of the LOCAL server. I assume some people might have the server installed at NetSDK, but many don't. How do I query the local server name? I read somewhere that up to a certain number of local servers (16?) can be installed on a machine. Each must have a different name. How do I query and enumerate their names?
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Also the following might be useful : http://support.microsoft.com/kb/316649
There is a property "InstalledInstances" in registry path

Please check if you can find anything in this..
I donot have multiple instances installed at my place so couldnt find out if this would help..


THe code for retireving all sql servers available on net is given on the link above
see section..
"List Available SQL Servers". The code is in Delphi..
Hope u can just try to match it up with the C# code syntax & ADO

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.