2013-06-23 246 views
0

所有這些代碼工作現在,我有這樣的代碼:我怎樣才能讓下BackgroundWorker的

private void button1_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog openFileDialog1 = new OpenFileDialog(); 

    var dialogResult = openFileDialog1.ShowDialog(); 
    string sWybranyPlik; 
    if (dialogResult == DialogResult.OK) 
    { 
     sWybranyPlik = openFileDialog1.FileName; 

     try 
     { 


     System.Data.OleDb.OleDbConnection ExcelConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + sWybranyPlik + "';Extended Properties=Excel 8.0;"); 

     System.Data.OleDb.OleDbDataAdapter OleDBAdapter = new System.Data.OleDb.OleDbDataAdapter("select * from [Tabelle1$]", ExcelConnection); 

     DtSet.Clear(); 


     //wypełnianie datagridview 
     OleDBAdapter.Fill(DtSet.Tables[0]); 
     dataGridView1.DataSource = DtSet.Tables[0]; 


     //kasowanie starych danych w bazie danych 
     string deletesql = @"DELETE FROM TabelaProdukty"; 
     System.Data.SqlServerCe.SqlCeCommand comDelete = new System.Data.SqlServerCe.SqlCeCommand(deletesql, sqlconnection); 
     sqlconnection.Open(); 
     comDelete.ExecuteNonQuery(); 
     sqlconnection.Close(); 


     backgroundWorker1.RunWorkerAsync(); 
     progressform.ShowDialog(); 



     ExcelConnection.Close(); 
     } 

     catch (Exception ex) 
     { 
     MessageBox.Show(ex.ToString()); 
     } 
    } 
} 

而且在_DoWork:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
{ 

    //wprowadzanie nowych danych z datagridview do bazy danych 
    string strQuery = @"INSERT INTO TabelaProdukty VALUES (@VD, @ItemCode, @Item, @Qty, @Ppcur, @StandardPrice, @CeMarked, @Description, @Description2, @Edma)"; 
    sqlconnection.Open(); 
    using (System.Data.SqlServerCe.SqlCeCommand comm = new System.Data.SqlServerCe.SqlCeCommand(strQuery, sqlconnection)) 
    { 
    comm.Parameters.Add("@VD", SqlDbType.NVarChar); 
    comm.Parameters.Add("@ItemCode", SqlDbType.NVarChar); 
    comm.Parameters.Add("@Item", SqlDbType.NVarChar); 
    comm.Parameters.Add("@Qty", SqlDbType.NVarChar); 
    comm.Parameters.Add("@Ppcur", SqlDbType.NVarChar); 
    comm.Parameters.Add("@StandardPrice", SqlDbType.NVarChar); 
    comm.Parameters.Add("@CeMarked", SqlDbType.NVarChar); 
    comm.Parameters.Add("@Description", SqlDbType.NVarChar); 
    comm.Parameters.Add("@Description2", SqlDbType.NVarChar); 
    comm.Parameters.Add("@Edma", SqlDbType.NVarChar); 

    for (int i = 0; i < dataGridView1.Rows.Count; i++) 
    { 

     comm.Parameters["@VD"].Value = dataGridView1.Rows[i].Cells["VD"].Value.ToString().Trim(); 
     comm.Parameters["@ItemCode"].Value = dataGridView1.Rows[i].Cells["ItemCode"].Value.ToString().Trim(); 
     comm.Parameters["@Item"].Value = dataGridView1.Rows[i].Cells["ITEM"].Value.ToString().Trim(); 
     comm.Parameters["@Qty"].Value = dataGridView1.Rows[i].Cells["QUANTITY"].Value.ToString().Trim(); 
     comm.Parameters["@Ppcur"].Value = dataGridView1.Rows[i].Cells["PPCUR"].Value.ToString().Trim(); 
     comm.Parameters["@StandardPrice"].Value = dataGridView1.Rows[i].Cells["STANDARD_SELL_PRICE"].Value.ToString().Trim(); 
     comm.Parameters["@CeMarked"].Value = dataGridView1.Rows[i].Cells["CE-MARKED"].Value.ToString().Trim(); 
     comm.Parameters["@Description"].Value = dataGridView1.Rows[i].Cells["ITEM_DESCRIPTION"].Value.ToString().Trim(); 
     comm.Parameters["@Description2"].Value = dataGridView1.Rows[i].Cells["ITEM_DESCRIPTION2"].Value.ToString().Trim(); 
     comm.Parameters["@Edma"].Value = dataGridView1.Rows[i].Cells["EDMA"].Value.ToString().Trim(); 


     comm.ExecuteNonQuery(); 

     backgroundWorker1.ReportProgress(i); 

     if (backgroundWorker1.CancellationPending) 
     { 
     e.Cancel = true; 
     break; 
     } 

    } 
    sqlconnection.Close(); 


    } 
} 

正如你可以看到這個代碼讓我挑一個excel文件使用OpenFileDialog清除DataSet,使用數據填充數據集和datagridview,清除SQL表nad,然後打開一個表單,其中進度條顯示將數據從datagridview複製到sql數據庫的一個長進程(12行中大約150000行。 At這點它工作正常。我遇到的問題是在打開progressform之前的所有內容都需要花費時間,所以我想把它全部放在backgroundworker下,並在progressform中顯示狀態報告,例如「舊數據已刪除」或表「正在載入新數據」,然後運行dgv-> sql部分。不幸的是,通過在_DoWork下放置這些代碼,我得到了不同的錯誤,具體取決於我在那裏複製了多少代碼(button1_Click的全部或僅「嘗試」部分)。

你有什麼想法應該看起來如何?

最好的問候,

編輯:

其他方法:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) 
{ 

    progressform.progressBar1.Value = e.ProgressPercentage; 
} 

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
{ 
    if (e.Cancelled) 
    { 
    MessageBox.Show("Operacja przerwana! Cennik nie został wprowadzony w całości", "Uwaga!", MessageBoxButtons.OK, MessageBoxIcon.Warning); 


    } 
    else 
    { 

    progressform.label1.Text = "Zakończono pomyślnie! :)"; 
    progressform.button1.Enabled = true; 
    } 
} 

回答

1

您需要使用reportProgress事件的BackgroundWorker的。

將文件名從openfiledialog保存爲實例級別的字符串,即在方法外部。現在將代碼填入dataset完全在backgroundworker.dowork事件處理程序中。填入後dataset致電backgroundworker.ReportProgress。這將觸發backgroundworker.ProgressChanged事件。在此方法中填充datagridview,因爲此方法將在實例化backgroundworker的線程上調用。現在用dowork方法發送其餘的ADO連接代碼。這應該夠了。

**EDIT** 

class YourClass 
    { 
     string filePath; 
     DataSet dataset; 
     private static void Do_Work(object sender, DoWorkEventArgs e) 
     { 
      //populate dataset 
      //call worker.ReportProgress to trigger worker.ProgressChanged 
      //rest of time consuming ADO.Net Codes and other codes you wrote in your DoWork method 
     } 

     private static void Progress_Changed(object sender, ProgressChangedEventArgs e) 
     { 
      //this method is invoked in the thread where background worker is instantiated. 
      //do all UI stuff here like populating datagridview from dataset 
     } 
    } 
+0

謝謝,我試圖得到這(這是不容易的,因爲我只是用c#stariting)。無論如何,你應該把填充數據集的代碼放在reportprogress下。這個事件在加載到sql期間已經被調用以使進度條工作。我應該怎麼做才能知道reportchanged事件是否應該執行數據集或sql部分?一些如果方法?或者,也許我不明白你...(可能是這個;)) –

+0

你包括progresschanged方法嗎? – Androidz

+0

編輯答案。看看它是否清楚。 –