Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Could any body give me code to get server name of the computer that client computers connect to access SQL Server database?

Thanks
Posted

1 solution

Not the specific one, if there are more than one, but:
C#
SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
DataTable dt = instance.GetDataSources();
foreach (System.Data.DataRow row in dt.Rows)
    {
    Console.WriteLine("ServerName = {0}", row["ServerName"]);
    }
 
Share this answer
 
Comments
Furqan Sehgal 19-Jan-12 11:58am    
Added to my confusion. My purpose is that when I my client installs client side of the application, he could enter the server name on which database has been attached by the Server side installation.
Don't we need to know exact server name in the given case?
OriginalGriff 19-Jan-12 12:08pm    
Yes, but unless your software installed the database - in which case you already know the server instance and the database name - he probably won't.
Most users don't even know their own PC name, let alone the SQL server instance names. :laugh:
That fragment lists all the instance names - you could let the user pick one (if there is more than one) or scan each looking for your database. The latter is much more user friendly - and not that hard to do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900