Click here to Skip to main content
11,928,352 members (50,437 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#2.0 C# database

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

from connection string
Database=DbName;Server=SomeServer;Integrated Security=SSPI;
Database=DbName;Server=SomeServer;User Id=myUsername;Password=myPassword;
Posted 22-Mar-10 0:38am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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
Rate this: bad
Please Sign up or sign in to vote.

Solution 6

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.
ProgramFOX 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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151126.1 | Last Updated 22 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100