Database Connection String Info

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

Answers


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;

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.


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.


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


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


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

Need Your Help

Suggest what user could buy if he already has something in the cart

mysql database machine-learning

I am developing e-shop where I will sell food. I want to have a suggestion box where I would suggest what else my user could buy based on what he's already have in cart. If he has beer, I want him to

How to pass variables and data from PHP to JavaScript?

javascript php

I have a variable in PHP, and I need its value in my JavaScript code. How can I get my variable from PHP to JavaScript?