Swetha Sankaran

Swetha Sankaran

  • NA
  • 52
  • 35.4k

FYI Getting the server name and SQL instance name

Apr 28 2009 3:06 PM

public string RetrieveDataSource

        {

                  get

                  {

                SqlConnection sqlConnection = new SqlConnection(this.CurrentConnectionString);

                      SqlCommand sqlCmd = new SqlCommand();

                      sqlConnection.Open();

                sqlCmd.Connection = sqlConnection;

                      return "Server Name :" + sqlCmd.Connection.DataSource;

                  }

        }

The above code returns the DataSource in the following format “MyServerName\SQLInstanceName”

Is there any other way we can achieve the same result? The CurrentConnectionString comes from a web.config file. I'm trying to improve the code.

Thanks!


Answers (1)