I managed to get the name of all databases present in my server using the code below, but I don't know how to create the connection string for each.
List<string> list = new List<string>();
string connectionString = "Data Source=(localdb)\\MSSQLLocalDB; Integrated Security=True;";
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT name from sys.databases", con))
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
list.Add(dr[0].ToString());
}
}
}
}
