简体   繁体   English

检查MYSQL数据库中的某个表是否为空C#(骨架表)

[英]Check if a certain Table in MYSQL Database is empty C# (skeleton table)

My application starts with the login menu, a user provides his login credentials and the process of comparing passwords (hashed) goes on, logged in if everything is fine and error handlers kick in if there is an error.我的应用程序从登录菜单开始,用户提供他的登录凭据,然后比较密码(散列)的过程继续进行,如果一切正常则登录,如果出现错误则错误处理程序启动。

My question is, is there a way to check if the targeted table is empty like not having any sort of data in it (just a skeleton table in a database).我的问题是,有没有办法检查目标表是否为空,就像其中没有任何类型的数据一样(只是数据库中的骨架表)。 Because I am not willing 150+ employees to the table and they may leave their job, get promoted and get fired... so I wanna leave it for the admins running the HR of the company...因为我不愿意 150 多名员工坐在桌子上,他们可能会离职、升职和被解雇......所以我想把它留给管理公司人力资源的管理员......

I used the Form_Activated event but nothing changed, tried the Form_Initialize event no luck.我使用了Form_Activated事件但没有任何改变,尝试了Form_Initialize事件没有运气。 What am I doing wrong here?我在这里做错了什么?

Should I change my query?我应该更改我的查询吗? I am totally lost here cause I read through dozens of forms and NON got even close!我完全迷失在这里,因为我阅读了数十种表格,而NON甚至接近了!

Using the code provided with the form initialize event did not work.使用表单initialize事件提供的代码不起作用。 for it will dispose the form and you just can not get around the problem or at least I couldn't!因为它会处理表单,而您无法解决问题,或者至少我不能!



try
{
    using (MySqlConnection connection = Connect())
    {
        DataTable table = new DataTable("employee");
        string checkuserexistance = "select count(uname) from employee";
        MySqlCommand command = new MySqlCommand(checkuserexistance, connection);
        using (MySqlDataReader reader = command.ExecuteReader())
        {
            if (reader.Read() && reader.FieldCount > 0 && reader.HasRows)
            {
                Form1_Load(sender, e);
                reader.Close();
            }
            else
            {
                DialogResult dialog = MessageBox.Show("Can not sign in as the given user, would you like to add a user now?", "Empty Database", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (dialog == DialogResult.Yes)
                {
                    new Thread(() => new User_Managment().ShowDialog()).Start();
                    this.Close();
                }
                else
                {
                    Application.Exit();
                }
            }
        }
    }
}
catch (MySqlException ex)
{
    MessageBox.Show(ex.Message, "Error Connecting to Database!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

Your logic is currently checking whether there are any rows returned:您的逻辑当前正在检查是否有任何行返回:

MySqlCommand command = new MySqlCommand("select count(uname) from employee", connection);
using (MySqlDataReader reader = command.ExecuteReader())
{
    if (reader.Read() && reader.FieldCount > 0 && reader.HasRows)
    {
        // OK
    }
}

However, a SELECT COUNT(...) always returns (at least) one row so you'll need to also check that the count read from that single line is more than zero by reading the zeroth result column's value.但是, SELECT COUNT(...)始终返回(至少)一行,因此您还需要通过读取第零个结果列的值来检查从该单行读取的计数是否大于零。

MySqlCommand command = new MySqlCommand("select count(uname) from employee", connection);
using (MySqlDataReader reader = command.ExecuteReader())
{
    if (reader.Read() && reader.FieldCount > 0 && reader.HasRows && reader.GetInt32(0) > 0)
    {
        // OK
    }
}

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

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