Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2005-04-09
6
Medium Priority
?
7,296 Views
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.
Thanks.
0
Comment
Question by:rj94070
  • 3
  • 2
6 Comments
 
LVL 12

Assisted Solution

by:mcv22
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:
http://www.aspsimply.com/cs/NetScan.aspx

Hope this solved your problem!
0
 

Author Comment

by:rj94070
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?
0
 
LVL 12

Accepted Solution

by:
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.
Examples

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!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

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

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

Assisted Solution

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

regds
0
 
LVL 1

Assisted Solution

by:virendra_malushte
virendra_malushte earned 450 total points
ID: 13751368
http://delphi.about.com/od/sqlservermsdeaccess/l/aa090704a.htm

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

regds
0

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