1

我正在为 Windows mobile 6.5 构建应用程序,在资源受限的设备 Unitech PA690 上,将记录插入我的 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 慢插入,还是我错了?我有一个大约 20 000 行的文件要经常插入,所以请帮忙...谢谢,

4

1 回答 1

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

}

没有参数化并不是很好,但这可能会告诉您您的瓶颈是否在于进行多个数据库调用。

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

于 2013-10-15T15:28:59.697 回答