如何检查数据库中是否存在值并显示消息 [英] How to check if value exists in database or not and show a message

查看:83
本文介绍了如何检查数据库中是否存在值并显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void btnSearch_Click(object sender, EventArgs e)
        {


            if (searchtext.Text == "")
            {
                MessageBox.Show("Plese Inter Employee's ID you are searching for");
                searchtext.Focus();
            }
            else
            {
                
                OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Employees.mdb");
                OleDbDataAdapter ad = new OleDbDataAdapter("select * from Employee where EmpID=@EmpID", con);
                ad.SelectCommand.Parameters.Add("@EmpID", OleDbType.Integer);
                ad.SelectCommand.Parameters["@EmpID"].Value = int.Parse(searchtext.Text);


                DataSet ds = new DataSet();
                ad.Fill(ds, "Emp");
                DGV1.DataSource = ds.Tables["Emp"];

               
                if (ds == null)
                {
                    
                    MessageBox.Show("not found!!");

                }
                else
                {
                   MessageBox.Show("The Employee you are searching for is listed down, if you want to update the data click the arrow sign on the lift side of the record");
                }
            }

推荐答案

试试



Try

if(ds.Tables["Emp"].Rows.Count>0)
{
   MessageBox.Show("Found !!");
}
else
{
   MessageBox.Show("not found!!");
}


尝试类似

If(ds.Tables [0] .HasRows)

发现消息员工
Try something like
If(ds.Tables[0].HasRows)
Message employee found


这篇关于如何检查数据库中是否存在值并显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆