Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

22 Experts available now in Live!

Get 1:1 Help Now