473,379 Members | 1,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

newbee memory question

Hello,

I am trying to loop through a collection of objects, create a sql
string
from data held within those objects and insert into a database.

Each time through the loop the program seems to consume more memory to
the
point it almost slows to a halt. The first 200 inserts take around 1
second,
but it takes 15 minutes to to 1200. I can watch memory usage increase
as it loops through the collection.
Some code:

Customers is a collection of customer objects provided by a 3rd party
application. myObjDb is a simple database abstraction class,
addslashes is a simple function that escapes special charaters
using Regex

foreach(Customer cust in customers)
{

string query ="insert ignore into dbseThxCustomers.tblCustomer"+
" (strCustomerName,strCustomerAccountReference,strCu stomerShortName"+
",fltCustomerAccountBalance,"+
" fltCustomerCreditLimit, strCustomerAddress1, strCustomerAddress2,"+
"strCustomerAddress3, strCustomerAddress4"+
" ,strCustomerContact, strCustomerTelephoneNumber,+
"strCustomerFaxNumber,strCustomerEmailAddress )"+
" VALUES ("+
"'" + myObjDb.addSlashes(cust.Name) +"'" +
",'" + myObjDb.addSlashes(cust.Reference)+"'" +
",'" + myObjDb.addSlashes(cust.ShortName)+"'" +
",'" + myObjDb.addSlashes(cust.Balance.ToString())+"'" +
",'" +
myObjDb.addSlashes(cust.CustomerAccount.CoreCredit Limit.ToString())+"'"
+
",'" + myObjDb.addSlashes(cust.AddressLine1)+"'" +
",'" + myObjDb.addSlashes(cust.AddressLine2)+"'" +
",'" + myObjDb.addSlashes(cust.AddressLine3)+"'" +
",'" + myObjDb.addSlashes(cust.AddressLine4)+"'" +
",'" + myObjDb.addSlashes(cust.ContactName)+"'" +
",'" + myObjDb.addSlashes(cust.TelephoneNumber)+"'" +
",'" + myObjDb.addSlashes(cust.FaxNumber)+"'" +
",'" + myObjDb.addSlashes(cust.TeMailAddress) + "')";

}

I have tries declaring string query outside the loop, setting it to
null at
the end of the loop and using stringBuilder but same result every
time.

Hope I have provided enough info.

TIA tony
Nov 16 '05 #1
6 1345
> I am trying to loop through a collection of objects, create a sql
string

The string type allocates a new object and copies the old string each time
which gets expensive as you have discovered.

Try using System.Text.StringBuilder instead of string. StringBuilder is
optimized for this type of work (building up a string from parts).

StringBuilder s = new StringBuilder();
s.Append("insert ignore into dbseThxCustomers.tblCustomer");
s.Append(...);

Mark
Nov 16 '05 #2
Mark,
Using stringbuilder in this case will have no affect on performance or
memory consumption. See my blog post:
http://dotnetjunkies.com/WebLog/jpal.../31/49465.aspx

Tony, can you post the code you are using to actually send the sql to
the database? The problem is likely to be there. Also, please post
the code of the addShashes method so I can verify that logic isn't the
problem. The code you posted only does string concatenation. I fail
to see how this code causes this behavior.

Best regards,
Jeffrey Palermo
Blog: http://www.jeffreypalermo.com

Nov 16 '05 #3
> Tony, can you post the code you are using to actually send the sql
to
the database? The problem is likely to be there. Also, please post
the code of the addShashes method so I can verify that


Thanks for the reply Jeffrey, heres the class I use for handling
connection to the database, it included the addslashes method

public class dbMysql
{
private OdbcConnection MyConnection;

public void connect(string MyConString)
{
MyConnection = new OdbcConnection(MyConString);
MyConnection.Open();
}

public string addSlashes(string query)
{
query = Regex.Replace(query,@"\\",@"\\");
query = Regex.Replace(query,"'","\\'");
query = Regex.Replace(query,"\"","\\\"");
return query;
}

public OdbcCommand getCommand()
{
OdbcCommand MyCommand = new OdbcCommand();
MyCommand.Connection = MyConnection;
return MyCommand;
}

public void close()
{
MyConnection.Close();
}
}
and the code that does the insert is simply
try
{
command.CommandText = query;
command.ExecuteNonQuery();
}
catch (OdbcException MyOdbcException)
{
Console.WriteLine("in exception");
for (int i=0; i < MyOdbcException.Errors.Count; i++)
{
MessageBox.Show("Message: " +
MyOdbcException.Errors[i].Message);
}
}

regards
tony
Nov 16 '05 #4
I have just found that i was calling the wrong method on the customer
object

the line:
myObjDb.addSlashes(cust.CustomerAccount.CoreCredit Limit.ToString())+"'"

should actually be
", '" + myObjDb.addSlashes(cust.CreditLimit.ToString())+"' " +
My appologies for the bogus question...

Regards
Tony
Nov 16 '05 #5
> Using stringbuilder in this case will have no affect on performance or
memory consumption. See my blog post:


It's late and I'm jet lagged, so I can't see how your test code in your blog
post applies here. This example did not concatenate literals like the tests
you did.

Thanks,

Mark
Nov 16 '05 #6
Tony,
Not a problem. Do make sure that the "close()" method is getting
called after the transaction.

Sometimes talking through a problem leads to the answer.

Best regards,
Jeffrey Palermo
Blog: http://www.jeffreypalermo.com

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Newbee Adam | last post by:
some said that .NET app can run on any program where rutime exists. What is "runtime" in this sense? will I have to install runtime or .net framework or .NET support on an xp machine for a...
10
by: Martin Andert | last post by:
Why ist the following code not working? I want to initialize a string in function, but I am getting those STATUS_ACCESS_VIOLATION exceptions. #include <stdio.h> #include <stdlib.h> void...
0
by: valmir cinquini | last post by:
i'm developing a faq session of our website and I'm facing some problems handling xml files and DataSet relations First off all, here's the structure of my xml file: <?xml version="1.0"?>...
3
by: nahur | last post by:
why do you need a heap and a stack why not all memory called a heap or call it a stack what is the purpose of having a heap and a stack
9
by: EMW | last post by:
I have created a page in aspx and after a click on a button, a new page should open. How is this possible? I tried it doing it like in vb.NET with opening a new form, but it doesn't work. rg,...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
2
by: Mel | last post by:
I have a selection box with 3 values. what i need is to pass 3 urls to a function that has a switch statement and based on the selection index goes on one of the tree urls. Question is how do i...
1
by: Martin | last post by:
Hi! I have a class like this Class MyClass { Public MyClass() { // Doing stuff }
0
by: Martin Arvidsson, Visual Systems AB | last post by:
Hi! I have a couple of newbee questions. I have a .aspx page with a couple of TextBoxes and a submit button. Now... When i press the submitbutton i want the data in the TextBoxes to be...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.