Get the Server name and SQL instance name in ToolPart (c#)

Requirement: ServerName and InstanceName to be displayed in a ToolPart

Data: The classes required to retrieve the DataSource(this contains the server name and instance name used in the connection string) using ASP.NET are,

SqlConnection, SqlCommand

Solution:

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;

}

}

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s