Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2007

Tip: Looking for answers? Try searching our database.

string.replace and outofmemoryexception

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
pb - 21 Mar 2007 01:38 GMT
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 - 21 Mar 2007 02:53 GMT
always recommended to use StringBuilder (System.Text) class when performing
string operations

VJ

> can someone suggest a better implementation than my sqlescaptestr function
> below?
[quoted text clipped - 15 lines]
>        Return str
>    End Function
Jon Skeet [C# MVP] - 21 Mar 2007 08:26 GMT
> 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.

Signature

Jon Skeet - <skeet@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 - 21 Mar 2007 15:03 GMT
Right.. but looks like there is a for loop (many times) in which this done,
so I reco'd moving to Stringbuilder.

>> 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 [C# MVP] - 21 Mar 2007 20:58 GMT
> 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.

Signature

Jon Skeet - <skeet@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 - 21 Mar 2007 22:24 GMT
Ok Thanks John, did noto see that mulitple strings.. Yes I agree it works
with same string only

VJ

>> Right.. but looks like there is a for loop (many times) in which this
>> done,
[quoted text clipped - 5 lines]
> StringBuilder is useful when you're doing multiple operations on the
> *same* string.
Jon Skeet [C# MVP] - 21 Mar 2007 08:26 GMT
> 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.

Signature

Jon Skeet - <skeet@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

Michael Nemtsev - 21 Mar 2007 10:57 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.