Click here to Skip to main content
15,882,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello members,
how to get server name dynamically when my application gets installed on a machine..
i m using ADO.NET and Sql server 2005...???
Posted

try this, this may helps you

C#
using(SqlConnection _con = new SqlConnection(your-connection-string))
{
   string instanceName = new Microsoft.SqlServer.Management.Smo.Server
                            (new ServerConnection(_con)).InstanceName;
}



Thanks
--RA
 
Share this answer
 
Comments
[no name] 13-Feb-12 7:46am    
what does it mean by "Your-connection-string"..???
Rajesh Anuhya 13-Feb-12 7:46am    
that is your connection string using for the sqlserver connection.
--RA
[no name] 13-Feb-12 7:49am    
what are the namespace required for this...???
[no name] 13-Feb-12 7:54am    
i m getting this error....


Error 1 The type or namespace name 'Management' does not exist in the namespace 'Microsoft.SqlServer' (are you missing an assembly reference?) C:\Documents and Settings\Dinesh Suthar\Desktop\WindowsApplication1\WindowsApplication1\Form1.cs 24 50 WindowsApplication1
 
Share this answer
 
Comments
Michael dg 13-Feb-12 7:58am    
the above link will list all the available Sql servers.
[no name] 13-Feb-12 8:02am    
i m getting this error....


Error 1 The type or namespace name 'Management' does not exist in the namespace 'Microsoft.SqlServer' (are you missing an assembly reference?) C:\Documents and Settings\Dinesh Suthar\Desktop\WindowsApplication1\WindowsApplication1\Form1.cs 24 50 WindowsApplication1

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