-1

Let's say I have a string

metadata=res://*/Mapping.Models.csdl|res://*/Mapping.Models.ssdl|res://*/Mapping.Models.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string="data source=TESTSERVER;password=pass;persist security info=True;user id=id\"

How can I get the string as below:

data source=TESTSERVER;password=pass;persist security info=True;user id=id
SpiderCode
  • 10,062
  • 2
  • 22
  • 42
king jia
  • 692
  • 8
  • 20
  • 43

7 Answers7

6

if you want to parse the string mentioned then you can use the EntityConnectionStringBuilder.

There are Properties like ProviderConnectionString which gives you the string you want.

Example

var myEntityConnection = @"metadata=res://*/Mapping.Models.csdl|res://*/Mapping.Models.ssdl|res://*/Mapping.Models.msl;provider=Oracle.ManagedDataAccess.Client;provider connectionstring='""data source=TESTSERVER;password=pass;persist security info=True;user id=id";

var builder = new EntityConnectionStringBuilder(myEntityConnection);
Console.WriteLine(builder.ProviderConnectionString);

Thats all you Need :)

S.L.
  • 1,056
  • 7
  • 15
1

Why do you need to substring connectionstring even that is possible using EntityConnectionStringBuilder as mentioned below :

var connString = new EntityConnectionStringBuilder(ConfigurationManager.ConnectionStrings["ConnectionStringKey"].ConnectionString);

And from it you can retrieve ProviderConnectionString as shown below:

var providerString = connString.ProviderConnectionString;

For Oracle:

For oracle you can use OracleConnectionStringBuilder

SpiderCode
  • 10,062
  • 2
  • 22
  • 42
  • Hi... Because I want to use OracleDependency but EF doesn't provide.. So I have to extract the Connection String.......... and use OracleConnection – king jia Aug 11 '14 at 08:01
  • @kingjia : For that you can use OracleConnectionBuilder. Have a look at my updated answer. – SpiderCode Aug 11 '14 at 08:04
0

You can use a Regex to get the connection string if you want:

(data source).*[^"]
Complexity
  • 5,682
  • 6
  • 41
  • 84
  • 2
    The order of elements within a provider connection string is arbitrary; the "data source" doesn't have to be the first. – CodeCaster Aug 11 '14 at 07:46
0

Try splitting string by the given delimiter. Then concatenate, the array elements.

string[] stringArray = str.Split(new string[] { "=" }, StringSplitOptions.None);

Semih Yagcioglu
  • 4,011
  • 1
  • 26
  • 43
0

You can use String.Substring by first seraching the sub-string provider connection string=:

int index = text.IndexOf(@"provider connection string=");
string result = text.Substring(index).Trim('"');
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

Try this regex:

\"[^\"]*\"

or

\".*?\"

explain :

[^ character_group ]

Negation: Matches any single character that is not in character_group.

*?

Matches the previous element zero or more times, but as few times as possible.

and a sample code:

foreach(Match match in Regex.Matches(inputString, "\"([^\"]*)\""))
Console.WriteLine(match.ToString());

//or in LINQ

var result = from Match match in Regex.Matches(line, "\"([^\"]*)\"") 
         select match.ToString();
Jay Nirgudkar
  • 426
  • 4
  • 18
0

You can use SubString logic.

        string strConnection = @"metadata=res://*/Mapping.Models.csdl|res://*/Mapping.Models.ssdl|res://*/Mapping.Models.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string='""data source=TESTSERVER;password=pass;persist security info=True;user id=id";

        strConnection = strConnection.Substring(strConnection.LastIndexOf("data source"));
Mukesh
  • 1
  • almost there.... but contain '\' data source=TESTSERVER;password=pass;persist security info=True;user id=id\ – king jia Aug 11 '14 at 07:58