SQL Connection string regex pattern to parse sections

Joined
Jul 6, 2023
Messages
1
Reaction score
0
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:

C#:
jdbc:sqlserver://
theServerName
1433
theDBName
true
true
true

And help would be great!
 
Joined
Jul 4, 2023
Messages
370
Reaction score
41
Your idea is not bad, but try using Split just like that.
C#:
using System;

class HelloWorld {
    static void Main() {
        string connectionString = "dbConnectionString=jdbc:sqlserver://theServerName:1433;database=theDBName;integratedSecurity=true;encrypt=true;trustServerCertificate=true";
        string[] parts = connectionString.Split(";");
    
        /* for presentation purposes */
        string txt_box1 = "", txt_box2 = "", txt_box3 = "", txt_box4 = "",
               txt_box5 = "", txt_box6 = "", txt_box7 = "";
    
        foreach (string part in parts)
        {
            string[] keyValue = part.Split("=");
            string key = keyValue[0].ToLower();
            string value = keyValue[1] ?? "";
        
            switch (key)
            {
                case "dbconnectionstring":
                    txt_box1 = value.Split("//")[0] + "//";
                    txt_box2 = value.Split("//")[1].Split(":")[0] ?? value.Split("//")[1];
                    txt_box3 = value.Split("//")[1].Split(":")[1] ?? "";
                    break;
                case "database":
                    txt_box4 = value;
                    break;
                case "integratedsecurity":
                    txt_box5 = value;
                    break;
                case "encrypt":
                    txt_box6 = value;
                    break;
                case "trustservercertificate":
                    txt_box7 = value;
                    break;                   
            }
        }
    
        /* for presentation purposes */
        Console.WriteLine("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}", txt_box1, txt_box2, txt_box3, txt_box4, txt_box5, txt_box6, txt_box7);
    }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,614
Members
45,289
Latest member
stfp04

Latest Threads

Top