Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2005-04-09
Medium Priority
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
  • 3
  • 2
LVL 12

Assisted Solution

mcv22 earned 1050 total points
ID: 13744804
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

ID: 13745722
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

mcv22 earned 1050 total points
ID: 13746652
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!

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 12

Assisted Solution

mcv22 earned 1050 total points
ID: 13746656
Also the following might be useful : http://support.microsoft.com/kb/316649

Assisted Solution

virendra_malushte earned 450 total points
ID: 13751350
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..


Assisted Solution

virendra_malushte earned 450 total points
ID: 13751368

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


Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

810 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