我正在爲資源受限制的設備Unitech PA690構建windows mobile 6.5的應用程序,我在將記錄插入到我的SQL Server精簡版數據庫時遇到速度問題... 任何人都知道將值插入到緊湊型數據庫中的最佳和最快的方法? 這裏是我的嵌入測試代碼,我'使用直接插入:C#,SQL Server 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();
}
經歷的時間是96秒,這讓21行/秒的插入速度。有沒有人知道在這裏提高插入速度的最佳方法?我知道這個移動設備速度較慢,但我也相信根據此鏈接,插入速度應至少爲400行/秒:SQL CE slow insert, 或者我錯了嗎? 我有一個約20000行的文件需要經常插入,所以請幫助... 謝謝,
這是回答您的問題嗎? http://stackoverflow.com/questions/1606487/sqlbulkcopy-using-sql-ce – BlueMonkMN
交易情況如何?如果在一次交易中完成,它應該會更快。 var transaction = connection.BeginTransaction(); //代碼 transaction.Commit(); –
感謝您的鏈接BlueMonkMN,我從你的方法有一點速度提高。現在我有29行/秒的處理器敲了100%... ... 弗拉基米爾,你可以更具體的交易?謝謝... –