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

C#,SQL Server Compact-优化插入记录速度,c#,sql,windows-mobile-6.5,sql-server-ce-3.5,C#,Sql,Windows Mobile 6.5,Sql Server Ce 3.5,我正在资源受限的设备Unitech PA690上为windows mobile 6.5构建应用程序,在将记录插入SQL server compact edition数据库时遇到速度问题。。。 有人知道将值插入compact数据库的最佳和最快的方法吗? 这是我的插入测试代码,我使用的是直接插入: private void button1_Click(object sender, EventArgs e) { Stopwatch stopwatch = new Stopwat

我正在资源受限的设备Unitech PA690上为windows mobile 6.5构建应用程序,在将记录插入SQL server compact edition数据库时遇到速度问题。。。 有人知道将值插入compact数据库的最佳和最快的方法吗? 这是我的插入测试代码,我使用的是直接插入:

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

    }
private void按钮1\u单击(对象发送者,事件参数e)
{
秒表秒表=新秒表();
秒表。开始();
string conn=“Data Source=“+(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutionGassembly().GetName().CodeBase))+”\\AppDatabase1.sdf;持久安全信息=False”;
SqlCeConnection连接=新的SqlCeConnection(conn);
connection.Open();
int z=0;
string name=“stack”;
字符串姓氏=“溢出”;
progressBar1.最大值=2000;

while(z我不确定具体的性能时间,但是您是否尝试过使用while循环构建一个查询字符串,然后将其传递给SQL server?这可能会减少您的时间,因为您只需对数据库进行一次调用,而不是执行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();

}
private void按钮1\u单击(对象发送者,事件参数e)
{
秒表秒表=新秒表();
秒表。开始();
string conn=“Data Source=“+(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutionGassembly().GetName().CodeBase))+”\\AppDatabase1.sdf;持久安全信息=False”;
SqlCeConnection连接=新的SqlCeConnection(conn);
intz=1;
string name=“stack”;
字符串姓氏=“溢出”;
最大值=2001;
String query=“插入测试(id、名称、姓氏)值(0、名称、姓氏)”

while(zd)这是否回答了您的问题?关于事务呢?如果在一个事务中完成,应该会更快。var transaction=connection.BeginTransaction();//while code transaction.Commit();感谢BlueMonkMN提供的链接,我从您的方法中获得了一些速度改进。现在我的处理器速度达到了100%,达到了29行/秒……Vladimir,您能更详细地说明事务吗?谢谢。。。