INSERT从内存到SQL效率低下

时间:2015-06-29 20:34:17

标签: c# sql sqlite

所以我一直在努力优化情况。基本上,我有一个WEB API服务,它查询SQL Server数据库,然后将其插入客户端计算机上的SQLite数据库。

运行一些测试后。我发现花费0.50%的时间将数据从SQL Server加载到C#中的对象数组中,并插入到其他所有时间。

我的插页难以低效吗?

这是我的方法:

public void addDataToTable()
{
    using (SQLiteConnection SQLconn = new SQLiteConnection(SQLiteConnectionString))
    {
        SQLconn.Open();
        SQLiteCommand cmd = SQLconn.CreateCommand();
        foreach (Step row in allSteps)
        {
            cmd.CommandText = "INSERT INTO Step (StepID, EWPID, StepNum, Description, MTOQty, ToDateQty, CraftCode, OriginPkg, SF01, SF10, Updated) VALUES " + 
                "(@param1, @param2, @param3, @param4, @param5, @param6, @param7, @param8, @param9, @param10, @param11);";
            cmd.Parameters.Add(new SQLiteParameter("@param1", row.StepID));
            cmd.Parameters.Add(new SQLiteParameter("@param2", row.EWPID));
            cmd.Parameters.Add(new SQLiteParameter("@param3", row.StepNum));
            cmd.Parameters.Add(new SQLiteParameter("@param4", row.Description));
            cmd.Parameters.Add(new SQLiteParameter("@param5", row.MTOQty));
            cmd.Parameters.Add(new SQLiteParameter("@param6", row.ToDateQty));
            cmd.Parameters.Add(new SQLiteParameter("@param7", row.CraftCode));
            cmd.Parameters.Add(new SQLiteParameter("@param8", row.OriginPkg));
            cmd.Parameters.Add(new SQLiteParameter("@param9", row.SF01));
            cmd.Parameters.Add(new SQLiteParameter("@param10", row.SF10));
            cmd.Parameters.Add(new SQLiteParameter("@param11", row.Updated));
            cmd.ExecuteNonQuery();
        }
    }
}

allSteps是一个对象数组,与我得到的SQL的列重合:

public class Step
{
    public int StepID { get; set; }
    public int EWPID { get; set; }
    public int StepNum { get; set; }
    public string Description { get; set; }
    public double MTOQty { get; set; }
    public double ToDateQty { get; set; }
    public string CraftCode { get; set; }
    public string OriginPkg { get; set; }
    public string SF01 { get; set; }
    public string SF10 { get; set; }
    public string Updated { get; set; }
}

有没有办法将整个Select语句转储到没有循环的SQLite表中?

1 个答案:

答案 0 :(得分:2)

这很粗糙,可能会有一些错误,但你明白了这个想法:

using (SQLiteConnection connection = new SQLiteConnection(SQLiteConnectionString))
    {
        connection.Open();

        var transaction = connection.BeginTransaction();

        SQLiteCommand cmd = connection.CreateCommand();

        cmd.Transaction = transaction;
        cmd.CommandText = "INSERT INTO Step (StepID, EWPID, StepNum, Description, MTOQty, ToDateQty, CraftCode, OriginPkg, SF01, SF10, Updated) VALUES (@param1, @param2, @param3, @param4, @param5, @param6, @param7, @param8, @param9, @param10, @param11); ";

        cmd.Parameters.Add(new SQLiteParameter("@param1"));
        cmd.Parameters.Add(new SQLiteParameter("@param2"));
        cmd.Parameters.Add(new SQLiteParameter("@param3"));
        cmd.Parameters.Add(new SQLiteParameter("@param4"));
        cmd.Parameters.Add(new SQLiteParameter("@param5"));
        cmd.Parameters.Add(new SQLiteParameter("@param6"));
        cmd.Parameters.Add(new SQLiteParameter("@param7"));
        cmd.Parameters.Add(new SQLiteParameter("@param8"));
        cmd.Parameters.Add(new SQLiteParameter("@param9"));
        cmd.Parameters.Add(new SQLiteParameter("@param10"));
        cmd.Parameters.Add(new SQLiteParameter("@param11"));

        foreach (Step row in allSteps)
        {
            cmd.Parameters["@param1"].Value = row.StepID;
            cmd.Parameters["@param2"].Value = row.EWPID;
            cmd.Parameters["@param3"].Value = row.StepNum;
            cmd.Parameters["@param4"].Value = row.Description;
            cmd.Parameters["@param5"].Value = row.MTOQty;
            cmd.Parameters["@param6"].Value = row.ToDateQty;
            cmd.Parameters["@param7"].Value = row.CraftCode;
            cmd.Parameters["@param8"].Value = row.OriginPkg;
            cmd.Parameters["@param9"].Value = row.SF01;
            cmd.Parameters["@param10"].Value = row.SF10;
            cmd.Parameters["@param11"].Value = row.Updated;

            cmd.ExecuteNonQuery();
        }

        transaction.Commit();
    }