我有一个组合框中的服务器列表。我想在选择服务器名称时将数据库列表放到另一个组合框中。我的代码如下 [英] I have a list of servers in a combobox. I want to get the list of databases into an other combo box when i select the server name. my code is below

查看:59
本文介绍了我有一个组合框中的服务器列表。我想在选择服务器名称时将数据库列表放到另一个组合框中。我的代码如下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 private void Form1_Load(object sender,EventArgs e)
{

SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
DataTable table = instance.GetDataSources();
cboservers.DataSource = table;
cboservers.DisplayMember =ServerName;

}
//当我选择servername

解决方案

尝试:

 使用(SqlConnection con =  new  SqlConnection( @  Data Source = GRIFFPC\SQLEXPRESS; Integrated Security = True))
{
con.Open();
使用(SqlCommand cmd = new SqlCommand( sp_databases,con))
{
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader read = cmd.ExecuteReader();
while (read.Read())
{
Console.WriteLine(( string )读取[ DATABASE_NAME]);
}
}
}

这将列出单个服务器上的数据库。


private void Form1_Load(object sender, EventArgs e)
        {

            SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
            DataTable table = instance.GetDataSources();
            cboservers.DataSource = table;
            cboservers.DisplayMember = "ServerName";
            
        }
//i want to get the database names into an other combobox when i select servername

解决方案

Try:

using (SqlConnection con = new SqlConnection(@"Data Source=GRIFFPC\SQLEXPRESS;Integrated Security=True"))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("sp_databases", con))
        {
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataReader read = cmd.ExecuteReader();
        while (read.Read())
            {
            Console.WriteLine((string)read["DATABASE_NAME"]);
            }
        }
    }

That will list the databases on a single server.


这篇关于我有一个组合框中的服务器列表。我想在选择服务器名称时将数据库列表放到另一个组合框中。我的代码如下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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