MySQL C# 查询问题 - 正在更新表

本文关键字:更新 查询 问题 MySQL | 更新日期: 2023-09-27 18:30:49

我在创建更新数据库的函数时遇到问题。更新教职员工似乎工作正常,而人员表的更新则不然。我假设 MySQL 查询不正确,无法更新人员表。

附加信息:我的代码目前已挂接到 GUI 模拟以进行测试。 带有 @Id.. 的更新字符串只是为了选择我想更改的 ID..

public static void Update(string update,string fName, string lName, string DOB, string postCode, string address, string phoneNumber,
                                        bool isTenured, string qualifications, string previousEmployment)
            {
                MySqlConnection conn;
                MySqlCommand cmd;
                string sql = "UPDATE person SET firstName = @FirstName , lastName = @LastName, DOB = @DOB, phoneNumber = @PhoneNumber, address = @Address, postCode = @PostCode WHERE ID =@Id;";
                GetConnection(out conn, out cmd, sql);
                try
                {
                    cmd.Parameters.AddWithValue("@Id", update);
                    cmd.Parameters.AddWithValue("@FirstName", fName);
                    cmd.Parameters.AddWithValue("@LastName", lName);
                    cmd.Parameters.AddWithValue("@DOB", DOB);
                    cmd.Parameters.AddWithValue("@PhoneNumber", phoneNumber);
                    cmd.Parameters.AddWithValue("@Address", address);
                    cmd.Parameters.AddWithValue("@PostCode", postCode);
                    long id = (long)cmd.LastInsertedId;
                    sql = "UPDATE facultymember SET isTenured = @IsTenured, qualifications = @Qualifications, previousEmployment = @PreviousEmployment WHERE Person_personID=@Id";
                    cmd = new MySqlCommand(sql, conn);
                    cmd.Parameters.AddWithValue("@IsTenured", isTenured);
                    cmd.Parameters.AddWithValue("@Qualifications", qualifications);
                    cmd.Parameters.AddWithValue("@PreviousEmployment", previousEmployment);
                    cmd.ExecuteNonQuery();
                }
                catch (NullReferenceException nre)
                {
                    MessageBox.Show(nre.Message);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
                finally
                {
                    try
                    {
                        MessageBox.Show("Updated");
                        cmd.Connection.Close();
                        conn.Close();
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                    }
                }
            }

MySQL C# 查询问题 - 正在更新表

您忘记在第二个 sql 查询中添加@Id参数。

sql = "UPDATE facultymember
       SET isTenured = @IsTenured, qualifications = @Qualifications, previousEmployment = @PreviousEmployment
       WHERE Person_personID=@Id";
                        //   ^^^^
cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@IsTenured", isTenured);
cmd.Parameters.AddWithValue("@Qualifications", qualifications);
cmd.Parameters.AddWithValue("@PreviousEmployment", previousEmployment);
cmd.Parameters.AddWithValue("@Id", YourIdValue);
cmd.ExecuteNonQuery();

也使用using声明来处置您的MySqlConnectionMySqlCommand喜欢;

using(MySqlConnection conn = new MySqlConnection(ConnectionString))
using(MySqlCommand cmd = conn.CreateCommand())
{
  //
}