Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the value from connection string?

Tags:

c#

I have connection string:

<add name="fullDataBase"  
     connectionString="Data Source=.;Initial Catalog=Equipme;Integrated Security=True" 
     providerName="System.Data.SqlClient"/>

And then I can get name of catalog from it when I get this string using:

 string connectionString = ConfigurationManager.ConnectionStrings[_connectionString].ConnectionString;

In my case it should be Equipme

like image 579
revolutionkpi Avatar asked Nov 25 '25 11:11

revolutionkpi


2 Answers

Use the SqlConnectionStringBuilder class:

string initialCatalog = new SqlConnectionStringBuilder(connectionString).InitialCatalog;
like image 50
SLaks Avatar answered Nov 28 '25 02:11

SLaks


This is your solution

System.Data.SqlClient.SqlConnectionStringBuilder builder = New System.Data.SqlClient.SqlConnectionStringBuilder(System.Configuration.ConfigurationManager.ConnectionStrings("fullDataBase").ToString()))
string initialCatalog=builder.InitialCatalog()

The SqlConnectionStringBuilder will create a ConnectionString or it will break the connection string into individual parts line Database name etc..

like image 34
Kishore Kumar Avatar answered Nov 28 '25 00:11

Kishore Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!