简体   繁体   English

将Windows窗体文本框输入值与数据库C#SQL中的值进行比较

[英]Comparing Windows Form Textbox input value to value in database C# SQL

I am looking to compare the input on a windows form textbox to a field in a database. 我希望将Windows窗体文本框中的输入与数据库中的字段进行比较。 The table is 'products' and the field is 'ReOrderlevel'. 表格为“产品”,字段为“ ReOrderlevel”。 If the value entered into the text box is less than the ReOrderlevel, I need a message box to show 'Please Order Now'. 如果在文本框中输入的值小于ReOrderlevel,则需要一个消息框来显示“请立即订购”。 I am completely new to C# and not sure if any of this will make sense but I will put my effort below in case anyone can work out what I am going on about! 我对C#完全陌生,不知道这是否有意义,但我会尽力而为,以防任何人能弄清楚我将要发生的事情! Thank you so much in advance ! 提前非常感谢您!

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 MySql.Data.MySqlClient;
using System.Text.RegularExpressions;


namespace rescue_centre
{
public partial class FrmStock : Form
{
    public FrmStock()
    {
        InitializeComponent();
    }

    private void FrmStock_Load(object sender, EventArgs e)
    {

        refresh_data();
    }
    public void refresh_data()
    {
        string Query = "select * from stock";
        MySqlConnection mycon = new     MySqlConnection("datasource=localhost;username=root;password='';database=fsd;");            MySqlCommand cmd = new MySqlCommand(Query, mycon);
        MySqlDataAdapter adapter = new MySqlDataAdapter();
        MySqlDataReader Mdr;

        adapter.SelectCommand = cmd;
        DataTable dTable1 = new DataTable();
        adapter.Fill(dTable1);
        DtgStock.DataSource = dTable1;

        mycon.Open();
        Query = "Select * from stock";
        cmd = new MySqlCommand(Query, mycon);
        Mdr = cmd.ExecuteReader();

        CmbPrd.Items.Clear();
        while (Mdr.Read())
        {
            CmbPrd.Items.Add(Mdr.GetString("ProdCode"));
        }
        mycon.Close();

        mycon.Open();
        Query = "Select * from stock";
        cmd = new MySqlCommand(Query, mycon);
        Mdr = cmd.ExecuteReader();

        CmbLoc.Items.Clear();
        while (Mdr.Read())
        {
            CmbLoc.Items.Add(Mdr.GetString("LocCode"));
        }
        mycon.Close();
    }
   private void QtyTxt_Validating(object sender, CancelEventArgs e)
    {
        string query = "Select ReOrderlevel from Product where (ProdCode)='" + CmbPrd.Text + "';";

        MySqlConnection mycon = new MySqlConnection("datasource=localhost;username=root;password='';database=fsd");
        MySqlCommand cmd = new MySqlCommand(query, mycon);

        if
            QtyTxt.Text<ReOrderLevel
            MessageBox.Show("Stock Low Please Order Now")

        mycon.Open();
        cmd.ExecuteNonQuery();
        mycon.Close();
        refresh_data();

You need to execute the query before the if statement. 您需要在if语句之前执行查询。 I'm going to assume that ReOrderlevel is an integer and your query always returns one row or more. 我将假设ReOrderlevel是一个整数,并且您的查询始终返回一行或更多行。

MySqlCommand cmd = new MySqlCommand(query, mycon);
int ReOrderLevel = Convert.ToInt32(cmd.ExecuteScalar());

if(Convert.ToInt32(QtyTxt.Text) < ReOrderLevel)
{
    MessageBox.Show("Stock Low Please Order Now");
}

As a side note, you don't want to query the database 3 times for the Stock table. 另外,您不想在数据库中查询3次“库存”表。 On the refresh_data method after using adapter.Fill(dTable1); 关于使用adapter.Fill(dTable1);之后的refresh_data方法 you could load the combo boxes like this instead: 您可以改为这样加载组合框:

CmbPrd.Items.Clear();
CmbLoc.Items.Clear();
foreach (DataRow row in dTable1.Rows)
{
   CmbPrd.Items.Add((string)row["ProdCode"]);
   CmbLoc.Items.Add((string)row["LocCode"]);
}

And please use parameterized queries to avoid SQL injection. 并且请使用参数化查询以避免SQL注入。 Code sample 代码样例

string query = "Select ReOrderlevel from Product where (ProdCode)= @prodcode;";
cmd.Parameters.AddWithValue("@prodcode", CmbPrd.Text); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM