Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Get database name from connectionstring

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Luigi - 07 Jan 2008 09:31 GMT
Hi all,
how can I obtain the dabase name from a connectionString retrieved from a
ConfigurationManager.ConnectionStrings["MyConnectionString"]?

I need this to set the text value of a label.

Thanks in advance.

Luigi

Signature

Luigi
http://blogs.dotnethell.it/ciupaz/

Morten Wennevik [C# MVP] - 07 Jan 2008 09:41 GMT
Hi Luigi,

I haven't tested this, but if you create an SqlConnection it has a Database
property that might hold the value you need.  If the connection string is in
a specific format, you could also just parse the string, but connection
strings can come in many flavours (see www.connectionstrings.com).

Signature

Happy Coding!
Morten Wennevik [C# MVP]

> Hi all,
> how can I obtain the dabase name from a connectionString retrieved from a
[quoted text clipped - 5 lines]
>
> Luigi
Luigi - 07 Jan 2008 10:04 GMT
> Hi Luigi,
>
> I haven't tested this, but if you create an SqlConnection it has a Database
> property that might hold the value you need.  If the connection string is in
> a specific format, you could also just parse the string, but connection
> strings can come in many flavours (see www.connectionstrings.com).

Hi Morten,
yes, finally I've made like this:

string connString =
ConfigurationManager.ConnectionStrings["PrideConnectionString"].ToString();
             
SqlConnection connection = new SqlConnection(connString);
string database = connection.DataSource.ToString();
               
if (database == "MILDBBP2S")
                   this.lblCurrentEnviroment.Text = "Produzione";
               
else if (database == "MILDBBP2STEST")
                   this.lblCurrentEnviroment.Text = "Test";
               
else
                   this.lblCurrentEnviroment.Text = "Server unknown";

Thanks so much.

Luigi
Nicholas Paldino [.NET/C# MVP] - 07 Jan 2008 16:39 GMT
You can also use the SqlConnectionString class, which will give you more
detailed information about the individual properties of the connection
string.  It inherits from the DbConnectionStringBuilder, which most
providers should provide an implementation of to help with encapsulating the
logic of constructing (and parsing) connection strings.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

>> Hi Luigi,
>>
[quoted text clipped - 26 lines]
>
> Luigi

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.