开发者

Get's database's name?

How can I get’s all database’s name that created in sql server in local system. 开发者_开发百科thanks..


The following SQL command will get you all of the names of the databases on the server where it is executed. To see more information, change name to *.

SELECT name
  FROM sys.databases


Here is the code (you may want to check for the details from here: 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;
        }


The most platform-independent way would be to query the information_schema. SQL Server also offers a range of system tables / views which can be used for the same purpose; they offer more information (especially SQL Server specific), but they aren't portable, not even across different versions of SQL Server.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜