Solved

ASP.Net Get Datasource name only from entity connection string?

Posted on 2015-01-12
3
288 Views
Last Modified: 2015-01-13
Hello all,

I have the following as an example:

<add name="CustomerEntities" connectionString="metadata=res://*/Customer.csdl|res://*/Customer.ssdl|res://*/Customer.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DB111-SERVER;initial catalog=Customer;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />

I need to get the server name only into a string variable so "DB111-SERVER".   I tried using the SQL SqlConnectionStringBuilder but it throws an error on metadata doesn't understand it.
0
Comment
Question by:sbornstein2
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
sudheeshthegreat earned 500 total points
ID: 40545574
Yes, SqlConnectionStringBuilder doesnt understand the metadata part.

Why is your connection string formed like this, containing another connection string inside it?

You could use simple string operations to split the connection string parts using the delimiter (;) and then extracting the data source name from it.
            var connString = ConfigurationManager.ConnectionStrings["CustomerEntities"].ConnectionString;
            var parts = connString.Split(';');
            var dataSourcePart = parts.FirstOrDefault(p => p.StartsWith("data source="));

Open in new window


Because of the strange connectionString in connectionString value that you have, you will get this item in the array
"provider connection string=\"data source=DB111-SERVER"
instead of
"data source=DB111-SERVER"
0
 

Author Comment

by:sbornstein2
ID: 40546670
Entity Framework needs the metadata to build your entity mappings.
0
 

Author Closing Comment

by:sbornstein2
ID: 40546984
tx
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question