Monday, April 19, 2010

Get all running Sql Server instances name within network using Asp.Net C# vB.Net

Basically who did sql server operational job in any organization sometimes requires to get all running sql server instances name. Here in this asp.net C# VB.Net article i will give you a simple code snippet how you can achieve this. After running my sample code you will get all running sql server instances in your Local Area Network (LAN)/Network even from your local PC like below:

Sql Server Instances name

To run the below code sample just add a page in your project & add a GridView control. Then under page_load event write the below code sample:

C# complete Code:
using System;
using System.Data;
using System.Data.Sql;

public partial class SQL_Insances : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlDataSourceEnumerator SqlEnumerator;
            SqlEnumerator = SqlDataSourceEnumerator.Instance;
            DataTable dTable = SqlEnumerator.GetDataSources();
            GridView1.DataSource = dTable;
            GridView1.DataBind();
        }
    }
}
VB.Net Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (IsPostBack=False) Then
            Dim SqlEnumerator As System.Data.Sql.SqlDataSourceEnumerator
            SqlEnumerator = System.Data.Sql.SqlDataSourceEnumerator.Instance
            Dim dTable As System.Data.DataTable
            dTable = SqlEnumerator.GetDataSources()
            GridView1.DataSource = dTable
            GridView1.DataBind()
        End If
    End Sub

3 comments:

  1. Hi Shawpnendu,

    This was a nice example for listing the SQL Server instances. The best thing about this code is, it works even there is no LAN in your system, which is not possible using the SQLDMO object.

    Thanks
    Abhijeet Kanjilal
    Senior Developer
    Navsoft, Kolkata

    ReplyDelete
  2. Thanks!! This works very well..

    ReplyDelete

Write your Comment: