Get List Of Database Name From SQL Server In C#

In real time scenarios, we validate the database connection string in our application. In the meantime, it will throw exceptions. If the connection is not valid at the time, we cannot judge easily whether its connection string is Invalid or our server doesn't contain that respective database. So, in this blog, I will explain how to get the list of database name from SQL Server using C# to help find the root cause of the problem and to notify the exact error to the user or developer. 
 
sys.databases 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Data.SqlClient;  
  7. using System.Data;  
  8. namespace SQLTableListInCSharp {  
  9.     class Program {  
  10.         static void Main(string[] args) {  
  11.             string connectionString = "Data Source=.; Integrated Security=True;";  
  12.             using(SqlConnection con = new SqlConnection(connectionString)) {  
  13.                 con.Open();  
  14.                 using(SqlCommand cmd = new SqlCommand("SELECT name from sys.databases", con)) {  
  15.                     using(SqlDataReader dr = cmd.ExecuteReader()) {  
  16.                         while (dr.Read()) {  
  17.                             Console.WriteLine(dr[0].ToString());  
  18.                         }  
  19.                     }  
  20.                 }  
  21.             }  
  22.         }  
  23.     }  
  24. }  
In the above code, you can see that we can get the list of database names using sys.databases and the result will be as below.
 
 
 
Now, with the help of this list, we can validate the database connection string. Another way we can get the list of database name is by using GetSchema.
 
GetSchema 
  1. using(var con = new SqlConnection(connectionString)) {  
  2.     con.Open();  
  3.     DataTable databases = con.GetSchema("Databases");  
  4.     foreach(DataRow database in databases.Rows) {  
  5.         String databaseName = database.Field < String > ("database_name");  
  6.         short dbID = database.Field < short > ("dbid");  
  7.         DateTime creationDate = database.Field < DateTime > ("create_date");  
  8.         Console.WriteLine(databaseName + "," + dbID + "," + creationDate);  
  9.     }  
  10. }  
And, the result will be -
 
 
 
Now, you can use any one option to validate the connection string.
 
Complete Code 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Data.SqlClient;  
  7. using System.Data;  
  8. namespace SQLTableListInCSharp {  
  9.     class Program {  
  10.         static void Main(string[] args) {  
  11.             string connectionString = "Data Source=.; Integrated Security=True;";  
  12.             using(SqlConnection con = new SqlConnection(connectionString)) {  
  13.                 con.Open();  
  14.                 using(SqlCommand cmd = new SqlCommand("SELECT name from sys.databases", con)) {  
  15.                     using(SqlDataReader dr = cmd.ExecuteReader()) {  
  16.                         while (dr.Read()) {  
  17.                             Console.WriteLine(dr[0].ToString());  
  18.                         }  
  19.                     }  
  20.                 }  
  21.             }  
  22.             using(var con = new SqlConnection(connectionString)) {  
  23.                 con.Open();  
  24.                 DataTable databases = con.GetSchema("Databases");  
  25.                 foreach(DataRow database in databases.Rows) {  
  26.                     String databaseName = database.Field < String > ("database_name");  
  27.                     short dbID = database.Field < short > ("dbid");  
  28.                     DateTime creationDate = database.Field < DateTime > ("create_date");  
  29.                     Console.WriteLine("DatabaseName : " + databaseName + ", DatabaseID : " + dbID + ", CreationDate : " + creationDate);  
  30.                 }  
  31.             }  
  32.         }  
  33.     }  
  34. }  
I hope it's helpful.