Click here to Skip to main content
15,561,125 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use OLEDB to insert data to a DB4 .dbf file. Inserting 13 row takes almost 1 minute wich is soooo loong, theis problem only accur during insertion in one table , that contain a varchar 20 , 2 dates and a decimal. Is there any alternative faster ways to do this?
here is some code samples

C#
foreach (DataRow row in fstathotel.Rows)
               {
                   cmd.CommandText = @"insert into fstathote values (" + Convert.ToInt32(row["mpehotel"]) + ",'" + Convert.ToDateTime(row["date"]) + "','" + row["type"].ToString() + "',?,'" + Convert.ToDateTime(row["edate"]) + "')";
                   cmd.Parameters.AddWithValue("parmSlot1", Decimal.Parse(row["value"].ToString()));
                   cmd.ExecuteNonQuery();
               }
Posted

Try changing the way you are setting up your command to use prepared sql

see http://msdn.microsoft.com/en-GB/library/system.data.oledb.oledbcommand.prepare.aspx[^]

Apart from making things more performant using (fully) parameterised queries will also help protect against sql injection

[Edit]

C#
cmd.CommandText = @"insert into fstathote values (?,?,?,?,?)
foreach (DataRow row in fstathotel.Rows)
     cmd.Parameters.AddWithValue("mpehotel", Convert.ToInt32(row["mpehotel"]));
     cmd.Paremeters.AddWithValue("date", Convert.ToDateTime(row["date"]));
     cmd.Parameters.AddWithValue("type", row["type"].ToString());
     cmd.Parameters.AddWithValue("value", Decimal.Parse(row["value"].ToString())); // parmSlot1 in your code
     cmd.Parameters.AddWithValue("edate", Convert.ToDateTime(row["edate"]));
     cmd.ExecuteNonQuery();
 
Share this answer
 
v2
Comments
SalouaK 18-Mar-13 12:13pm    
This method are depricated ! that why i used command.Parameters.AddWithValue();
CHill60 18-Mar-13 12:28pm    
oh ok - sorry, it's not deprecated in the versions I'm currently using and I didn't see anything to that effect on the microsoft site.
However you're still not using a fully parameterised commandtext - I'll update the solution with what I mean
SalouaK 18-Mar-13 12:33pm    
You're right, and Ia m going to change my code to use parameters instead of injecting values directly.
CHill60 18-Mar-13 12:51pm    
And I've just found the obsolete comments - but only against OleDbParameterCollection.Add(String, Object) - you might still get further gains by using Prepare with the above changes
SalouaK 18-Mar-13 13:08pm    
oh! you're right, I was using the one without the third parameters
The problem was that I have cmd.Parameters.AddWithValue in the looop wish means a parameter is added at each iteration.

C#
foreach (DataRow row in fstathotel.Rows)
{
    cmd.CommandText = @"insert into fstathote values (" + Convert.ToInt32(row["mpe"]) + ",'" + Convert.ToDateTime(row["date"]) + "','" + row["type"].ToString() + "',?,'" + Convert.ToDateTime(row["edate"]) + "')";
    cmd.Parameters.Clear(); // Clear the parameter list 
    cmd.Parameters.AddWithValue("parmSlot1", Decimal.Parse(row["value"].ToString()));
    cmd.ExecuteNonQuery();
}
 
Share this answer
 
Comments
phil.o 18-Mar-13 12:26pm    
You cannot reuse a command object with parameters in a loop. That means you have to initialize it on each loop :

<pre>
foreach (DataRow row in fstathotel.Rows)
{
SqlCommand cmd = new SqlCommand();
// Initialize here the command text and the parameters
}
</pre>

Of course you will have to suppress the cmd variable declaration outside the loop.
CHill60 18-Mar-13 12:45pm    
Has that behaviour changed? I only ask because I'm supporting some applications that are doing bulk inserts (many thousands of records per table) and we derive our sql first, set up a .CommandText = sql and then just use cmd.Parameters["@xxx"].Value = yyyyy;. My concern would be a complete re-engineer would be required to upgrade from VS2005 if we have to do a new SqlCommand() on each iteration.
SalouaK 18-Mar-13 12:50pm    
But you should change your way of initialising the params, it got depricated since VS2010
CHill60 18-Mar-13 13:00pm    
Nope - I've just tried cmd.Parameters["@xxx"].Value = yyyyy; in VS2010 Prof. and it seems to be working fine - not a "deprecate" or "obsolete" to be found!?!? Note we're not using .Add(String, Object) ... we use .Add(String, SqlDbType, Int32) - which is still around in .NET4.5
SalouaK 18-Mar-13 12:48pm    
I couldn't implement phil.o way , I just cleared my parameters in every loop and then i add the params

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900