|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
string.replace and outofmemoryexceptionbelow? 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 Show quote "pb" <pb@newsgroups.nospam> wrote in message news:B640390D-BB27-4440-8794-7C073CAFC6DA@microsoft.com... > 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 > VJ <nonewsaddr***@yahoo.com> wrote:
> always recommended to use StringBuilder (System.Text) class when performing For a simple string replacement like this, it wouldn't help at all. > string operations Using StringBuilder when it isn't useful just makes code less readable. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too Right.. but looks like there is a for loop (many times) in which this done,
so I reco'd moving to Stringbuilder. Show quote "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MPG.206aee6bd1595bef98d9d7@msnews.microsoft.com... > VJ <nonewsaddr***@yahoo.com> wrote: >> 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. > > -- > Jon Skeet - <sk***@pobox.com> > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet > If replying to the group, please do not mail me too VJ <nonewsaddr***@yahoo.com> wrote:
> Right.. but looks like there is a for loop (many times) in which this done, It's being called multiple times, but on different strings - so again, > so I reco'd moving to Stringbuilder. using a StringBuilder wouldn't help. StringBuilder is useful when you're doing multiple operations on the *same* string. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too Ok Thanks John, did noto see that mulitple strings.. Yes I agree it works
with same string only VJ Show quote "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MPG.206b9ecbdfe5949e98d9dc@msnews.microsoft.com... > VJ <nonewsaddr***@yahoo.com> wrote: >> 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. > > -- > Jon Skeet - <sk***@pobox.com> > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet > If replying to the group, please do not mail me too pb <pb@newsgroups.nospam> wrote:
> can someone suggest a better implementation than my sqlescaptestr I'd suggest avoiding it to start with. Use parameterised queries > function below? instead. That way the driver does whatever is necessary - far more likely to be accurate. > unfortunately this function throws an outofmemoryexception when i am That sounds very unlikely. I think it's more likely that you have a > inserting multiple rows in a loop (str in such cases can be quite large... > could be upwards of 100K in some cases). different problem. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too 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 |
|||||||||||||||||||||||