473,408 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Get database name from connectionstring

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

--
Luigi
http://blogs.dotnethell.it/ciupaz/
Jan 7 '08 #1
3 20599
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).

--
Happy Coding!
Morten Wennevik [C# MVP]
"Luigi" wrote:
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

--
Luigi
http://blogs.dotnethell.it/ciupaz/
Jan 7 '08 #2
"Morten Wennevik [C# MVP]" wrote:
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).

--
Happy Coding!
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
Jan 7 '08 #3
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Luigi" <ci****************@inwind.itwrote in message
news:FD**********************************@microsof t.com...
"Morten Wennevik [C# MVP]" wrote:
>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).

--
Happy Coding!

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

Jan 7 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: markaelkins | last post by:
I want to create a simple user interface to collect the following data and store the data in a SQL database…. Could someone please help me get started? Data to collect from user interface and...
4
by: brent | last post by:
Currently our application is reading from the web.config, and retrieves a section, "softwareRequirements". Because the section will begin to grow dramatically, I'd like to place it in the SQL...
2
by: RSH | last post by:
I am struggling a bit trying to get at all of the Table names in a given Access database. I have the code below which should be retrieving the information...i am just having a bit of trouble...
0
by: Jonathan Wood | last post by:
I seem to be having errors creating and accessing an SQL database. Unfortunatley, I am brand new to SQL setup and administration issues so this really is not my area of expertise. I know I had...
5
by: igotyourdotnet | last post by:
I'm trying to add a web part to my page and when I run my page with the web part I get this error: An error has occurred while establishing a connection to the server. When connecting to SQL...
4
by: =?Utf-8?B?QmFyYmFyYSBBbGRlcnRvbg==?= | last post by:
I setup a simple gridview as a utility just to do some updates, nothing fancy just wanted easy UI to make updates. When I select ‘Edit’, I get the fields I want to edit. I edit them and click...
4
by: shapper | last post by:
Hello, I am using Microsoft Enterprise Library, on a class, to connect to a database as follows: Dim db As Database = DatabaseFactory.CreateDatabase("DatabaseName") However, I would like...
5
by: Edwin Smith | last post by:
Hello: I'm using VS2005 with an ODBC database. I am trying to add a Dialog to an existing Database application to allow the user to select the database. We have 2 different databases with the...
1
by: priyamtheone | last post by:
What's the connectionstring to connect to the Oracle9.0 Database Server irrespective of servername,username and password using VB.Net2002 (.Net framework 1.0)? In case of Microsoft SQL Server the...
1
Curtis Rutland
by: Curtis Rutland | last post by:
How To Use A Database In Your Program Part II This article is intended to extend Frinny’s excellent article: How to Use a Database in Your Program. Frinny’s article defines the basic concepts...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.