加速查询.net的执行

时间:2015-01-09 06:54:13

标签: c# sql .net

我想通过创建大约10个表来创建博客并插入大量数据,问题是这个工作可以创建一些博客但是如果我们想要创建1500它就像蜗牛一样。

  private void createBlog_Click(object sender, EventArgs e)
    {
       for (int i = 0; i < Blogs_dataGridView.RowCount; i++)
       {
        bool IscommentCreated = MySQL.CreateComments(blogList);
        if (IscommentCreated)
        {
            MySQL.InsertIntoComments(blogList);
        }
        //===========Part2=========================================
        MySQL.Createlinks1(blogList);
        MySQL.Createlinks2(blogList);
        MySQL.Createlinks3(blogList);
        MySQL.Createoptions(blogList);
        MySQL.InsertIntooptions(blogList, mail, getePermalink());
        //other tables are inserted here
       }
    }

我试图在创建的许多步骤之间进行联合,这会加快执行速度但不会太多

如何加快执行速度?

1 个答案:

答案 0 :(得分:0)

使用ADO.NET时常见的缺陷不是重用IDbCommand个对象:

public class X
{
    public void PerformQuery(...)
    {
        using (MySqlCommand cmd = conn.CreateCommand())
        {
            // create parameters and parameter values
            cmd.ExecuteNonQuery();
        }
    }
}

相反,它看起来有点像这样:

public class X : IDisposable
{
    public MySqlCommand cmdInsertThings;
    public MySqlCommand cmdInsertOtherThings;
    public bool disposed; 

    public X(MySqlConnection c)
    {
        cmdInsertThings = c.CreateCommand();
        // create parameters
        cmdInsertOtherThings = ...
    }

    public void PerformQuery(...)
    {
        // assign parameter values
        cmdInsertThings.ExecuteNonQuery();
    }

    public void Dispose()
    {
        if (disposed) return;
        disposed = true;
        cmdInsertThings.Dispose();
    }
}

(当然,你必须在整个操作过程中保持MySqlConnection开放,我认为你已经这样做了)