List SQL Server databases in C#.net

How to List SQL Server databases in C#.net

using System.Data;
using System.Data.SqlClient;

// change your connection string below in connString

String connString =
   "Data Source=LegalServer; Integrated Security=True;";
 
using (SqlConnection sqlConx = new SqlConnection (connString))
   {
   sqlConx.Open();
   DataTable tblDatabases = sqlConx.GetSchema ("Databases");
   sqlConx.Close();
 
   foreach (DataRow row in tblDatabases.Rows)
   {
      Console.WriteLine ("Database: " + row["database_name"]);
   }
}

By Kalit Sikka   Popularity  (4574 Views)