We help IT Professionals succeed at work.

Getting connection information from connection string (App.Config) .... VB.NET / C#

milani_lucie
milani_lucie asked
on
Hi,

I want to get connection information from connection string:

1) Provider Name
2) Database Name
3) Server Name

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
             <add name="DigiBrd"
       connectionString="server=localhost;user id=****;Password=****;database=DigiBrd"
       providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
</configuration>

etc...

I do NOT want to parse the CONFIG file. I think so after reading the connection string, there are API methods / properties to get that info.

Thanks
Comment
Watch Question

Armand GSenior Developer
CERTIFIED EXPERT

Commented:
You could do something like this: (C#)

using System.Configuration;

string str = ConfigurationManager.ConnectionStrings["DigiBrd"].ConnectionString;
Armand GSenior Developer
CERTIFIED EXPERT

Commented:
Also, you need to reference the System.configuration.dll into your project so your ConfigurationManager object will function properly.

:)
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
On top of armchang's suggestion, and per the remainder of your question, with ConfigurationManager you can get the Provider by using ProviderName instead of ConnectionString:

string str = System.Configuration.ConfigurationManager.ConnectionStrings["DigiBrd"].ProviderName;

Open in new window


You can get the DB name by loading the connection string into a DBConnectionStringBuilder and then querying that value:

System.Data.Common.DbConnectionStringBuilder builder = new System.Data.Common.DbConnectionStringBuilder();
object value;

builder.ConnectionString = str;

builder.TryGetValue("database", out value);

string dbName = value.ToString();

Open in new window


TryGetValue returns a boolean indicating whether or not the given key could be found, so you can use that for some error checking.
To get database name one more option there.
//To get provider name.

string providerName = System.Configuration.ConfigurationManager.ConnectionString["DigiBrd"].ProviderName; 


//To get database name.
string sqlConnectionString= System.Configuration.ConfigurationManager.ConnectionString["DigiBrd"]; 


SqlConnection conn = new SqlConnection(sqlConnectionString);
string databaseName = conn.DataSource.ToString();

Open in new window

Armand GSenior Developer
CERTIFIED EXPERT
Commented:
EaswaranP: I believe you're comment already duplicate with mine and kaufmed. So you don't need to repeat it.

//To get provider name.

string providerName = System.Configuration.ConfigurationManager.ConnectionString["DigiBrd"].ProviderName;


//To get database name.
string sqlConnectionString= System.Configuration.ConfigurationManager.ConnectionString["DigiBrd"];
I mentioned that "To get database name one more option there.".

I show option to to get Database name from SqlConnection object. I don't repeat your code.
Armand GSenior Developer
CERTIFIED EXPERT

Commented:

Look carefully at our posts #1 and #2, see if you have similar codes.
Armand GSenior Developer
CERTIFIED EXPERT

Commented:
kaufmed also have the code already in his first code highlight. Below is the code you also duplicate:

string providerName = System.Configuration.ConfigurationManager.ConnectionString["DigiBrd"].ProviderName;
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
@armchang
While I appreciate that we are all trying to help milani_lucie, please realize that your original post only clarifies how to get the connection string, and not how to get the individual parts of the connection string. Specifically, the parts in question are:

Provider Name
Database Name
Server Name

Your post does not demonstrate how to access these individual parts. Does the code posted by myself and others duplicate yours--yes, but only to a point. And that duplication was necessary to properly answer the question.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
P.S.

I even gave you credit in my post  ; )

Explore More ContentExplore courses, solutions, and other research materials related to this topic.