2010-08-10 9 views

답변

4

다음 SQL 명령은 실행되는 서버의 데이터베이스 이름을 모두 가져옵니다. 자세한 내용을 보려면 name*으로 변경하십시오.

SELECT name 
    FROM sys.databases 
2

가장 플랫폼 독립적 인 방법은 information_schema을 쿼리하는 것입니다. 또한 SQL Server는 같은 목적으로 사용할 수있는 다양한 시스템 테이블/뷰를 제공합니다. 더 많은 정보 (특히 SQL Server에만 해당)를 제공하지만 이식성이 아니며 SQL Server의 다른 버전에서도 마찬가지입니다. 그것을 해결 ...

 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; 
     }