如何在C#中使用后台工作程序在sqlite数据库中插入数据 [英] How do I make background worker in C# to insert data in sqlite database

查看:91
本文介绍了如何在C#中使用后台工作程序在sqlite数据库中插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



首先请不要判断我是编程新手



i希望在数据库中添加和更新一些数据,但表格总是冻结



所以我搜索解决我的问题,我发现后台工作者



我试图实现我在INTERNET中找到的步骤,但它什么也没做我认为没有以正确的方式工作



我需要的是什么



1.禁用主窗体中的某些组件



2.显示面板加载(进度条 Marquee 并标签和图片)然后在显示面板和程序栏工作时添加数据

我的加载面板图片



3.完成后在主窗体中启用某些组件



如果有的话ne可以帮我实现这个想法我会感恩的



我尝试过的事情:



Hi Everybody

First of all "Please don't judge me I am New In Programming"

i want to add and update some data in database but the form always got Freeze

so i search to solve my problem and i found Background Worker

and i tried to implement the steps that i found in INTERNET but it do nothing and didn't work in wright way i think

What I need

1. disable some Components in the main form

2. showing "Panel Loading" (has progress bar "Marquee" and label and Picture) then adding data when showing the panel and the progrss bar work
My Loading Panel Picture

3. after finishing enable some Components in the main form

if anyone can help me to implement this idea i will be thankful

What I have tried:

private void btnAddHoleInvoiceData_Click(object sender, EventArgs e)
        {
            

            switch (MessageBox.Show(this, "هل تريد بالتأكيد إدخال بيانات الفاتورة (" + it.TempInvoice_Id + ") \"لا يمكن التراجع عن العملية\" ؟", "تأكيد الإدخال", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                case DialogResult.Yes:

                    #region To Do Befor Adding

                    cmbBxCompanyName.Enabled = false;
                    txtBxInvoiceDescription.Enabled = false;
                    txtBxInvoiceNote.Enabled = false;
                    btnAddNewInvoice.Enabled = false;
                    btnDeleteTempInvoice.Enabled = false;
                    txtBxInvoiceTaxRate.Enabled = false;
                    txtBxInvoiceOther.Enabled = false;
                    txtBxInvoiceDeposit.Enabled = false;

                    cmbBxItems.Enabled = false;
                    txtBxQuantity.Enabled = false;
                    txtBxUnitPrice.Enabled = false;
                    txtBxDiscount.Enabled = false;
                    listView.Enabled = false;
                    btnAddItem.Enabled = false;
                    btnRemoveItem.Enabled = false;
                    lblItemsTotalPrice.Enabled = false;
                    lblInvoiceTotalPrice.Enabled = false;

                    button5.Enabled = false;
                    btnUpdateCustomerData.Enabled = false;
                    btnCancel.Enabled = false;

                    ControlBox = false;

                    pnlLoding.Visible = true;
                    pnlLoding.Refresh();

                    int milliseconds = 4000;
                    Thread.Sleep(milliseconds);

                    #endregion


                    bgWorker.RunWorkerAsync();



                    #region To Do After Adding

                    it.LastInvoice_Id = it.TempInvoice_Id;

                    it.TempInvoice_Id = 0;

                    int milliseconds2 = 4000;
                    Thread.Sleep(milliseconds2);

                    grpBxInvoiceData.Text = " Invoice Data ";

                    foreach (ListViewItem eachItem in listView.Items)
                    {
                        listView.Items.Remove(eachItem);
                    }

                    lblItemsTotalPrice.Text = getItemsSum().ToString("0.00");
                    lblInvoiceTotalPrice.Text = getInvoiceTotalSum().ToString("0.00");

                    txtBxInvoiceDescription.Text = string.Empty;
                    txtBxInvoiceNote.Text = string.Empty;
                    txtBxInvoiceTaxRate.Text = "0.00";
                    txtBxInvoiceOther.Text = "0.00";
                    txtBxInvoiceDeposit.Text = "0.00";

                    cmbBxItems.SelectedIndex = 0;
                    txtBxStoreQuantity.Refresh();
                    txtBxQuantity.Text = string.Empty;
                    txtBxUnitPrice.Text = string.Empty;
                    txtBxDiscount.Text = string.Empty;

                    pnlLoding.Visible = false;

                    cmbBxCompanyName.Enabled = true;
                    txtBxInvoiceDescription.Enabled = true;
                    txtBxInvoiceNote.Enabled = true;
                    btnAddNewInvoice.Enabled = true;
                    btnDeleteTempInvoice.Enabled = false;
                    txtBxInvoiceTaxRate.Enabled = true;
                    txtBxInvoiceOther.Enabled = true;
                    txtBxInvoiceDeposit.Enabled = true;

                    cmbBxItems.Enabled = false;
                    txtBxQuantity.Enabled = false;
                    txtBxUnitPrice.Enabled = false;
                    txtBxDiscount.Enabled = false;
                    listView.Enabled = false;
                    btnAddItem.Enabled = false;
                    btnRemoveItem.Enabled = false;
                    lblItemsTotalPrice.Enabled = true;
                    lblInvoiceTotalPrice.Enabled = true;

                    button5.Enabled = true;
                    btnUpdateCustomerData.Enabled = false;
                    btnCancel.Enabled = true;

                    ControlBox = true;


                    #endregion

                    break;

                case DialogResult.No:

                    return;

                default:

                    break;
            }
        }





工作BackgroundWorker





Do Work "BackgroundWorker"

private void BgWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            int StoreQuantity = 0;

            //try
            //{
                if (listView.Items.Count > 0)
                {

                    dbConnection StartConn = new dbConnection();
                    SQLiteConnection MyConnetion = StartConn.GetConnection();

                    #region Update Invoice Data In Database

                    SQLiteCommand Cm_1 = new SQLiteCommand("Update Invoices set Invoice_Description = @Invoice_Description, Invoice_TaxRate =@Invoice_TaxRate, Invoice_Other = @Invoice_Other, Invoice_Deposit = @Invoice_Deposit, Invoice_DetailTotal = @Invoice_DetailTotal, Invoice_InvoiceTotal = @Invoice_InvoiceTotal, Invoice_Note = @Invoice_Note where Invoice_Id = @Invoice_Id", MyConnetion);

                    Cm_1.Parameters.Add("@Invoice_Id", it.TempInvoice_Id.ToString());
                    Cm_1.Parameters.Add("@Invoice_Description", txtBxInvoiceDescription.Text);
                    Cm_1.Parameters.Add("@Invoice_TaxRate", txtBxInvoiceTaxRate.Text);
                    Cm_1.Parameters.Add("@Invoice_Other", txtBxInvoiceOther.Text);
                    Cm_1.Parameters.Add("@Invoice_Deposit", txtBxInvoiceDeposit.Text);
                    Cm_1.Parameters.Add("@Invoice_DetailTotal", lblItemsTotalPrice.Text);
                    Cm_1.Parameters.Add("@Invoice_InvoiceTotal", lblInvoiceTotalPrice.Text);
                    Cm_1.Parameters.Add("@Invoice_Note", txtBxInvoiceNote.Text);

                    MyConnetion.Open();

                    Cm_1.ExecuteNonQuery();

                    MyConnetion.Close();

                    #endregion


                    #region Insert Invoice Items

                    for (var i = 0; i < listView.Items.Count; i++)
                    {

                        SQLiteCommand Cm_2 = new SQLiteCommand("insert into InvoiceDetails (Invoice_Id,Item_Id,Item_Description,Item_NeededQuantity,Item_UnitPrice,Item_Discount,Item_Total) " +
                                    "values (@Invoice_Id,@Item_Id,@Item_Description,@Item_NeededQuantity,@Item_UnitPrice,@Item_Discount,@Item_Total)", MyConnetion);

                        Cm_2.Parameters.Add("@Invoice_Id", it.TempInvoice_Id.ToString());
                        Cm_2.Parameters.Add("@Item_Id", listView.Items[i].SubItems[0].Text);
                        Cm_2.Parameters.Add("@Item_Description", listView.Items[i].SubItems[1].Text);
                        Cm_2.Parameters.Add("@Item_NeededQuantity", listView.Items[i].SubItems[2].Text);
                        Cm_2.Parameters.Add("@Item_UnitPrice", listView.Items[i].SubItems[3].Text);
                        Cm_2.Parameters.Add("@Item_Discount", listView.Items[i].SubItems[4].Text);
                        Cm_2.Parameters.Add("@Item_Total", listView.Items[i].SubItems[5].Text);

                        MyConnetion.Open();

                        Cm_2.ExecuteNonQuery();

                        MyConnetion.Close();

                    }

                    #endregion

                    #region Update Items Quantity In Database

                    for (var i = 0; i < listView.Items.Count; i++)
                    {
                        SQLiteCommand Cm_3 = new SQLiteCommand(" select Item_StoreQuantity from Items where Item_Id = '" + listView.Items[i].SubItems[0].Text + "'", MyConnetion);

                        MyConnetion.Open();

                        int count = Convert.ToInt32(Cm_3.ExecuteScalar());

                        SQLiteDataReader dr = Cm_3.ExecuteReader();

                        dr.Read();

                        if (count != 0)
                        {
                            StoreQuantity = Convert.ToInt32(dr["Item_StoreQuantity"]);


                            MyConnetion.Close();

                        }
                        else
                        {

                        }

                        int updateStoreQuantity = StoreQuantity - Convert.ToInt32(listView.Items[i].SubItems[2].Text);

                        SQLiteCommand Cm_4 = new SQLiteCommand("Update Items set Item_StoreQuantity = @Item_StoreQuantity where Item_Id = @Item_Id", MyConnetion);

                        Cm_4.Parameters.Add("@Item_Id", listView.Items[i].SubItems[0].Text);
                        Cm_4.Parameters.Add("@Item_StoreQuantity", updateStoreQuantity.ToString());

                        MyConnetion.Open();

                        Cm_4.ExecuteNonQuery();

                        MyConnetion.Close();

                    }

                    #endregion

                }
                else
                {

                }
}

推荐答案

我写了一篇关于SQLite的文章,展示了如何使用同步和异步方法来回答所有问题:在C#中使用SQLite& VB [ ^ ]
I've written an article on SQLite that shows how to work with both synchronous and asynchronous methods that should answer all of your questions: Working with SQLite in C# & VB[^]


这篇关于如何在C#中使用后台工作程序在sqlite数据库中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆