Skip to main content
added the CODES!
Source Link
Wyatt Barnett
  • 15.7k
  • 3
  • 37
  • 54

Not that I am aware of. And I have felt this pain before. The good news is that if you wrap the insert in a transaction and don't close the connection between each call (eg--pass the command as a parameter) then it is orders of magnitude quicker than open()->INSERT->close() is.

Use a code pattern something like:

using (DbConnection conn = new OdbcConnection())
using (DbCommand cmd = conn.CreateCommand())
{
    cmd.CommandText = "INSERT INTO foo(bar) VALUES (@bar)";
    DbParameter p = cmd.CreateParameter();
    p.ParameterName = "@bar";
    cmd.CommandType = CommandType.Text;
    conn.Open();
    using (DbTransaction tran = conn.BeginTransaction())
    {
        cmd.Transaction = tran;
        try
        {
            for (int i = 0; i < 1000; i++)
            {
                p.Value = i;
                cmd.ExecuteNonQuery();
            }
            tran.Commit();
            conn.Close();
        }
        catch (Exception)
        {
            tran.Rollback();
            throw;
        }
    }
}

Not that I am aware of. And I have felt this pain before. The good news is that if you wrap the insert in a transaction and don't close the connection between each call (eg--pass the command as a parameter) then it is orders of magnitude quicker than open()->INSERT->close() is.

Not that I am aware of. And I have felt this pain before. The good news is that if you wrap the insert in a transaction and don't close the connection between each call (eg--pass the command as a parameter) then it is orders of magnitude quicker than open()->INSERT->close() is.

Use a code pattern something like:

using (DbConnection conn = new OdbcConnection())
using (DbCommand cmd = conn.CreateCommand())
{
    cmd.CommandText = "INSERT INTO foo(bar) VALUES (@bar)";
    DbParameter p = cmd.CreateParameter();
    p.ParameterName = "@bar";
    cmd.CommandType = CommandType.Text;
    conn.Open();
    using (DbTransaction tran = conn.BeginTransaction())
    {
        cmd.Transaction = tran;
        try
        {
            for (int i = 0; i < 1000; i++)
            {
                p.Value = i;
                cmd.ExecuteNonQuery();
            }
            tran.Commit();
            conn.Close();
        }
        catch (Exception)
        {
            tran.Rollback();
            throw;
        }
    }
}
Source Link
Wyatt Barnett
  • 15.7k
  • 3
  • 37
  • 54

Not that I am aware of. And I have felt this pain before. The good news is that if you wrap the insert in a transaction and don't close the connection between each call (eg--pass the command as a parameter) then it is orders of magnitude quicker than open()->INSERT->close() is.