获取数据库列表取决于所选服务器 [英] Get list of database depends on chosen server

查看:53
本文介绍了获取数据库列表取决于所选服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将C#与Framework 4.0和SQL Server 2008 R2结合使用。
我已经用以下代码列出了SQL Server 2008:

I'm using C# with framework 4.0 and SQL server 2008 R2. I have listed the SQL server 2008 with this code:

 public static string[] GetSQLServerList()
        {
            SqlDataSourceEnumerator dse = SqlDataSourceEnumerator.Instance;
            DataTable dt = dse.GetDataSources();
            if (dt.Rows.Count == 0)
            {
                return null;
            }

            string[] SQLServers = new string[dt.Rows.Count];
            int f = -1;
            foreach (DataRow r in dt.Rows)
            {
                string SQLServer = r["ServerName"].ToString();
                string Instance = r["InstanceName"].ToString();
                if (Instance != null && !string.IsNullOrEmpty(Instance))
                {
                    SQLServer += "\\" + Instance;
                }
                SQLServers[System.Math.Max(System.Threading.Interlocked.Increment(ref f), f - 1)] = SQLServer;
            }
            Array.Sort(SQLServers);
            return SQLServers;
        }

我在ComboBox上列出了我的服务器。

i listed my Server on ComboBox.

如何列出数据库,取决于我在ComboBox中选择的服务器?

How can I list the database, depends on which server that I choose in ComboBox?

我发现了这个教程,但它需要 sqlconnection ,未选择服务器时如何连接?

I found this tutorial, but it needs sqlconnection, how can I connect when I have not chosen the server ?

推荐答案

这是获取服务器名称列表的方式在网络上:

This is how you get a list of server names on the network:

List<String> ServerNames = new List<String>();

 SqlDataSourceEnumerator servers = SqlDataSourceEnumerator.Instance;
 DataTable serversTable = servers.GetDataSources();

     foreach (DataRow row in serversTable.Rows) {
            string serverName = row[0].ToString();

             try {

                if (row[1].ToString() != "") {

                            serverName += "\\" + row[1].ToString();

                }


              }
              catch {


              }

              ServerNames.Add(serverName);
      }

要从选定服务器获取数据库列表:

To Get a List of databases from selected server:

List<String> databases = new List<String>();

SqlConnectionStringBuilder connection = new SqlConnectionStringBuilder();

 connection.DataSource = SelectedServer;
 // enter credentials if you want
 //connection.UserID = //get username;
// connection.Password = //get password;
 connection.IntegratedSecurity = true;

 String strConn = connection.ToString();

 //create connection
  SqlConnection sqlConn = new SqlConnection(strConn);

//open connection
sqlConn.Open();

 //get databases
DataTable tblDatabases = sqlConn.GetSchema("Databases");

//close connection
sqlConn.Close();

//add to list
foreach (DataRow row in tblDatabases.Rows) {
      String strDatabaseName = row["database_name"].ToString();

       databases.Add(strDatabaseName);


}     

这篇关于获取数据库列表取决于所选服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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