我一直试图在我的注射交易中添加进度条但没有成功。我需要显示在我的表上插入数据时的百分比。我有多个表和多个插入。我还在多个表上有一个循环插入,如使用listview数据在foreach语句中所示。当我尝试从列表视图中插入100条记录时,我的计算机开始滞后,并且无法确定何时完成。
任何人都可以告诉我如何操作以及如何将backgroundworker链接到进度条。
我还需要检查saveEdit()的进度 - 多个表上有多个更新的函数和多个表上有多个删除的saveDelete()函数
如果我将saveAdd()代码放在backgroundWorkerDoWork函数中,我认为这将是一种过度杀伤,因为我还需要放置具有多行代码的saveEdit()和saveDelete()函数。
顺便说一句,我使用的是fbconnection,它与sqlconnection功能几乎相同。
private void saveAdd()
{
backgroundWorker.RunWorkerAsync(); // loading bar
DBconnection dbConString = new DBconnection(); //initialize connectDB with connection string
FbConnection dbConnect = new FbConnection(dbConString.getConnectionString()); // connect to database using connectdb
dbConnect.Open(); // open the connection
FbTransaction transaction = dbConnect.BeginTransaction(); // instantiate transaction
backgroundWorker.ReportProgress(10);// loading bar not working
try
{
FbCommand myCommand1 = new FbCommand(getQuerryStrings("insEquipMstDtl"), dbConnect, transaction); //stantiate sql command
myCommand1.Parameters.Add("equipmentid", SqlDbType.BigInt).Direction = ParameterDirection.Output;
myCommand1.ExecuteScalar();// insert into tbl_equipmst or equipmaster
string strDtlequipmentid = myCommand1.Parameters["equipmentid"].Value.ToString();
//MessageBox.Show(strDtlequipmentid);
backgroundWorker.ReportProgress(15);// loading bar not working
FbCommand myCommand2 = new FbCommand(getQuerryStrings("insEquipTransaction", strDtlequipmentid), dbConnect, transaction); //stantiate sql command
myCommand2.Parameters.Add("EQUIPTRANSID", SqlDbType.BigInt).Direction = ParameterDirection.Output;
myCommand2.ExecuteScalar(); // inserts transaction details
string strDtlequiptransid = myCommand2.Parameters["EQUIPTRANSID"].Value.ToString();
backgroundWorker.ReportProgress(20);// loading bar not working
FbCommand myCommand3 = new FbCommand(getQuerryStrings("insEquipWarServDtl", strDtlequipmentid, strDtlequiptransid), dbConnect, transaction); //stantiate sql command
myCommand3.ExecuteNonQuery(); // inserts service warranty in details
backgroundWorker.ReportProgress(25);// loading bar not working
FbCommand myCommand4 = new FbCommand(getQuerryStrings("insEquipWarProdDtl", strDtlequipmentid, strDtlequiptransid), dbConnect, transaction); //stantiate sql command
myCommand4.ExecuteNonQuery(); // inserts product warranty in details
backgroundWorker.ReportProgress(30);// loading bar not working
FbCommand myCommand5 = new FbCommand(getQuerryStrings("insEquipStatDtl", strDtlequipmentid, strDtlequiptransid), dbConnect, transaction); //stantiate sql command
myCommand5.ExecuteNonQuery(); // inserts equipmtn status
backgroundWorker.ReportProgress(35);// loading bar not working
FbCommand myCommand6 = new FbCommand(getQuerryStrings("insEquipLocationDtl", strDtlequipmentid, strDtlequiptransid), dbConnect, transaction); //stantiate sql command
myCommand6.ExecuteNonQuery(); // inserts location
backgroundWorker.ReportProgress(40);// loading bar not working
if (chkParts.Checked == true)
{
if (lvwPartsList.Items.Count !=0 )
{
foreach (ListViewItem item in lvwPartsList.Items)
{
FbCommand myCommand7 = new FbCommand(getPartsQuerryStrings("insEquipMstPart", strDtlequipmentid, item), dbConnect, transaction); //stantiate sql command
myCommand7.Parameters.Add("equipmentid", SqlDbType.BigInt).Direction = ParameterDirection.Output;
myCommand7.ExecuteScalar();// parts insert into tbl_equipmst or equipmaster
string strPartsequipmentid = myCommand7.Parameters["equipmentid"].Value.ToString();
FbCommand myCommand8 = new FbCommand(getPartsQuerryStrings("insEquipTransactionPart", strPartsequipmentid), dbConnect, transaction); //stantiate sql command
myCommand8.Parameters.Add("EQUIPTRANSID", SqlDbType.BigInt).Direction = ParameterDirection.Output;
myCommand8.ExecuteScalar(); // inserts transaction details - parts
string strPartequiptransid = myCommand8.Parameters["EQUIPTRANSID"].Value.ToString();
FbCommand myCommand9 = new FbCommand(getPartsQuerryStrings("insEquipWarServPart", strPartsequipmentid, strPartequiptransid,item), dbConnect, transaction); //stantiate sql command
myCommand9.ExecuteNonQuery(); // inserts service warranty in details - parts
FbCommand myCommand10 = new FbCommand(getPartsQuerryStrings("insEquipWarProdPart", strPartsequipmentid, strPartequiptransid, item), dbConnect, transaction); //stantiate sql command
myCommand10.ExecuteNonQuery(); // inserts product warranty in details - parts
FbCommand myCommand11 = new FbCommand(getPartsQuerryStrings("insEquipStatPart", strPartsequipmentid, strPartequiptransid, item), dbConnect, transaction); //stantiate sql command
myCommand11.ExecuteNonQuery(); // inserts equipmtn status - parts
FbCommand myCommand12 = new FbCommand(getPartsQuerryStrings("insEquipLocationPart", strPartsequipmentid, strPartequiptransid, item), dbConnect, transaction); //stantiate sql command
myCommand12.ExecuteNonQuery(); // inserts equipmtn location - parts
}
}
}
transaction.Commit();
MessageBox.Show("Successful");
frmNormalMode();
}
catch (Exception x)
{
transaction.Rollback();
MessageBox.Show("No Update has been made. Error (" +
x.Message + ")");
}
finally
{
dbConnect.Close();
}
}
答案 0 :(得分:0)
您以错误的方式使用BackgroundWorker
。首先,对代码进行以下更改。在创建BackgroundWorker
时,将backgroundWorker.WorkerReportsProgress
设置为True(或者如果使用控件,请在“属性”窗口中将其设置为true)。
接下来,实现DoWork
事件。它应该包含您的保存代码。同样,您可以在“属性”窗口中使用事件DoWork
。现在看起来应该是这样的(注意我使用了Toolbox窗口中的控件,所以我将backgroundworker作为我表单上的控件):
private bool arePartsChecked;
private List<string> partsList = new List<string>();
private void saveAdd()
{
arePartsChecked = chkParts.Checked;
partsList.Clear();
foreach (ListViewItem item in lvwPartsList.Items)
partsList.Add(item.Text);
backgroundWorker.RunWorkerAsync(); // loading bar
// remove saving code, move it to DoWork event
}
private void backgroundWorkerDoWork(object sender, DoWorkEventArgs e)
{
DBconnection dbConString = new DBconnection();
FbConnection dbConnect = new FbConnection(dbConString.getConnectionString());
// part of your code ......
if ((arePartsChecked) && (partsList.Count > 0))
{
foreach (itemList partsList)
{
FbCommand myCommand7 = new FbCommand(getPartsQuerryStrings("insEquipMstPart",
strDtlequipmentid, item), dbConnect, transaction); //stantiate sql command AND note: item is now a string...!!
// etc.
}
// ...
}
finally
{
dbConnect.Close();
}
}
现在实现private void ProgressChanged
事件。它应该包含如下代码:
private void backgroundWorkerProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
其中progressBar是表单上的进度条。