3

I need to get the server name from my connection string in my web.config file. How can I parse this value out? I have tried split but it wont allow me to split on a = sign. How do I go about parsing this out?

<add name ="Connection" connectionString="server=L\sqlexpress;database=System;User ID=TEst;Password=pass"></add>

I need L\sqlexpress I know it seems simple but I'm not getting it. Thank you for your help.

2
  • Duplicate of Is there any connection string parser in C#?. Commented Jan 24, 2014 at 16:25
  • What do you mean it won't allow you to split on an equal sign character? What is the error message? Commented Jan 24, 2014 at 16:26

1 Answer 1

5

The class SqlConnectionStringBuilder could be used to initialize an instance with the connectionstring from your web.config. Then you could read the DataSource property

string conString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
SqlConnectionStringBuilder cns = new SqlConnectionStringBuilder(conString);
string dataSource = cns.DataSource;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.