c# - 删除按钮 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();
}
推荐阅读
- dom - DOM HTML 中的“HTML 编程 API”是什么意思?
- c - 理解 K&R 的 putc 宏:K&R 第 8 章(Unix 系统接口)练习 2
- python - 在 Pandas DataFrame 中反转列顺序的大 O 复杂度是多少?
- python - 将网络浏览器数据发送到另一个网络浏览器。(蟒蛇首选)
- r - choroplethrZip 返回“无法添加绑定”字幕错误 - 即使使用示例代码
- javascript - 关键帧背景图像动画不使用预加载图像
- ruby - 在 ruby 中将数据写入 csv 文件的问题
- python - 为 Keras 中的某些张量元素分配新值
- r - 累积对数正态函数不返回正确的概率
- c# - 发送到 Azure 事件中心错误