Solved

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

756 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