Ever wanted to do a wildcard Search and Replace in Visual Studio? I don’t just mean finding things using wildcards, but selectively replacing part of what you find while leaving other parts untouched. I came across an example recently where I needed to do just that – and since what I discovered saved me an inordinate amount of work, I thought I’d share it with you.
I was asked to look over the code for an ASP.NET Web Forms web site with a Sql Server back end. When I looked into the code, however, I discovered that the database was actually MySql… which I don’t support. I managed to migrate the database over to Sql Server, but then found that although the project used NHibernate, much of the data access code was written as inline SQL in the code-behind pages. And that code wouldn’t work with Sql Server.
The problem was primarily lots of code along these lines:
Unfortunately, this code errors with the Sql Server driver because GetString() only accepts integers. I needed to keep the variable name, but change the code so that it was something like this:
And there were a huge number of column names and GetXXX() methods that needed changing.
So I did a little research, and discovered that there’s an option to use regular expressions in the Find and Replace dialog.
Crucially, you can use Regular Expressions for both find and replace. So I was able to enter the following find string:
and the following replace string:
And change all the GetStrings() for all of the columns in one simple Find and Replace – thus avoiding a great deal of tedious and repetitive work. Here’s hoping I’ve saved you some of the same.
For other related information, check out these courses from Learning Tree: