繁体   English   中英

如何检查输入的数据在SQLite中是否存在?

[英]How to check if the entered data is exist in SQLite?

我的PosMain SQLite数据库中有一个表名称Staff ,该表名称包含StaffIdStaffName属性

我有一个名为LoginWindows函数的窗口,如带有一个名为txtStaffId文本框的Login页面。

我想要的是当用户在txtStaffId中输入其ID时,系统将检查其ID是否存在于Staff表中。 如果存在,则系统将转到我的主窗口PosWindows并获取其数据,例如StaffIdStaffName

问题是我没有找到一种方法来检查txtStaffId的Id密钥是否存在。

我使用了这段代码,但是由于我使用的是FirstOrDefault因此它仅获取表中的第一个数据。 该代码位于我的业务层,称为SalesmanBo

 public int GetStaffId(int staffId)
    {
        try
        {

            using (PosMainDBContext db = new PosMainDBContext())
            {

                return db.Staff.Select(s => s.StaffId).FirstOrDefault();
            }
        }
        catch (Exception ex)
        {
            CustomExceptionHandling customExceptionHandling = new CustomExceptionHandling();
            customExceptionHandling.CustomExHandling(ex.ToString());
            return 0;
        }
    }

这是我在LoginWindows上登录的代码

 private void txtStaffId_KeyDown(object sender, KeyEventArgs e)
    {
        try
        {
            SalesmanBO salesmanBO = new SalesmanBO();
            Staff staff = new Staff();

            if (e.Key == Key.Return)
            {

                if (!string.IsNullOrEmpty(txtStaffId.Text))
                {
                    int salesmanId = Int32.Parse(txtStaffId.Text);
                    int staffId = salesmanBO.GetStaffId(staff.StaffId);

                    if (salesmanId == staffId )
                    {
                        PosWindows posWindows = new PosWindows();
                        posWindows.Show();
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("ID not Valid", "Alert", MessageBoxButton.OK, MessageBoxImage.Information);
                    }

                }
                else
                {
                    MessageBox.Show("Please Key In your ID", "Alert", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }

        }
        catch (Exception ex)
            { 
                CustomExceptionHandling customExceptionHandling = new CustomExceptionHandling();
                customExceptionHandling.CustomExHandling(ex.ToString());
            }

    }

你能帮我吗 我是WPF和SQLITE的新手

您提供的代码中有很多错误的地方。 例如,您在GetStaffId方法中传递了staffId ,但从未使用过。 我建议您检查Staff实体而不是仅检查staffId因为它可以进一步使用。 检查以下改进版本以了解一些想法。

public Staff GetStaffById(int staffId)
{
    try
    {
        using (PosMainDBContext db = new PosMainDBContext())
        {
            return db.Staff.Where(s => s.StaffId == staffId).FirstOrDefault();
        }
        catch (Exception ex)
        {
            CustomExceptionHandling customExceptionHandling = new CustomExceptionHandling();
            customExceptionHandling.CustomExHandling(ex.ToString());
            return null; // changed return 0 to return null
        }
    }
}

txtStaffId_KeyDown事件中更改以下代码行。

if (!string.IsNullOrEmpty(txtStaffId.Text))
{
    int salesmanId = Int32.Parse(txtStaffId.Text);
    staff = salesmanBO.GetStaffById(salesmanId);


    if (staff!=null) // no need to check salesmanId == staff.StaffId as we already checked while querying it.
    {
        PosWindows posWindows = new PosWindows();
        posWindows.Show();
        this.Close();
    }
    else
    {
        MessageBox.Show("ID not Valid", "Alert", MessageBoxButton.OK, MessageBoxImage.Information);
    }

}

UPDATE

GetStaffById方法中,将返回类型更改为return null

无需创建新的Staff实例。 更改Staff staff = new Staff(); Staff staff;

暂无
暂无

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

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