Listing available Databases using c#

Introduction

This blogs list all the database available in your database server(SQL) using ADO.NET, taking help of GetSchema() method of Connection class you can list databases. see the bellow code for clarification.

SqlConnection connection=new SqlConnection("ConnectionString");
connection.Open();
DataTable table = connection.GetSchema("Databases");
foreach (DataRow Row in table.Rows) 
{
Console.WriteLine("Database Name" + " " + "Create Date ");
Console.WriteLine( Row[0].ToString()+" " + Row[2].ToString());
}

In above code I have used GetSchema method of connection class, this method returns datatable filling the  database details. I created a object of datatable called table and the return datatable of GetSchema function assigned to it. Then retrieved all the row of table and displayed their value such as database name and create date.