如果数量少于5个项目,如何显示警告消息

时间:2014-03-15 19:21:07

标签: c# mysql

我正在创建一个库存系统,该系统应该显示需要重新进货的产品。如果产品数量仅为5,则应显示一条消息,说明您现在需要重新进货。但我不知道该怎么做,因为我只创建一个库存系统,而不是销售和库存,那么如果我没有销售系统,我应该如何扣除呢?对不起,希望你们明白。

这是我的代码:

public void all()
{
        SqlConnection MySqlConnection;
        DataTable p_table = new DataTable();

        MySqlConnection = new SqlConnection("Data Source=christina\\sqlexpress;Initial Catalog=cafe_inventory;User ID=sa;Password=tina;");

        MySqlConnection.Open();
        SqlCommand command1 = new SqlCommand("Select * from inventory", MySqlConnection);

        //Clear the datatable to prevent duplicate generation of data in gridview.
        p_table.Clear();
        SqlDataAdapter m_da = new SqlDataAdapter("Select * from inventory", MySqlConnection);
        //DataSet ds = new DataSet();
        //DataTable dtable = ds.Tables["empinfo1"];
        m_da.Fill(p_table);
        // Clear the ListView control
        //listView3.Items.Clear();

        // Display items in the ListView control
        for (int i = 0; i < p_table.Rows.Count; i++)
        {
            DataRow drow = p_table.Rows[i];

            // Only row that have not been deleted
            if (drow.RowState != DataRowState.Deleted)
            {
                // Define the list items
                //(drow["bnum"].ToString());
                ListViewItem lvi = new ListViewItem(drow["pnum"].ToString());
                lvi.SubItems.Add(drow["pname"].ToString());
                lvi.SubItems.Add(drow["descr"].ToString());
                lvi.SubItems.Add(((DateTime)drow["dater"]).ToShortDateString());
                //lvi.SubItems.Add(drow["exp"].ToString());
                lvi.SubItems.Add(((DateTime)drow["exp"]).ToShortDateString());
                lvi.SubItems.Add(drow["qt"].ToString());

                // Add the list items to the ListView
                listView2.Items.Add(lvi);
            }
        }
    }

编辑代码:

public void stock()
    {

        SqlConnection MySqlConnection;
        DataTable p_table = new DataTable();

        MySqlConnection = new SqlConnection("Data Source=christina\\sqlexpress;Initial Catalog=cafe_inventory;User ID=sa;Password=tina;");

        MySqlConnection.Open();
        SqlCommand command1 = new SqlCommand("Select pname from inventory where qt < 5", MySqlConnection);

        //Clear the datatable to prevent duplicate generation of data in gridview.
        p_table.Clear();
        SqlDataAdapter m_da = new SqlDataAdapter("Select pname from inventory where qt < 5", MySqlConnection);
        m_da.Fill(p_table);

        SqlDataReader reader;
        reader = command1.ExecuteReader();

        StringBuilder productNames = new StringBuilder();

        while (reader.Read())
        {
            productNames.Append(reader["pname"].ToString() + Environment.NewLine);
        }

        MySqlConnection.Close();

        MessageBox.Show("There are products that needs restocking, check to restock now." + productNames);

        // Display items in the ListView control
        for (int i = 0; i < p_table.Rows.Count; i++)
        {

            DataRow drow = p_table.Rows[i];

            // Only row that have not been deleted
            if (drow.RowState != DataRowState.Deleted)
            {
                // Define the list items
                //ListViewItem lvi = new ListViewItem(drow["bnum"].ToString());
                ListViewItem lvi = new ListViewItem(drow["pnum"].ToString());
                lvi.SubItems.Add(drow["pname"].ToString());
                lvi.SubItems.Add(drow["descr"].ToString());
                lvi.SubItems.Add(((DateTime)drow["dater"]).ToShortDateString());
                //lvi.SubItems.Add(drow["exp"].ToString());
                lvi.SubItems.Add(((DateTime)drow["exp"]).ToShortDateString());
                lvi.SubItems.Add(drow["qt"].ToString());
                lvi.SubItems.Add(drow["interval"].ToString());

                // Add the list items to the ListView
                listView4.Items.Add(lvi);
            }
        }

    }

2 个答案:

答案 0 :(得分:0)

试试这个:

void DisplayLowQuantityItems()
{
    MySqlConnection con = new MySqlConnection("Data Source=christina\\sqlexpress;
    Initial Catalog=cafe_inventory;User ID=sa;Password=tina;");
    MySqlCommand command = new MySqlCommand("Select pname from inventory 
                                                  where qt < 5", con);
    con.Open();
    MySqlDataReader reader = commad.ExecuteReader();
    StringBuilder productNames= new StringBuilder();
    while(reader.Read())
    {
        productNames.Append(reader["pname"].ToString()+Environment.NewLine);
    }
    con.Close();
    MessageBox.Show("Following Products quantity is lessthan 5\n"+productNames);
}

解决方案2:

编辑代码:

public void stock()
    {

        MySqlConnection con;
        DataTable p_table = new DataTable();

        con = new MySqlConnection("Data Source=christina\\sqlexpress;Initial 
                             Catalog=cafe_inventory;User ID=sa;Password=tina;");

        con.Open();
        MySqlCommand command1 = new MySqlCommand("Select pname from inventory where
                                                         qt < 5", con);

        //Clear the datatable to prevent duplicate generation of data in gridview.
        p_table.Clear();
        MySqlDataAdapter m_da = new MySqlDataAdapter("Select * from inventory where
                                                          qt < 5", con);
        m_da.Fill(p_table);

        MySqlDataReader reader;
        reader = command1.ExecuteReader();

        StringBuilder productNames = new StringBuilder();

        while (reader.Read())
        {
            productNames.Append(reader["pname"].ToString() + Environment.NewLine);
        }

        con.Close();

        MessageBox.Show("There are products that needs restocking, 
                                            check to restock now." + productNames);

        // Display items in the ListView control
        for (int i = 0; i < p_table.Rows.Count; i++)
        {

            DataRow drow = p_table.Rows[i];

            // Only row that have not been deleted
            if (drow.RowState != DataRowState.Deleted)
            {
                // Define the list items
                //ListViewItem lvi = new ListViewItem(drow["bnum"].ToString());
                ListViewItem lvi = new ListViewItem(drow["pnum"].ToString());
                lvi.SubItems.Add(drow["pname"].ToString());
                lvi.SubItems.Add(drow["descr"].ToString());
                lvi.SubItems.Add(((DateTime)drow["dater"]).ToShortDateString());
                //lvi.SubItems.Add(drow["exp"].ToString());
                lvi.SubItems.Add(((DateTime)drow["exp"]).ToShortDateString());
                lvi.SubItems.Add(drow["qt"].ToString());
                lvi.SubItems.Add(drow["interval"].ToString());

                // Add the list items to the ListView
                listView4.Items.Add(lvi);
            }
        }

    }

答案 1 :(得分:0)

另一种检查数据的方法

    DataTable p_table = new DataTable();

    con = new MySqlConnection("Data Source=christina\\sqlexpress;Initial 
                         Catalog=cafe_inventory;User ID=sa;Password=tina;");

    con.Open();
    MySqlCommand command1 = new MySqlCommand("Select pname from inventory where
                                                     qt < 5", con);
    p_table.load(command1.ExecuteReader());