如何在combobox C#visual studio中从所选服务器获取数据库 [英] How to get databases from selected server in combobox C# visual studio

查看:149
本文介绍了如何在combobox C#visual studio中从所选服务器获取数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在第一个组合框中有2个组合框我插入了我的服务器列表....在我的第二个组合框中我想获得该特定服务器的数据库列表....请你建议我插入的代码第二个组合框中的数据库列表?.....我在第一个组合框中选择服务器列表的代码是这个



I have 2 combobox in first combo box I have inserted my server list....In my second combo box I want to get list of databases for that Particular server....Please can you suggest me the code to insert database list in second combobox?.....my code for selecting server list in first combo box is this

DataTable dt = SqlDataSourceEnumerator.Instance.GetDataSources();
           for (int i = 0; i < dt.Rows.Count; i++)
           {
               if ((dt.Rows[i]["InstanceName"] as string) != null)
                   comboBoxServer.Items.Add(dt.Rows[i]["ServerName"] + "\\" + dt.Rows[i]["InstanceName"]);
               else
                   comboBoxServer.Items.Add(dt.Rows[i]["ServerName"]);
           }





我尝试过:



我试过这段代码







What I have tried:

I have tried this code


string conString = "server=xeon;uid=sa;pwd=manager; database=northwind";

    using (SqlConnection con = new SqlConnection(conString))
    {
        con.Open();

        // Set up a command with the given query and associate
        // this with the current connection.
        using (SqlCommand cmd = new SqlCommand("SELECT name from sys.databases", con))
        {
            using (IDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    list.Add(dr[0].ToString());
                }
            }
        }
    }
    return list;

推荐答案

据我所见,其他一切都很好,除了你有一个恒定的连接字符串。要从选定的服务器获取数据库,请使用第一个组合框中所选项目的服务器名称。换句话说,比如

As far as I can see, everything else is fine except that you have a constant connection string. In order to fetch the databases from a selected server, use the server name from the selected item in your first combo box. In other words something like
string conString = "server=" + servercombo.SelectedItem.ToString() + ";uid=sa;pwd=manager";



此外,除非您拥有所有服务器的相同用户名和密码,否则需要提示输入要使用的用户名和密码。另一种选择是使用集成安全性,如果这更可行。


Also, unless you have the same user name and password for all servers, you need to prompt for the user name and password to use. Another option is to use integrated security if that's more feasible.


这篇关于如何在combobox C#visual studio中从所选服务器获取数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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