Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
My goal here is to allow a user to select which instance of SQL Server to use for storing information. We have a couple of versions running on the network, so I have to let users find for themselves the right location.

According to the Watch Window output, my code for obtaining a table of available servers is workinh nicely, but I can't figure out how to display it in a way that the user can see. I tried using a DataGridView, but it won't let me bind to a table that has not yet been created:

namespace SQLTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static DataTable GetSources()
        {
            SqlDataSourceEnumerator MyEnum = SqlDataSourceEnumerator.Instance;
            DataTable SourceTable = MyEnum.GetDataSources();
            return SourceTable;
        }
        private DataTable Servers = new DataTable();
        private static void DisplayData(System.Data.DataTable table)
        {
            foreach (System.Data.DataRow row in table.Rows)
            {
                foreach (System.Data.DataColumn col in table.Columns)
                {
                    Console.WriteLine("{0} = {1}", col.ColumnName, row[col]);
                }
                Console.WriteLine("============================");
            }
        }

        private void btnSelectSource_Click(object sender, EventArgs e)
        {
            Servers = GetSources();
            DisplayData(Servers);
            
        }
    }
}


The call to DisplayData() shows the proper information in a watch window, but when I try to bind the DataGridView to the newly-created dataset table, Servers, it won't let me.

I want the user to see all the available instances of SQL Server, and to select a destination database, but I haven't a clue where to go next. I've searched a few hundred articles tonight, but haven't come away with much of value relating to this particular problem.

How do I display a table generated on the fly, and allow the user to select one instance of the servers available?
Posted
Comments
Henry Minute 25-Mar-11 1:31am    
Roger, if you go to this (http://sqlserversamples.codeplex.com/) page on codeplex select the Microsoft SQL Server Database Engine link and then download Microsoft Database Engine Samples (CLR, Full Text, SMO). When you have unpacked them, have a look at the Samples\Engine\Programmability\SMO\ServerConnect\CS solution (it's a VS2005 but updates with no problems to 2008). It shows a Server Selection Dialog. For how to use it look at the Samples\Engine\Programmability\SMO\ManageDatabases sample. Good stuff!

I use this:
C#
private void GetServers()
{
    this.cboxServers.Items.Clear();
    using (DataTable dt = SmoApplication.EnumAvailableSqlServers())
    {
        foreach (DataRow dr in dt.Rows)
        {
            this.cboxServers.Items.Add(dr[0]);
        }
    }
}


SmoApplication can be found in using Microsoft.SqlServer.Management.Smo

Obviously you can substitute a ListBox for my ComboBox.

MSDN Page for EnumAvailableSqlServers[^]

Hope this helps. :)
 
Share this answer
 
Comments
Wayne Gaylard 25-Mar-11 3:41am    
Thanks, learnt something new.
Roger Wright 25-Mar-11 23:39pm    
I may give it a try, Henry, but the method I'm using works fine across the network. I just couldn't figure out a way to get the returned table to display. A listbox would be far simpler than the DataGridView, but the book I have for reference is rather limited. Thanks!
For the record, this worked quite nicely:

MSIL
private void GetSources()
        {
            this.lbxSelectServer.Items.Clear();
            SqlDataSourceEnumerator MyEnum = SqlDataSourceEnumerator.Instance;
            DataTable SourceTable = MyEnum.GetDataSources();
            using (SourceTable)
            {
                foreach (DataRow dr in SourceTable.Rows)
                {
                    string item=dr[0].ToString() + '/' + dr[1].ToString();
                    this.lbxSelectServer.Items.Add(item);
                }
            }

        }

        private void btnSelectSource_Click(object sender, EventArgs e)
        {
            GetSources();

        }

        private void lbxSelectServer_SelectedIndexChanged(object sender, EventArgs e)
        {
            string Database = lbxSelectServer.SelectedItem.ToString();
            MessageBox.Show(Database); // Add functional code
        }


Thanks, Henry, for the excellent clues!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900