A
arak123
consider the following oversimplified and fictional code
public void CreateInvoices(Invoice[] invoices)
{
IDbCommand command=Util.CreateDbCommand();
foreach(Invoice invoice in invoices) //lets say you have 200
invoices
{
command.CommandText+="INSERT INTO Invoice(Amount)
VALUES(@Amount); ";
Util.AddParamToDbCommand("@Amount", invoice.Amount, command);
}
if(command.CommandText!=String.Empty)
{
try
{
command.ExecuteNonQuery();
}
catch(Exception ex)
{
//something here
}
}
}
/**************************************************************************
Problem: We can't add multiple parameters with the same name
Possible Solution: Change foreach loop with a for loop and assign a
number at the end of each parameter (which is not clean)
**************************************************************************/
public void CreateInvoices2(Invoice[] invoices)
{
IDbCommand command=Util.CreateDbCommand();
foreach(Invoice invoice in invoices) //lets say you have 200
invoices
{
command.CommandText+=String.Format("INSERT INTO
Invoice(Amount) VALUES({0}); ", invoice.Amount);
}
if(command.CommandText!=null)
{
try
{
command.ExecuteNonQuery();
}
catch(Exception ex)
{
//something here
}
}
}
/****************************************************************************
Problem: It seem that the ToString() function take into account the
culture info of the current thread
so in some cultures you have a different decimal number separator than
the one needed in the database
Possible solution: Set the culture info of the current thread to us-En
or do invoice.Amount.ToString(new CultureInfo("en-US")) which is not
clean either
****************************************************************************/
I was wondering if there was any cleaner and more 'natural' way to do
this than using one of the solutions described above.
public void CreateInvoices(Invoice[] invoices)
{
IDbCommand command=Util.CreateDbCommand();
foreach(Invoice invoice in invoices) //lets say you have 200
invoices
{
command.CommandText+="INSERT INTO Invoice(Amount)
VALUES(@Amount); ";
Util.AddParamToDbCommand("@Amount", invoice.Amount, command);
}
if(command.CommandText!=String.Empty)
{
try
{
command.ExecuteNonQuery();
}
catch(Exception ex)
{
//something here
}
}
}
/**************************************************************************
Problem: We can't add multiple parameters with the same name
Possible Solution: Change foreach loop with a for loop and assign a
number at the end of each parameter (which is not clean)
**************************************************************************/
public void CreateInvoices2(Invoice[] invoices)
{
IDbCommand command=Util.CreateDbCommand();
foreach(Invoice invoice in invoices) //lets say you have 200
invoices
{
command.CommandText+=String.Format("INSERT INTO
Invoice(Amount) VALUES({0}); ", invoice.Amount);
}
if(command.CommandText!=null)
{
try
{
command.ExecuteNonQuery();
}
catch(Exception ex)
{
//something here
}
}
}
/****************************************************************************
Problem: It seem that the ToString() function take into account the
culture info of the current thread
so in some cultures you have a different decimal number separator than
the one needed in the database
Possible solution: Set the culture info of the current thread to us-En
or do invoice.Amount.ToString(new CultureInfo("en-US")) which is not
clean either
****************************************************************************/
I was wondering if there was any cleaner and more 'natural' way to do
this than using one of the solutions described above.