StrataFrame Forum

Enumerate Database Names

http://forum.strataframe.net/Topic7159.aspx

By Kent Meyer - 2/24/2007

How do I get a list of Databases available on a Server?
By Kent Meyer - 2/25/2007

Here is one way....

 

SqlConnectionStringBuilder cnStr = new SqlConnectionStringBuilder();

cnStr.UserID = this.txtUsername.Text;

cnStr.Password = this.txtPassword.Text;

cnStr.DataSource = this.cboSQLServer.Text;

cnStr.ConnectTimeout = 30;

SqlConnection cn = new SqlConnection();

cn.ConnectionString = cnStr.ConnectionString;

cn.Open();

System.Data.DataTable dbList = cn.GetSchema("Databases");

foreach (System.Data.DataRow row in dbList.Rows)

{

this.cboDatabase.Items.Add(row[0]);

}

By StrataFrame Team - 2/26/2007

Yes, the GetSchema() is probably the preferred way to do it.  You can also look at the sys.sysobjects view and I think there is a sys.sysdatabases view, but using GetSchema() is version independent.  Also, if you call GetSchema() and don't pass it any restrictions, it will give you a DataTable containing the different schema objects that can be queried and the restrictions that are needed to narrow each of the selections.
By Kent Meyer - 2/27/2007

I was just wondering if there was a way do do this using the Strataframe connection manager...
By StrataFrame Team - 2/27/2007

Ah, the ConnectionManager uses the GetSchema() method internally, but I don't believe that the method is exposed to enumerate the databases.  We have a background worker thread that gathers the available databases when the you click the drop-down for the database selection.