Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removed database out of connection string

I was given a method to get our database connection string to Sql Server:

SqlConnection GetConnectionString()

I call that and get what the connection string should be. If the database does not exist, I need the connection string without the database name in it. If I try to use the connection string with the database name in it, I get an error that it cannot connect to the database, which is it since it does not exist.

I am calling like this:

using (var connection = new SqlConnection(GetConnectionString().ConnectionString))

Is there a way to recreate the connection string easily without the database name?

like image 271
ErocM Avatar asked Dec 14 '22 16:12

ErocM


1 Answers

SqlConnectionStringBuilder aBuilder = 
   new SqlConnectionStringBuilder(yourConnectionStringWithDatabase);
aBuilder.InitialCatalog = "";

string yourConnectionStringWithoutDatabase = aBuilder.ConnectionString;
like image 126
MutantNinjaCodeMonkey Avatar answered Dec 29 '22 14:12

MutantNinjaCodeMonkey