Get database name from connectionstring

  • Thread starter Thread starter Luigi
  • Start date Start date
L

Luigi

Hi all,
how can I obtain the dabase name from a connectionString retrieved from a
ConfigurationManager.ConnectionStrings["MyConnectionString"]?

I need this to set the text value of a label.

Thanks in advance.

Luigi
 
Hi Luigi,

I haven't tested this, but if you create an SqlConnection it has a Database
property that might hold the value you need. If the connection string is in
a specific format, you could also just parse the string, but connection
strings can come in many flavours (see www.connectionstrings.com).
 
Morten Wennevik said:
Hi Luigi,

I haven't tested this, but if you create an SqlConnection it has a Database
property that might hold the value you need. If the connection string is in
a specific format, you could also just parse the string, but connection
strings can come in many flavours (see www.connectionstrings.com).

Hi Morten,
yes, finally I've made like this:

string connString =
ConfigurationManager.ConnectionStrings["PrideConnectionString"].ToString();

SqlConnection connection = new SqlConnection(connString);
string database = connection.DataSource.ToString();

if (database == "MILDBBP2S")
this.lblCurrentEnviroment.Text = "Produzione";

else if (database == "MILDBBP2STEST")
this.lblCurrentEnviroment.Text = "Test";

else
this.lblCurrentEnviroment.Text = "Server unknown";

Thanks so much.

Luigi
 
You can also use the SqlConnectionString class, which will give you more
detailed information about the individual properties of the connection
string. It inherits from the DbConnectionStringBuilder, which most
providers should provide an implementation of to help with encapsulating the
logic of constructing (and parsing) connection strings.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Luigi said:
Morten Wennevik said:
Hi Luigi,

I haven't tested this, but if you create an SqlConnection it has a
Database
property that might hold the value you need. If the connection string is
in
a specific format, you could also just parse the string, but connection
strings can come in many flavours (see www.connectionstrings.com).

Hi Morten,
yes, finally I've made like this:

string connString =
ConfigurationManager.ConnectionStrings["PrideConnectionString"].ToString();

SqlConnection connection = new SqlConnection(connString);
string database = connection.DataSource.ToString();

if (database == "MILDBBP2S")
this.lblCurrentEnviroment.Text = "Produzione";

else if (database == "MILDBBP2STEST")
this.lblCurrentEnviroment.Text = "Test";

else
this.lblCurrentEnviroment.Text = "Server unknown";

Thanks so much.

Luigi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top