C#,SQL Server Compact - 优化插入记录速度

时间:2013-10-15 15:03:40

标签: c# sql windows-mobile-6.5 sql-server-ce-3.5

我在资源受限设备Unitech PA690上构建Windows Mobile 6.5应用程序,并且在将记录插入我的SQL Server紧凑版数据库时出现速度问题... 有谁知道将值插入压缩数据库的最佳和最快的方法? 这是我的插入测试代码,我使用直接插入:

private void button1_Click(object sender, EventArgs e)
    {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        string conn = "Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) + "\\AppDatabase1.sdf;Persist Security Info=False";
        SqlCeConnection connection = new SqlCeConnection(conn);
        connection.Open();
        int z = 0;
        string name = "stack";
        string surname = "overflow";
        progressBar1.Maximum = 2000;
        while (z<2000)
            {
                try
                {
                    SqlCeCommand cmd = new SqlCeCommand("Insert into test (id,name,surname) values (@id, @name, @surname)", connection);
                    cmd.Parameters.AddWithValue("@id", z);
                    cmd.Parameters.AddWithValue("@name", name);
                    cmd.Parameters.AddWithValue("@surname", surname);
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.ExecuteNonQuery();
                    z++;
                    progressBar1.Value = z;

                }
                catch (SqlCeException)
                {
                    MessageBox.Show("!!!","exception");
                }
                finally
                {

                }
            }
        stopwatch.Stop();
        MessageBox.Show("Time: {0}" + stopwatch.Elapsed);
        connection.Close();

    }

经过的时间是:96秒,这使得插入速度为21行/秒。有谁知道提高插入速度的最佳方法?我知道这个移动设备速度较慢,但​​我也相信根据以下链接,插入速度至少应为400行/秒:SQL CE slow insert, 还是我错了? 我有一个大约20 000行的文件经常被插入,所以请帮助... 谢谢,

1 个答案:

答案 0 :(得分:0)

我不确定具体的性能时间,但您是否尝试使用while循环构建单个查询字符串,然后将其传递给SQL服务器?这可能会缩短您的时间,因为您只是对数据库进行一次调用而不是2000次单独的执行命令。

类似的东西:

private void button1_Click(object sender, EventArgs e)
{
    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();
    string conn = "Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) + "\\AppDatabase1.sdf;Persist Security Info=False";
    SqlCeConnection connection = new SqlCeConnection(conn);
    int z = 1;
    string name = "stack";
    string surname = "overflow";
    progressBar1.Maximum = 2001;

String query = "Insert into test (id,name,surname) values (0, name, surname)"
while (z<2000)
    {
    query = query + ", ("+z+", "+name+", "+surname+")"
    z++;
            progressBar1.Value = z;
    }
     try
        {
            connection.Open();
            SqlCeCommand cmd = new SqlCeCommand(query, connection);
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.ExecuteNonQuery();
            z++;
            progressBar1.Value = z;

         }
      catch (SqlCeException)
         {
             MessageBox.Show("!!!","exception");
         }
      finally
         {

         }

    stopwatch.Stop();
    MessageBox.Show("Time: {0}" + stopwatch.Elapsed);
    connection.Close();

}

将此参数设置为非参数化并不是很好,但这可能会向您显示您的瓶颈是否在进行多个数据库调用。

我想你也可以使用循环,只需在查询中添加越来越多的参数,而不只是构建一个大字符串。