删除按钮C#的密码验证条件



我有一个删除按钮,它可以从数据库中删除记录。我需要添加一个";输入您的密码";弹出表单,以防止用户意外删除。只有拥有密码的管理员才能删除数据。

这是我的按钮代码:

private void btnDelete_Click(object sender, EventArgs e)
{
if (MessageBox.Show("If you are not a System Administrator Please do not touch.", "Remove Row", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{

sqlCon.Open();
SqlCommand sqlCmd = new SqlCommand("ContactDeleteByID", sqlCon);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@PhoneBookID", PhoneBookID);
sqlCmd.ExecuteNonQuery();
MessageBox.Show("Deleted Successfully");
Clear();
GridFill();
}
}
else
{
MessageBox.Show("Row Not Removed", "Remove Row", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

非常感谢。

创建一些新窗口以进行授权和使用Window.ShowDialog()方法

private void btnDelete_Click(object sender, RoutedEventArgs e)
{
var auth = new AuthWindow();
auth.ShowDialog();
if (auth.IsAdmin)
{
//do smth
}
else
{
MessageBox.Show("Row Not Removed", "Remove Row", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

}

AuthWindow.cs

private void btnOk_Click(object sender, RoutedEventArgs e)
{
isAdmin = tb.Text == "SomePass";
this.Hide();
}

最新更新