使用数据库填充再次卡住 [英] Stuck Again Using Database Stuff

查看:94
本文介绍了使用数据库填充再次卡住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里的目标是允许用户选择要用于存储信息的SQL Server实例.我们在网络上运行着几个版本,因此我必须让用户自己找到正确的位置.

根据监视窗口"的输出,我获取可用服务器表的代码运行良好,但是我无法弄清楚如何以用户可以看到的方式显示它.我尝试使用DataGridView,但它不允许我绑定到尚未创建的表:

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);
            
        }
    }
}



对DisplayData()的调用会在监视窗口中显示正确的信息,但是当我尝试将DataGridView绑定到新创建的数据集表Servers时,它不会允许我.

我希望用户看到所有可用的SQL Server实例,并选择目标数据库,但是我不知道下一步要去哪里.今晚我搜索了几百篇文章,但是与这个特定问题有关的价值还不高.

如何显示动态生成的表,并允许用户选择可用服务器的一个实例?



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?

推荐答案

我使用以下命令:
I use this:
private void GetServers()
{
    this.cboxServers.Items.Clear();
    using (DataTable dt = SmoApplication.EnumAvailableSqlServers())
    {
        foreach (DataRow dr in dt.Rows)
        {
            this.cboxServers.Items.Add(dr[0]);
        }
    }
}



可以在Microsoft.SqlServer.Management.Smo
中找到SmoApplication
显然,您可以用ListBox代替我的ComboBox.

EnumAvailableSqlServers的MSDN页面 [



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. :)


记录下来,效果很好:

For the record, this worked quite nicely:

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!


这篇关于使用数据库填充再次卡住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆