2013-10-15 69 views
1

我正在爲資源受限制的設備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行的文件需要經常插入,所以請幫助... 謝謝,

+1

這是回答您的問題嗎? http://stackoverflow.com/questions/1606487/sqlbulkcopy-using-sql-ce – BlueMonkMN

+0

交易情況如何?如果在一次交易中完成,它應該會更快。 var transaction = connection.BeginTransaction(); //代碼 transaction.Commit(); –

+0

感謝您的鏈接BlueMonkMN,我從你的方法有一點速度提高。現在我有29行/秒的處理器敲了100%... ... 弗拉基米爾,你可以更具體的交易?謝謝... –

回答

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

} 

這不是偉大的,有這個聯合國的參數,但是這可能會顯示您的瓶頸是否是在使多個數據庫調用。

我想你也可以使用循環只是添加更多的參數到查詢中,而不是隻是建立一個大的字符串。