Question

I use OLEDB to insert data to a DB4 .dbf file. Inserting 13 row takes almost 1 minute which is soooo long, this problem only occurs during insertion in one table, that contain a varchar 20, 2 dates and a decimal. Is there any alternative faster ways to do this?

 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.AddWithValue("parmSlot1", Decimal.Parse(row["value"].ToString()));
                cmd.ExecuteNonQuery();
            }
Was it helpful?

Solution

You are doing cmd.Parameters.AddWithValue in the loop.

This means a parameter is added at each iteration. I do not know about DB4, but I would bet that the OleDB drivers is trying to handle extra additional unused parameters the best it can. It succeeds, but the operation take much more time.

Please insert cmd.Parameters.Clear() like this, and tell if things are running better.

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();
}

Additionally, I would also try to enclose the loop in a database transaction. Remember that too much indexes on the table are bad for insertion performances.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top