How to list databases from DB?

Here is sample for listing the databases from DB (SQLServer) in ASP.NET:

string connectionString = "server=ServerName;uid=sa;pwd=pwd;";

using (SqlConnection connection = new SqlConnection(connectionString))

{

connection.Open();
DataTable dt= connection.GetSchema("Databases");
connection.Close();
foreach (DataRow row in dt.Rows)
{
Response.Output.WriteLine("Database: " + row["database_name"]);
}
}

No comments: