string.replace and outofmemoryexception

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

can someone suggest a better implementation than my sqlescaptestr function
below?

unfortunately this function throws an outofmemoryexception when i am
inserting multiple rows in a loop (str in such cases can be quite large...
could be upwards of 100K in some cases).

i suppose the loop isn't allowing for the garbage collector to collect... i
could throw a GC in there, but I was hoping for a more memory efficient
function.


Public Shared Function sqlescapestr(ByVal str As String) As String

' replaces ' with ''

str = str.Replace("'", "''")
Return str
End Function
 
always recommended to use StringBuilder (System.Text) class when performing
string operations

VJ
 
VJ said:
always recommended to use StringBuilder (System.Text) class when performing
string operations

For a simple string replacement like this, it wouldn't help at all.
Using StringBuilder when it isn't useful just makes code less readable.
 
pb said:
can someone suggest a better implementation than my sqlescaptestr
function below?

I'd suggest avoiding it to start with. Use parameterised queries
instead. That way the driver does whatever is necessary - far more
likely to be accurate.
unfortunately this function throws an outofmemoryexception when i am
inserting multiple rows in a loop (str in such cases can be quite large...
could be upwards of 100K in some cases).

That sounds very unlikely. I think it's more likely that you have a
different problem.
 
Hello pb,

Try to profile your app to find out what's wrong.
As Jon noted the problem is obviously in other place

---
WBR, Michael Nemtsev [C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

p> can someone suggest a better implementation than my sqlescaptestr
p> function below?
p>
p> unfortunately this function throws an outofmemoryexception when i am
p> inserting multiple rows in a loop (str in such cases can be quite
p> large... could be upwards of 100K in some cases).
p>
p> i suppose the loop isn't allowing for the garbage collector to
p> collect... i could throw a GC in there, but I was hoping for a more
p> memory efficient function.
p>
p> Public Shared Function sqlescapestr(ByVal str As String) As
p> String
p>
p> ' replaces ' with ''
p>
p> str = str.Replace("'", "''")
p> Return str
p> End Function
 
Right.. but looks like there is a for loop (many times) in which this done,
so I reco'd moving to Stringbuilder.
 
VJ said:
Right.. but looks like there is a for loop (many times) in which this done,
so I reco'd moving to Stringbuilder.

It's being called multiple times, but on different strings - so again,
using a StringBuilder wouldn't help.

StringBuilder is useful when you're doing multiple operations on the
*same* string.
 
Ok Thanks John, did noto see that mulitple strings.. Yes I agree it works
with same string only

VJ
 
Back
Top