gpt4 book ai didi

c# - 消息框不显示数据库中是否有受影响的行 C# MySQL

转载 作者:行者123 更新时间:2023-11-29 22:09:02 25 4
gpt4 key购买 nike

我已经使用此代码成功更新了我的数据库:

        private void btnUpdt_Click(object sender, EventArgs e)
{
connect_db();

string name = txtName.Text;
string rep = txtRep.Text;
string contact = txtCntctNum.Text;
string address = txtAdd.Text;
string email = txtEmail.Text;

try
{
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "UPDATE clients SET client_Rep = @rep, client_Address = @address, client_CntctNum = @contact, client_Email = @email WHERE client_Name = @name";
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@rep", rep);
cmd.Parameters.AddWithValue("@address", address);
cmd.Parameters.AddWithValue("@contact", contact);
cmd.Parameters.AddWithValue("@email", email);
cmd.Connection = connect;

MySqlDataReader editClients = cmd.ExecuteReader();

txtName.Clear();
txtRep.Clear();
txtAdd.Clear();
txtCntctNum.Clear();
txtEmail.Clear();

txtName.ReadOnly = false;

btnReg.Text = "REGISTER";
btnReg.Name = "btnReg";
btnReg.Click -= this.btnUpdt_Click;
btnReg.Click += this.btnReg_Click;

if (editClients.HasRows)
{
while (editClients.Read())
{
MessageBox.Show("Client Successfully Updated!");
}
}

if (editClients.Read())
{
connect.Close();
}
else
{
connect.Close();
}

}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
}

我不明白的是,这个 MessageBox.Show("Client 成功更新!");尽管更新成功但没有显示。有什么我错过的吗?

编辑我试图计算受更新查询影响的行数,并在数据库中有任何行更改时显示消息框。

最佳答案

也许你应该尝试使用 cmd.ExecuteNonQuery();
并注释掉mysqldatareader相关代码

       int numberOfrows = cmd.ExecuteNonQuery();
if (numberOfrows > 0)
{
//means update is sucessful
MessageBox.Show("Client Successfully Updated!");

}
btnReg.Click -= this.btnUpdt_Click;
btnReg.Click += this.btnReg_Click;

关于c# - 消息框不显示数据库中是否有受影响的行 C# MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31873827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com