检查现有项目无效

时间:2011-02-17 14:27:45

标签: c# .net mysql winforms listview

在我的winforms应用程序中,我有一个列表视图控件,其中填充了新的订单信息。

我正在使用DISTINCT(某些东西)来获取数据库表中每个东西中的一个,我也在C#中进行检查,这样如果它已经在listview控件中,那么就不要添加它。

但是它一直在从listview控件中已经存在的表中添加垃圾,并且它也不遵守DISTINCT命令!

这是我的代码,任何帮助都非常感谢,谢谢:

// Check for new orders.
            MySql.Data.MySqlClient.MySqlConnection msc = new MySql.Data.MySqlClient.MySqlConnection(cs);
            try
            {



                this.Cursor = Cursors.WaitCursor;

                msc.Open();

                // Check for orders now.
                string st = "SELECT DISTINCT(sessionid), firstname, lastname, email, streetaddress, suburb, postcode, state, phone, company 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())
                {
                    if (thelist.Items.Count == 0 || !thelist.Items[0].Text.Contains(msdr[0].ToString()))
                    {
                        ListViewItem LItem = new ListViewItem(msdr[0].ToString());
                        ListViewItem.ListViewSubItemCollection SubItems = new ListViewItem.ListViewSubItemCollection(LItem);

                        SubItems.Add(msdr[1].ToString());
                        SubItems.Add(msdr[2].ToString());
                        SubItems.Add(msdr[3].ToString());
                        SubItems.Add(msdr[4].ToString() + " " + msdr[5].ToString() + " " + msdr[6].ToString() + " " + msdr[7]);
                        SubItems.Add(msdr[8].ToString());
                        SubItems.Add(msdr[9].ToString());

                        thelist.Items.Add(LItem);

                        thelist.Update();
                    }

                }


            }
            catch (Exception en)
            {
                MessageBox.Show(en.Message, "Uh, oohhhhhh!");
            }
            msc.Close();

            this.Cursor = Cursors.Arrow;

4 个答案:

答案 0 :(得分:1)

问题在于:

 if (thelist.Items.Count == 0 || 
     !thelist.Items[0].Text.Contains(msdr[0].ToString()))

在if条件下,您只是与列表视图中第一项的文本进行比较 - 您应该检查所有这些项目。

你可以这样做:

bool hasItem = false;
foreach (ListViewItem item in thelist.Items)
    if (item.Text.Contains(msdr[0].ToString())) hasItem = true;

if (!hasItem)
{
    //...
}

答案 1 :(得分:0)

不确定数据库的内容,但似乎您只是检查列表框中的FIRST元素是否与您要添加的元素相同:

 if (thelist.Items.Count == 0 || !thelist.Items[0].Text.Contains(msdr[0].ToString()))

你需要遍历整个列表。或许这样的事情(未经测试)?

 var found = false;
 foreach (var item in thelist.Items)
 {
   if (item.Text.Contains(msdr[0].ToString())
   {
     found = true;
     break;
   }
 }
 if (thelist.Items.Count == 0 || !found)
 {
   ....

答案 2 :(得分:0)

要修复查询,您需要添加GROUP BY。

string st = "SELECT DISTINCT(sessionid), firstname, lastname, email, streetaddress, suburb, postcode, state, phone, company FROM mysql_9269_dbase.order" GROUP BY sessionid;

那应该照顾那一部分。

答案 3 :(得分:0)

所以,感谢@CodingInsomnia和@BrokenGlass,这里有完整的代码片段:

// Check for new orders.
            MySql.Data.MySqlClient.MySqlConnection msc = new MySql.Data.MySqlClient.MySqlConnection(cs);
            try
            {



                this.Cursor = Cursors.WaitCursor;

                msc.Open();

                // Check for orders now.
                string st = "SELECT DISTINCT(sessionid), firstname, lastname, email, streetaddress, suburb, postcode, state, phone, company 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())
                {
                    if (thelist.Items.Count == 0)
                    {
                        ListViewItem LItem = new ListViewItem(msdr[0].ToString());
                        ListViewItem.ListViewSubItemCollection SubItems = new ListViewItem.ListViewSubItemCollection(LItem);

                        SubItems.Add(msdr[1].ToString());
                        SubItems.Add(msdr[2].ToString());
                        SubItems.Add(msdr[3].ToString());
                        SubItems.Add(msdr[4].ToString() + " " + msdr[5].ToString() + " " + msdr[6].ToString() + " " + msdr[7]);
                        SubItems.Add(msdr[8].ToString());
                        SubItems.Add(msdr[9].ToString());

                        thelist.Items.Add(LItem);

                        thelist.Update();
                    }
                    else
                    {
                        var found = false; foreach (var item in thelist.Items)
                        {
                            if (item.ToString().Contains(msdr[0].ToString()))
                                found = true;
                        }
                        if (thelist.Items.Count == 0 || !found)
                        {
                            ListViewItem LItem = new ListViewItem(msdr[0].ToString());
                            ListViewItem.ListViewSubItemCollection SubItems = new ListViewItem.ListViewSubItemCollection(LItem);

                            SubItems.Add(msdr[1].ToString());
                            SubItems.Add(msdr[2].ToString());
                            SubItems.Add(msdr[3].ToString());
                            SubItems.Add(msdr[4].ToString() + " " + msdr[5].ToString() + " " + msdr[6].ToString() + " " + msdr[7]);
                            SubItems.Add(msdr[8].ToString());
                            SubItems.Add(msdr[9].ToString());

                            thelist.Items.Add(LItem);

                            thelist.Update();
                        }
                    }
                }
            }
            catch (Exception en)
            {
                MessageBox.Show(en.Message, "Uh, oohhhhhh!");
            }
            msc.Close();

            this.Cursor = Cursors.Arrow;