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(Custome r cust in customers)
{
string query ="insert ignore into dbseThxCustomer s.tblCustomer"+
" (strCustomerNam e,strCustomerAc countReference, strCustomerShor tName"+
",fltCustomerAc countBalance,"+
" fltCustomerCred itLimit, strCustomerAddr ess1, strCustomerAddr ess2,"+
"strCustomerAdd ress3, strCustomerAddr ess4"+
" ,strCustomerCon tact, strCustomerTele phoneNumber,+
"strCustomerFax Number,strCusto merEmailAddress )"+
" VALUES ("+
"'" + myObjDb.addSlas hes(cust.Name) +"'" +
",'" + myObjDb.addSlas hes(cust.Refere nce)+"'" +
",'" + myObjDb.addSlas hes(cust.ShortN ame)+"'" +
",'" + myObjDb.addSlas hes(cust.Balanc e.ToString())+" '" +
",'" +
myObjDb.addSlas hes(cust.Custom erAccount.CoreC reditLimit.ToSt ring())+"'"
+
",'" + myObjDb.addSlas hes(cust.Addres sLine1)+"'" +
",'" + myObjDb.addSlas hes(cust.Addres sLine2)+"'" +
",'" + myObjDb.addSlas hes(cust.Addres sLine3)+"'" +
",'" + myObjDb.addSlas hes(cust.Addres sLine4)+"'" +
",'" + myObjDb.addSlas hes(cust.Contac tName)+"'" +
",'" + myObjDb.addSlas hes(cust.Teleph oneNumber)+"'" +
",'" + myObjDb.addSlas hes(cust.FaxNum ber)+"'" +
",'" + myObjDb.addSlas hes(cust.TeMail Address) + "')";
}
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
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(Custome r cust in customers)
{
string query ="insert ignore into dbseThxCustomer s.tblCustomer"+
" (strCustomerNam e,strCustomerAc countReference, strCustomerShor tName"+
",fltCustomerAc countBalance,"+
" fltCustomerCred itLimit, strCustomerAddr ess1, strCustomerAddr ess2,"+
"strCustomerAdd ress3, strCustomerAddr ess4"+
" ,strCustomerCon tact, strCustomerTele phoneNumber,+
"strCustomerFax Number,strCusto merEmailAddress )"+
" VALUES ("+
"'" + myObjDb.addSlas hes(cust.Name) +"'" +
",'" + myObjDb.addSlas hes(cust.Refere nce)+"'" +
",'" + myObjDb.addSlas hes(cust.ShortN ame)+"'" +
",'" + myObjDb.addSlas hes(cust.Balanc e.ToString())+" '" +
",'" +
myObjDb.addSlas hes(cust.Custom erAccount.CoreC reditLimit.ToSt ring())+"'"
+
",'" + myObjDb.addSlas hes(cust.Addres sLine1)+"'" +
",'" + myObjDb.addSlas hes(cust.Addres sLine2)+"'" +
",'" + myObjDb.addSlas hes(cust.Addres sLine3)+"'" +
",'" + myObjDb.addSlas hes(cust.Addres sLine4)+"'" +
",'" + myObjDb.addSlas hes(cust.Contac tName)+"'" +
",'" + myObjDb.addSlas hes(cust.Teleph oneNumber)+"'" +
",'" + myObjDb.addSlas hes(cust.FaxNum ber)+"'" +
",'" + myObjDb.addSlas hes(cust.TeMail Address) + "')";
}
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
Comment