none
Using System.Data.SqlClient to retrieve SQL Servers available RRS feed

  • Question

  • Hi,

    i want to build a wizard to let a user connect to a SQL server database.
    There for I want to show the user a list with SQL Servers found like the Add Connection in Visual Studio.

    How can I retrieve information about SQL Servers found in my network?

    Thanks.

        Dim Dbf As DbProviderFactory = DbProviderFactories.GetFactory("System.Data.SqlClient") 
        Dim Con As DbConnection = Dbf.CreateConnection
    
    Friday, November 20, 2009 9:56 AM

Answers

  • Hi,

    You can use System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources();

    Here is a sample.

    cmbSqlServers.DisplayMember = "ServerName";

    cmbSqlServers.DataSource = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources();

    • Marked as answer by Willie007 Friday, November 20, 2009 10:44 AM
    Friday, November 20, 2009 10:02 AM

All replies

  • Hi,

    You can use System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources();

    Here is a sample.

    cmbSqlServers.DisplayMember = "ServerName";

    cmbSqlServers.DataSource = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources();

    • Marked as answer by Willie007 Friday, November 20, 2009 10:44 AM
    Friday, November 20, 2009 10:02 AM
  • Hi,

    You can use System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources();

    Here is a sample.

    cmbSqlServers.DisplayMember = "ServerName";

    cmbSqlServers.DataSource = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources();

    i made this version:

    Windows.Forms.Cursor.Current = Cursors.WaitCursor

     

    Try

     

    Dim instance As System.Data.Sql.SqlDataSourceEnumerator = System.Data.Sql.SqlDataSourceEnumerator.Instance
    Dim dataTable As System.Data.DataTable = instance.GetDataSources()

     

    For Each oRow As DataRow In dataTable.Rows
    Try
       Me.ComboBox1.Items.Add(IO.Path.Combine(oRow.Item(0), IIf(IsDBNull(oRow.Item(1)), "", oRow.Item(1))))
    Catch ex As Exception
    End Try
    Next

     

    Catch ex As Exception

     

    Finally

    Windows.Forms.Cursor.Current = crntCursor

     

    End Try

     

     

    But the result doesn't show the REAL instances !! ???

    in REAL  i've got:

    MYPCNAME\SQLEXPRESS
    MYPCNAME\MAMUT
    PCSecond\SQLEXPRESS

    But the combobox show only:

    MYPCNAME
    PCSecond

    How  can i find all Instances ???


     

     

     

     


     

    Monday, March 22, 2010 10:23 AM