Question

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.

Was it helpful?

Solution

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top