无法从DoWork()内部获取MySQL数据库中的信息

时间:2011-03-19 21:35:10

标签: c# .net mysql winforms listview

我正在使用Background worker,我已经测试了这个背景工作者类的MySQL / C#select查询,并且它完美地工作。但是当我尝试使用backgroundworker时,所有的字符串值都是空的!

    void bWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        if (action != Action.ViewOrder)
        {
            if (HasErrors != null || HasErrors != "" || HasErrors != String.Empty)
            {
                Alert(HasErrors,
                    "Retry",
                    Error,
                    true,
                    Color.FromArgb(249, 87, 55)
                );
            }
            else
            {
                var found = false;

                foreach (var item in thelist.Items)
                {
                    if (item.ToString().Contains(OrderNumber))
                        found = true;
                }
                if (thelist.Items.Count == 0)
                {
                    ListViewItem LItem = new ListViewItem(OrderNumber);
                    ListViewItem.ListViewSubItemCollection SubItems = new ListViewItem.ListViewSubItemCollection(LItem);

                    SubItems.Add(FirstName + " " + LastName);
                    SubItems.Add(EmailAddress);
                    SubItems.Add(DeliveryAddress);
                    SubItems.Add(Company);
                    SubItems.Add(PhoneNumber);

                    thelist.Items.Add(LItem);
                    thelist.Update();
                }
            }
        }
    }

    void bWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        SlideTimer.Enabled = true;
        Alert("All unconfirmed orders have been retrieved.",
            "Dismiss",
            Information,
            true,
            Color.FromArgb(63, 187, 249)
        );
        action = Action.None;
    }


    void bWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 1; (i <= 10); i++)
        {
            if (bWorker.CancellationPending == true)
            {
                e.Cancel = true;
            }
            else
            {
                if (action == Action.ViewOrder)
                {
                    // Connect to Database, check for orders, and end.
                            string cs = "server=" +
                                Settings.Default.Server +
                                ";username=" +
                                Settings.Default.Username +
                                ";database=" +
                                Settings.Default.Database +
                                ";port=3306;password=BLAH" +
                            ";";
                    MySql.Data.MySqlClient.MySqlConnection msc = new MySql.Data.MySqlClient.MySqlConnection(cs);
                    try
                    {
                        msc.Open();

                        // Check for new orders.
                        string st = "SELECT sessionid, firstname, lastname, email, streetaddress, suburb, postcode, state, company, phone FROM mysql_9269_dbase.order";
                        MySql.Data.MySqlClient.MySqlCommand cd = new MySql.Data.MySqlClient.MySqlCommand(st, msc);
                        MySql.Data.MySqlClient.MySqlDataReader msdr = cd.ExecuteReader();

                        while (msdr.Read())
                        {
                            OrderNumber = msdr[0].ToString();
                            FirstName = msdr[1].ToString();
                            LastName = msdr[2].ToString();
                            EmailAddress = msdr[3].ToString();
                            DeliveryAddress = msdr[4].ToString() + " " + msdr[5].ToString() + " " + msdr[6].ToString() + " " + msdr[7];
                            Company = msdr[8].ToString();
                            PhoneNumber = msdr[9].ToString();


                           // System.Threading.Thread.Sleep(500);
                            bWorker.ReportProgress((i * 10));
                        }

                        msc.Close();
                    }
                    catch (Exception en)
                    {
                        HasErrors = en.Message;
                    }

                }
            }
        }
    }

我知道表中有信息,我也知道mysql查询没有任何问题,因为如果我不使用backgroundworker就可以了。

为什么它不适用于BackgroundWorker?

1 个答案:

答案 0 :(得分:0)

我发现两个主要问题会阻止您的列表更新。第一个是bWorker_ProgressChanged中HasErrors字符串的if语句。当你将这些条件组合在一起时,你也可以说if(true),因为每个字符串都不是空的或不是空的。我认为你可以完成你的目标:

if(!string.IsNullOrEmpty(HasErrors))

另一个问题是thelist仅在函数第一次运行时变为空。您需要将if (thelist.Items.Count == 0)更改为if (!found)