获取数据库名称?

时间:2010-08-10 07:47:10

标签: c# sql-server-2005

如何获取在本地系统的sql server中创建的所有数据库名称。 感谢..

3 个答案:

答案 0 :(得分:4)

以下SQL命令将获取执行它的服务器上的所有数据库名称。要查看更多信息,请将name更改为*

SELECT name
  FROM sys.databases

答案 1 :(得分:2)

与平台无关的最常用方法是查询information_schema。 SQL Server还提供了一系列可用于相同目的的系统表/视图;它们提供了更多信息(特别是特定于SQL Server),但它们不可移植,甚至不能跨越不同版本的SQL Server。

答案 2 :(得分:2)

以下是代码(您可能需要查看此处的详细信息:http://dataconnectionsuite.codeplex.com/SourceControl/changeset/view/57274#1206522

        try
        {
            this.databasesComboBox.Items.Clear();
            this.databasesComboBox.Items.Add("Please wait while loading available databases...");
            DataTable tables = new DataTable("Tables");
            using (IDbConnection connection = this.GetConnection())
            {
                IDbCommand command = connection.CreateCommand();
                command.CommandText = "SELECT * FROM sys.Databases";
                connection.Open();
                tables.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
            }
            this.databasesComboBox.Items.Clear();
            foreach (DataRow row in tables.Rows)
                this.databasesComboBox.Items.Add(row[0].ToString());
        }
        catch (SqlException)
        {
            this.databasesComboBox.Items.Clear();
            this.databasesComboBox.Items.Add("Connection error. Check server & credentials");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error while loading available databases: " + ex.ToString());
        }
        finally
        {
            DatabaseListCreating = false;
        }
相关问题