C# with Access DB - 达到临界水平时将警报框放入库存
C# with Access DB - Put Alert Box in Inventory when critical level is reached
我想在我的库存系统中添加一个功能,当程序检测到数据库中的项目数量低于 15 时,会出现一个警告消息框。我不知道是什么 method/command使用方法和放置位置。
这是我的代码:
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);
}
}
}
}
和
根据您的代码,当您获得 selected 项目的数量时,您应该在 combo_inventory_SelectedIndexChanged
方法中进行测试。
如果用户碰巧 select 数量少于 15 的文章,这只会弹出警告。
否则,如果您的意思是您想要某种自动化方式,那么您将需要以固定的时间间隔(计时器)或在应用程序工作流中的固定位置对小于 15 的数量执行查询.
至于查询,它与您已经为填充 ComboBox 所做的非常相似,但您只需将 WHERE
子句添加到 return 数量 < 15 的项目,然后你要么为每个项目弹出一个消息框,要么弹出一个显示 "faulty" 项列表的消息框。
如果您需要更具体的答案,请更新您的问题并指定您希望实施的方法(计时器或工作流)。
干杯
编辑:
根据您的意见,我整理了这个概念验证代码。它的一部分在 Program
class 中,我假设您已经将其作为 Windows Forms 应用程序的一部分:
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);
}
}
解决方案的另一部分将在您向用户显示的任何表单中。
例如,假设这是 Inventory 表单(可以是您应用程序中的任何表单):
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,所以请随意改编。
我在程序 class 中设置定时器的原因是,无论您的用户当前使用哪个 windows/GUI,定时器都会在任何情况下触发。
一个改进是将检查和随附的对话框移动到它自己的 class 这样您实际上就不会在每个表单中重复相同的代码。
我希望这能让你走上正轨。
干杯
我想在我的库存系统中添加一个功能,当程序检测到数据库中的项目数量低于 15 时,会出现一个警告消息框。我不知道是什么 method/command使用方法和放置位置。
这是我的代码:
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);
}
}
}
}
和
根据您的代码,当您获得 selected 项目的数量时,您应该在 combo_inventory_SelectedIndexChanged
方法中进行测试。
如果用户碰巧 select 数量少于 15 的文章,这只会弹出警告。
否则,如果您的意思是您想要某种自动化方式,那么您将需要以固定的时间间隔(计时器)或在应用程序工作流中的固定位置对小于 15 的数量执行查询.
至于查询,它与您已经为填充 ComboBox 所做的非常相似,但您只需将 WHERE
子句添加到 return 数量 < 15 的项目,然后你要么为每个项目弹出一个消息框,要么弹出一个显示 "faulty" 项列表的消息框。
如果您需要更具体的答案,请更新您的问题并指定您希望实施的方法(计时器或工作流)。
干杯
编辑:
根据您的意见,我整理了这个概念验证代码。它的一部分在 Program
class 中,我假设您已经将其作为 Windows Forms 应用程序的一部分:
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);
}
}
解决方案的另一部分将在您向用户显示的任何表单中。
例如,假设这是 Inventory 表单(可以是您应用程序中的任何表单):
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,所以请随意改编。
我在程序 class 中设置定时器的原因是,无论您的用户当前使用哪个 windows/GUI,定时器都会在任何情况下触发。
一个改进是将检查和随附的对话框移动到它自己的 class 这样您实际上就不会在每个表单中重复相同的代码。
我希望这能让你走上正轨。
干杯