Using Regular Expressions (Regex) to find all your SqlConnection strings

I was looking through some inherited code and there were some connection strings spread out throughout the project. Now because I try to make sure everything is closed in good order I needed a way to find where each was declared. I could change them all to using Statements but that just seems like a lot of work.

Instead, I found this nifty little piece of regex syntax that allows you to search through your connection strings:

new:Zs*(System.)*(Data.)*(SqlClient.)*SqlConnection(\([0-9a-zA-Z]*\))

In Visual Studio 2010 hit Ctrl+F and place the string in the Find what: textbox of the Find and Replace dialog box. Make sure to click the Use checkbox at the bottom and select Regular expressions. Also, make sure there are no trailing spaces after the last bracket or else it will not work. Click Find Next and voila. The nice thing about this is that it will find them if they are declared explicitly or not.

Find SQlConnection string with regex

Advertisements

About Ken Cenerelli
I am a Programmer Writer, .NET Developer, Microsoft MVP - Visual Studio and Development Technologies, Public Speaker, Blogger, and Microsoft Azure Nerd. I blog regularly at kencenerelli.wordpress.com and can be found on Twitter via @KenCenerelli.

One Response to Using Regular Expressions (Regex) to find all your SqlConnection strings

  1. Pingback: Using Statements to wrap your database connections « Ken Cenerelli

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: