Solved

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

Posted on 2015-01-12
3
284 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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.…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now