Click here to Skip to main content
15,887,333 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How to get :
- database
- server
- user
- password
- is integrated security

from connection string
Database=DbName;Server=SomeServer;Integrated Security=SSPI;
or
Database=DbName;Server=SomeServer;User Id=myUsername;Password=myPassword;
Posted

Quote:
System.Data.Common.DbConnectionStringBuilder builder = new System.Data.Common.DbConnectionStringBuilder();

builder.ConnectionString = ConfigurationManager.ConnectionStrings["inventoryDBConnection"].ConnectionString; ;

string server = builder["Data Source"] as string;
string database = builder["Initial Catalog"] as string;
string UserID = builder["User ID"] as string;
string password = builder["Password"] as string;



This way you can get all the properties.
You may find more help from this MSDN's link:
msdn's link

Hope this will help you.
Thanks.
 
Share this answer
 
Comments
Thomas Daniels 22-Dec-12 12:12pm    
This is a question from 2 years ago. Why do you answer to it? The question is solved already.
Jibesh 22-Dec-12 19:29pm    
:O thats interesting how the author dig this out where plenty of unanswered question left unattended!!

dhruvsheth 23-Dec-12 8:05am    
sorry boss but I was searching for same question and i found this link where i didn't found appropriate ans. so did. so someone will be helped.
VBNetHack 13-Jun-17 12:36pm    
Well nearly five years later I found your answer helpful, thanks dhruvsheth :)
dhruvsheth 28-Jun-17 1:38am    
Thanks for appreciating.
You have to parse the string. There are a couple of ways to do it, and which way you do it is completely based on your own needs.

0) You can use the string.Split method, splitting on the ';' character, and then parsing the resulting substrings

1) You can use the string.IndexOf() method that walks the string looking for specific items.
 
Share this answer
 
Did you got a chance to look at:
SqlConnection..::.ConnectionString Property[^]

Once you assign the connection string to SqlConnection object, you can access them separately.

For ex:
using (SqlConnection conn = new SqlConnection())
{
    conn.ConnectionString = connectionString;
    // Like this:
    //  conn.DataSource
    //  conn.Database

    //
    //  some work
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900