【发布时间】:2021-12-11 06:07:20
【问题描述】:
我的应用程序从登录菜单开始,用户提供他的登录凭据,然后比较密码(散列)的过程继续进行,如果一切正常,则登录,如果出现错误,错误处理程序就会启动。
我的问题是,有没有办法检查目标表是否为空,就像其中没有任何类型的数据(只是数据库中的骨架表)。因为我不愿意让 150 多名员工坐在一起,他们可能会离职、升职、被解雇……所以我想把它留给管理公司 HR 的管理员……
我使用了Form_Activated 事件但没有任何改变,尝试了Form_Initialize 事件没有运气。我在这里做错了什么?
我应该更改我的查询吗?我在这里完全迷失了,因为我阅读了几十个表格,NON甚至接近了!
使用initialize 事件表单提供的代码不起作用。因为它会处理表单,而您无法解决问题,或者至少我无法解决!
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);
}
【问题讨论】:
-
如果我对您的理解正确,也许可以考虑重构您的应用程序,以便整个登录/存在任何用户过程都采用自己的形式,毕竟您只显示经过身份验证的主表单完成了吗?
-
@AKX,我找到了一个更好的解决方案,结果我只需要编辑我的连接字符串
string checkuserexistance = "select LEAST(1,uname) from employee";感谢您的想法。
标签: c# mysql windows database-table