我想在我的库存系统中放置一个功能,当程序检测到数据库中的项目数量低于15时,将出现一个警告消息框。我不知道使用什么方法/命令以及放在哪里。
这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace inventoryForm
{
public partial class inventory : Form
{
private OleDbConnection connection = new OleDbConnection();
public inventory()
{
InitializeComponent();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Eziera Yvanne\Documents\Inventory.accdb; Persist Security Info = False";
}
private void inventory_Load(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select * from Inventory";
command.CommandText = query;
// Load Inventory table to ComboBox
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
combo_items.Items.Add(reader["Item"].ToString());
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
// Inventory ComboBox
private void combo_inventory_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select * from Inventory where Item='" + combo_items.Text + "'";
command.CommandText = query;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
text_quantity.Text = reader["Quantity"].ToString();
label_itemID.Text = reader["ItemID"].ToString();
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
// Button Load/Refresh the table
private void btn_loadTable_Click(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select * from Inventory";
command.CommandText = query;
int itemCount = 0;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
itemCount++;
int quantity;
if (quantity > 15)
{
}
}
// Connect Inventory table to Grid
OleDbDataAdapter data = new OleDbDataAdapter(command);
DataTable inventory_table = new DataTable();
data.Fill(inventory_table);
dataGridView_inventory.DataSource = inventory_table;
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
// Button Update
private void btn_update_Click(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "update Inventory set Quantity='" + text_quantity.Text + "' where ItemID=" + label_itemID.Text + "";
command.CommandText = query;
command.ExecuteNonQuery();
MessageBox.Show("Update Successful");
text_quantity.Text="";
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
}
}
并且
答案 0 :(得分:0)
根据您的代码,当您获得所选项目的数量时,您应该在combo_inventory_SelectedIndexChanged
方法中进行测试。
如果用户碰巧选择数量少于15的商品,则只会弹出警告。
否则,如果您的意思是某种自动方式,那么您需要以固定的时间间隔(计时器)或固定的位置执行查询,数量少于15。应用程序工作流程。
对于查询,它与您填写ComboBox的方式非常类似,但您只需添加WHERE
子句即可返回带有qty<的项目。 15,然后你要为每个项目弹出一个消息框,或弹出一个消息框,显示"错误"项目
如果您需要更具体的答案,请更新您的问题并指定您希望实施的方法(计时器或工作流程)。
干杯
编辑:
根据您的评论,我将这个概念证明代码放在一起。部分内容在Program
类中,我假设您将其作为Windows窗体应用程序的一部分:
static class Program
{
public static event EventHandler CheckInventoryEvent;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
System.Timers.Timer tmr = new System.Timers.Timer(300000); //Every 5 minutes
tmr.Elapsed += Tmr_Elapsed;
tmr.Start();
Application.Run(new MainForm());
tmr.Stop();
}
private static void Tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
OnCheckInventoryEvent();
}
public static void OnCheckInventoryEvent()
{
if (CheckInventoryEvent != null)
CheckInventoryEvent(null, EventArgs.Empty);
}
}
解决方案的其他部分将显示在您向用户显示的任何表单中。
例如,让我们说这是库存表单(可以是您的应用程序中的任何表单):
public InventoryForm()
{
InitializeComponent();
Program.CheckInventoryEvent += Program_CheckInventoryEvent;
}
private void Program_CheckInventoryEvent(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select Item, Quantity from Inventory WHERE Quantity < 15";
command.CommandText = query;
// Find low Inventory items and display in dialog
StringBuilder sb = new StringBuilder();
OleDbDataReader reader = command.Execute();
while (reader.Read())
{
sb.AppendLine(string.Format("{0} qty: {1}"), reader["Item"].ToString(), reader["Quantity"].ToString());
}
connection.Close();
MessageBox.Show(sb.ToString(), "Low Inventory Items");
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
这很粗糙,只是作为POC,所以请随意适应。
我在Program类中拥有计时器的原因是,对于您的用户当前使用的窗口/ GUI并不重要,计时器将在任何情况下触发。
一项改进是将支票和随附的对话框移到自己的班级,这样你就不会在每个表格中重复相同的代码。
我希望这会让你走上正轨。
干杯