T
tony
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,strCustomerShortName"+
",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.CoreCreditLimit.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
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,strCustomerShortName"+
",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.CoreCreditLimit.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