11

In .Net is there a class in .Net where you can get the DB name, and all the connection string info without acutally doing a substring on the connection string?

EDIT:

I am not creating a connection I am attempting to get info out of the connection string. So I am basicly looking for something that takes a connection string arg and has accessors to dbName, connection type, etc....

6 Answers 6

25

You can use the provider-specific ConnectionStringBuilder class (within the appropriate namespace), or System.Data.Common.DbConnectionStringBuilder to abstract the connection string object if you need to. You'd need to know the provider-specific keywords used to designate the information you're looking for, but for a SQL Server example you could do either of these two things:

Given

string connectionString = "Data Source = .\\SQLEXPRESS;Database=Northwind;Integrated Security=True;";

You could do...

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

builder.ConnectionString = connectionString;

string server = builder["Data Source"] as string;
string database = builder["Database"] as string;

Or

System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();

builder.ConnectionString = connectionString;

string server = builder.DataSource;
string database = builder.InitialCatalog;
Sign up to request clarification or add additional context in comments.

1 Comment

System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder(connectionString);
3

After you initialize the connection with the connection string, you can get those information from properties of the initialized connection object.

Check System.Data.Common.DbConnection.

1 Comment

This requires that you create a connection object, which seems like overkill for this application.
0
ConnectionInfo connectionInfo = new ConnectionInfo ();
connectionInfo = logOnInfo.ConnectionInfo;

connectionInfo.DatabaseName = database;
connectionInfo.ServerName = server;
connectionInfo.Password = password;
connectionInfo.UserID = user;

EDIT: Looks like Nathan beat me to it, as mine is from the same page.

EDIT AGAIN: I should note that ConnectionInfo is in the CrystalDecisions.Shared namespace. As for how to get it from a generic DB connection, I'm not sure.

1 Comment

isn't connectioninfo in the crystal reports library?
0

Yep ConnectionInfo

http://msdn.microsoft.com/en-us/library/ms226340(VS.80).aspx

EDIT: I, along with Chris, realized that this is only works if you have the Crystal Reports namespaces imported. Otherwise I'm not sure

Comments

0

if you build your connection string using the Connection string builder (e.g. OracleConnectionStringBuilde, and it will be different for different database), in that case easily retrieve the information out of it.

here it explained:

http://msdn.microsoft.com/en-us/library/ms254947.aspx

Comments

-3
SqlConnection sq = new SqlConnection(ConnectionString);

Reference

Done with "using" statement (from MSDN)

using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        // Do work here; connection closed on following line.
    }

2 Comments

he's looking to get information from the connectionstring without parsing. This allows him to use the connection, not determine what the database name is.
You're connecting to the database...there's no reason that he should have to do that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.