我正在为 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 行的文件要经常插入,所以请帮忙...谢谢,