Link to home
Start Free TrialLog in
Avatar of milani_lucie
milani_lucieFlag for United States of America

asked on

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

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
Avatar of Armand G
Armand G
Flag of New Zealand image

You could do something like this: (C#)

using System.Configuration;

string str = ConfigurationManager.ConnectionStrings["DigiBrd"].ConnectionString;
Also, you need to reference the System.configuration.dll into your project so your ConfigurationManager object will function properly.

:)
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

Look carefully at our posts #1 and #2, see if you have similar codes.
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;
@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.
P.S.

I even gave you credit in my post  ; )