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

Posted on 2005-04-09
Last Modified: 2008-01-09
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.
Question by:rj94070
    LVL 12

    Assisted Solution

    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!

    Author Comment

    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?
    LVL 12

    Accepted Solution

    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'))


    Hope this solves your problem!
    LVL 12

    Assisted Solution

    Also the following might be useful :
    LVL 1

    Assisted Solution

    There is a property "InstalledInstances" in registry path
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server

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

    LVL 1

    Assisted Solution


    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now