Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# SQL Connection string regex pattern to parse sections

stealthrt

New Coder
Hey all I am in need of getting a regex so that i can parse a sql connection string out.

I am currently doing so with .Split() but that is proving to not be very reliable.

The connection string looks like this:
dbConnectionString=jdbc:sqlserver://theServerName:1433;database=theDBName;intergratedSecurity=true;encrypt=true;trustServerCertificate=true
The .Split() code I am currently using is this (data being the connection string above):

C#:
string[] values = data.Split('=');

switch ((entry.Key.ToLower()))
{
  case "dbconnectionstring":
    string[] __values = data.Split(':');
    string[] ___values = __values[3].Split(';');

    txt_box1 = __value[0].ToLower().Replace("dbConnectionString", "") + ":" + __values[1].ToLower();
    txt_box2 = __values[2].Replace("//", "");
    txt_box3 = ___values[0];
    break;
  case "database":
    txt_box4 = _values[1];
    break;
  case "encrypt":
    txt_box5 = _values[1];
    break;
  case "trustServerCertificate":
    txt_box6 = _values[1];
    break;
}

It gets messed up between the splits of ":" and ";" so this is why I would like to figure out a regex pattern so its much easier (and more reliable) than doing this .Split() stuff. I'm just not really good at regex!

What I am needing to get is the format:

Code:
jdbc:sqlserver://
theServerName
1433
theDBName
true
true
true

And help would be great!
 
Solution
Your C# code looks not just incomplete but hopelessly muddled. I see these variables being used:

Code:
values
__values
___values
__value
_values

Does this even compile ? Even if it does, code like this seems a sure way to get yourself in trouble.

I am no good at regex either - often I'd rather write half a page of code than try and concoct some cryptic and unreadable regex pattern. In this case, I would not go for regex anyway because the property/value pairs in a dbConnectionString can be in any order.

It would be better if you post complete code, and explain what exactly goes wrong in your code, instead of saying it's not reliable or it gets messed up. Keep in mind that the word dbconnectionstring is not part of the...
Your C# code looks not just incomplete but hopelessly muddled. I see these variables being used:

Code:
values
__values
___values
__value
_values

Does this even compile ? Even if it does, code like this seems a sure way to get yourself in trouble.

I am no good at regex either - often I'd rather write half a page of code than try and concoct some cryptic and unreadable regex pattern. In this case, I would not go for regex anyway because the property/value pairs in a dbConnectionString can be in any order.

It would be better if you post complete code, and explain what exactly goes wrong in your code, instead of saying it's not reliable or it gets messed up. Keep in mind that the word dbconnectionstring is not part of the connection string - you seem to assume it is. The general format is

jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
 
Solution
Your C# code looks not just incomplete but hopelessly muddled. I see these variables being used:

Code:
values
__values
___values
__value
_values

Does this even compile ? Even if it does, code like this seems a sure way to get yourself in trouble.

I am no good at regex either - often I'd rather write half a page of code than try and concoct some cryptic and unreadable regex pattern. In this case, I would not go for regex anyway because the property/value pairs in a dbConnectionString can be in any order.

It would be better if you post complete code, and explain what exactly goes wrong in your code, instead of saying it's not reliable or it gets messed up. Keep in mind that the word dbconnectionstring is not part of the connection string - you seem to assume it is. The general format is

jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
Thanks for the help there. I got it working now!
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom