首页 > 解决方案 > 删除按钮 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);
        }
    }

非常感谢。

标签: c#function

解决方案


创建一些新的授权窗口和使用 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();
    }

推荐阅读