Abstract:
In this article I will teach you how to get names and all other information of
sql server instances that are in network.
This type of coding will help you in situation where you want to show selection
for the user what sqlserver instance he want to connect to.
Implementation:
For getting all instances of the sql server running on network computers we are
going to us namespace
System.Data.Sql;
Import the namespace by writing statement
using
System.Data.Sql;
Now we can get all the instance information in DataTable by executing the method
GetDataSources() of the
SqlDataSourceEnumerator
class
So simply we will write the code.
Like below to retrieve the data table containing information about all the
instances of slqserver. Then we can manipulate that datatable to show
information as our project requirement.
I have setup GUI like below as I want to show all the information like below
private
void Form1_Load(object
sender, EventArgs e)
{
/* get data sources */
DataTable dt =
SqlDataSourceEnumerator.Instance.GetDataSources();
/* Do whatever you
want to do with that table; */
comboBox1.Items.Clear();
foreach (DataRow
dr in dt.Rows)
{
comboBox1.Items.Add(dr[0].ToString());
}
dataGridView1.DataSource = dt;
}
As you can see we can get more information not only name of the sql server
instance like
ServerName
Instance Name
Is Clustered
And version also
Just two likes of code and you have enumerated all the instances of the
sqlserver instances in the network
Conclusion:
Article teaches how to enumerate Sqlserver instances in the network