Link to home
Start Free TrialLog in
Avatar of MohitPandit
MohitPanditFlag for India

asked on

Regular Expression: To change connection string in web.config

Hi Folks,

Could you assist me to generate regular expression to change web.config connection string?
For example, below "DBString", I need to make change in server, user id, password like servername, userid password values respectively
<add name="DBString" connectionString="server=servername;database=DBName;user id=userid;password=password;Application Name=Web:TestApp" providerName="System.Data.SqlClient"/>

Open in new window


Could you assist into it?

Best Regards
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

I'm not sure the .NET code for it without Googling it, but I do know you can do a regex replace with .NET.

I can help with the pattern and replacement. You'd use a pattern like this to replace the servername (you may need to escape the double quotes):
(<add name="DBString" connectionString="[^"]*?server=)[^;]+

Open in new window

with a replacement:
$1newservername

Open in new window


The $1 part of the replacement ensures that the <add... part of the string doesn't get removed during the replacement.

Then just re-run the replacement for each value and change the variable names and values as required, eg change "server" in the pattern to "user id" and change "newservername" in the replacement to the new user id.

Back up your data first, as obviously I don't offer a guarantee. :-)
I'm also not sure of the .NET code but this regex and replacement should work as well:

(?<=server=)\w+(;database=DBname;user id=)\w+(;password=)\w+

Open in new window


newservername${1}newuserid${2}newpassword

Open in new window

Avatar of MohitPandit

ASKER

Alright, thanks. I'll work on it and let you know.
Hi MohitPandit;

Here is a C# code snippet to replace the needed items in the connection string.
string pattern = "(?<1>server=)(?<2>[^;]+)(?<3>.*user id=)(?<4>[^;]+)(?<5>.*password=)(?<6>[^;]+)(?<7>.*)";
string server = "MyServer";
string userId = "MyUserName";
string pw = "MyPW";
string replacement = "$1" + server + "$3" + userId + "$5" + pw + "$7";
string oldConnStr = "<add name=\"DBString\" connectionString=\"server=servername;database=DBName;user id=userid;password=password;Application Name=Web:TestApp\" providerName=\"System.Data.SqlClient\"/>";

string newConnStr = Regex.Replace(oldConnStr, pattern, replacement);

Open in new window

Thank you Fernando Soto. It looks great.
Just know, I came to know other thing in connection string i.e. I can have two types of connection string which should be replace by reg ex. Could you assist on that too in single reg ex only? Please note, I can also have Data Source, Initial Catalog, uid, pwd


Old one:
<add name="DBString" connectionString="server=servername;database=DBName;user id=userid;password=password;Application Name=Web:TestApp" providerName="System.Data.SqlClient"/>


Other connection string
<add name="DBString" connectionString="Data Source=servername;Initial Catalog=DBName;uid=userid;pwd=password;Application Name=Web:TestApp" providerName="System.Data.SqlClient"/>

Best Regards
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
alright, thanks much. Let me check & let you know.

Warm Regards
Thank you very much. It works great.
Not a problem MohitPandit, glad I was able to help. 👍
Hi Fernando Soto,

Sorry to pester you again, as per aforesaid two strings i.e. "Old one:"  & "Other connection string"
Assume, we have static value in for name key like "DBString1" and "DBString2" as per below

<add name="DBString1" connectionString="server=servername;database=DBName;user id=userid;password=password;Application Name=Web:TestApp" providerName="System.Data.SqlClient"/>

Open in new window


<add name="DBString2" connectionString="Data Source=servername;Initial Catalog=DBName;uid=userid;pwd=password;Application Name=Web:TestApp" providerName="System.Data.SqlClient"/>

Open in new window


Could you create two matching pattern like I mentioned aforesaid "DBString1" and "DBString2" would not replace?
In case "DBString1" match then replace with different values.
In case "DBString2" match then replace with different values.

Best Regards
Do not understand. Can you give examples of before and after? Also please open a new question for this. Thank you.
Yes, I've given example. Okay, let me open new one.