-1

In C# I am trying to get the Data Source and initial catalog properties from a connection string.

So if I have

metadata=res://*/model.csdl|res://*/model.ssdl|res://*/model.msl;provider=System.Data.SqlClient;provider connection string="data source=myDESKTOP;initial catalog=dbName;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"

How can I extract just the values myDESKTOP and dbName regardless of case on the left hand of the expression?

1

1 Answer 1

2

You could dissect your connection string for Entity Framework and extract the internal basic connection string. At that point the SqlConnectionStringBuilder could give you the individual parts that compose the connection string

string test = "metadata=res://*/model.csdl|res://*/model.ssdl|res://*/model.msl;provider=System.Data.SqlClient;provider connection string=\"data source = myDESKTOP; initial catalog = dbName; integrated security = True; MultipleActiveResultSets = True; App = EntityFramework";

string con = string.Join("=", 
                    string.Join(";", test.Split(';').Skip(2))
                          .Split('=').Skip(1)).Trim('"');

SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder(con);
Console.WriteLine(sb.DataSource);
Console.WriteLine(sb.InitialCatalog);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.