1

I have a lot of C# VS2008 code that looks like this:

new SqlParameter("@Description", SqlDbType.NChar, 1500)

or this:

new SqlParameter("@IsRequired", SqlDbType.Bit)

I want to change this code throughout my project — there are thousands of occurrences of this pattern.

How can I write a regular expression to remove the new SqlParameter() portion and just leave:

"@Description", SqlDbType.NChar, 1500

and

"@IsRequired", SqlDbType.Bit

?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
CodeYun
  • 749
  • 2
  • 12
  • 20

4 Answers4

4

If you are looking for a simple search/replace, then try

  1. Ctrl+Shift+H (Replace in files)
  2. Select 'Use regular expressions'
  3. Enter this regex in the 'Find what' field: new SqlParameter({.*})
  4. Enter this in the 'Replace with' field: \1
  5. Hit 'Replace All'
Eric Eijkelenboom
  • 6,943
  • 2
  • 25
  • 29
  • Hi Eric, it partially works. it replaced new SqlParameter("@Description", SqlDbType.NChar, 1500) to ("@Description", SqlDbType.NChar, 1500) but i want to remove () as well, can you help? – CodeYun Mar 19 '10 at 14:10
  • 1
    Sorry, made a typo there! Use: new SqlParameter\({.*}\) – Eric Eijkelenboom Mar 19 '10 at 14:12
  • One more problem here it doesn't work in the following secnario: objCommand.Parameters.Add(new SqlParameter("@strXML", SqlDbType.NText, strXML.Length)).Value = strXML.ToString(); has been replaced as : objCommand.Parameters.Add("@strXML", SqlDbType.NText, strXML.Length)).Value = strXML.ToString(; – CodeYun Mar 19 '10 at 14:48
1

You can write a script that use regular expression to handle the parsing and replacing.

David
  • 5,356
  • 2
  • 26
  • 39
0

Read the question that I posted here.

More Efficient Way of Adding Parameters to a SqlCommand .NET

Regardless of how you do it, it will still create a SqlParameter object. It's all about personal preference. The question i posted has a few different ways of doing it.

Community
  • 1
  • 1
Aaron
  • 7,431
  • 12
  • 35
  • 37
  • That's not my question, I just want to remove new SqlParameter() from my code since I already implement new SqlParameter() in overload method. I have lots of palce need to replace. – CodeYun Mar 19 '10 at 13:49
  • If you have another method that you create the SQLParameter in wouldn't you just replace the call to new SQLParamater with a call to your overloaded method and pass in the same parameters you pass the the SQLParameter constructor? It might also help if you posted your overloaded method and how you want to use it. – cptScarlet Mar 19 '10 at 13:55
-1

Do a replace all on new SqlParameter( with blank.
Then, do a replace all on ) with blank.

shahkalpesh
  • 33,172
  • 3
  • 63
  • 88
  • Removing all close-brackets in his code is probably *not* the desired outcome! – Dan Puzey Mar 19 '10 at 14:16
  • @Dan: Ofcourse, user will have to be responsible to see that there isn't any other thing (other than statements with new SQLParameter...). – shahkalpesh Mar 19 '10 at 15:33