Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want get SQL servername by that query:

SQL
SELECT @@SERVERNAME


Now, i want try in C# like:
C#
try
           {
               string queryString = "SELECT @@SERVERNAME";
               {
                   SqlCommand command = new SqlCommand(queryString);
                   var result = command.ExecuteNonQuery();
               }

           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }

But it's not work.
Plz help me!
Thank!
Posted
Comments
[no name] 11-Oct-14 20:05pm    
Of course it does not work. Use SMO to get a list of servers.
GinCanhViet 11-Oct-14 20:17pm    
i have tried it. It's not work some computer :(
[no name] 11-Oct-14 22:27pm    
SMO works for all versions of SQL Server from 2005 on. So it does work. Why you think it does not, is a mystery.

Read this : http://sqlblogcasts.com/blogs/jonsayce/archive/2008/02/10/programatically-listing-sql-servers.aspx[^]

Generally database server names to connect to are a known configuration point when you deploy your application, and for security reasons the above way may not work (you don't want to advertise your database server for hackers etc.).
 
Share this answer
 
You can't! Why? You need to be connected to particular server to run sql command.

If you want to display the list of sql servers installed on local computer or on lan, please see: How to enum SQL Server instances in network[^]

If you want to check if SQL statement is correct, look for SQLParser[^].
 
Share this answer
 

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