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!
Kumar AGPosted Apr 29, 2009, 3:12 PM
public string RetrieveDataSource
{
get
{
using (SqlConnection sqlConnection = new SqlConnection(this.CurrentConnectionString))
{
sqlConnection.Open();
return "Server Name :" + sqlConnection.DataSource;
}
}
}