MySQL C#查询故障-更新表
我在创建此函数以更新数据库时遇到问题。更新教员似乎可以完美地工作,而更新人员表却不能。我假设MySQL查询不适合更新person表 附加信息:出于测试目的,我的代码现在已经连接到一个GUI模拟。具有@Id..的更新字符串。。这只是选择我想要更改的IDMySQL C#查询故障-更新表,c#,mysql,sql,C#,Mysql,Sql,我在创建此函数以更新数据库时遇到问题。更新教员似乎可以完美地工作,而更新人员表却不能。我假设MySQL查询不适合更新person表 附加信息:出于测试目的,我的代码现在已经连接到一个GUI模拟。具有@Id..的更新字符串。。这只是选择我想要更改的ID public static void Update(string update,string fName, string lName, string DOB, string postCode, string address, string phon
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);
}
}
}
您忘记在第二个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();
还可以使用处置您的MySqlConnection
和MySqlCommand
等
using(MySqlConnection conn = new MySqlConnection(ConnectionString))
using(MySqlCommand cmd = conn.CreateCommand())
{
//
}
您忘记在第二个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();
还可以使用处置您的MySqlConnection
和MySqlCommand
等
using(MySqlConnection conn = new MySqlConnection(ConnectionString))
using(MySqlCommand cmd = conn.CreateCommand())
{
//
}
您能告诉我您遇到了什么错误或该函数的意外行为吗。“Update faculty成员似乎工作得很好”当您的查询有@ID参数但在中您没有在命令对象中添加该参数时,您怎么能这么说。@Allonaz您确定您的第二个查询工作正常吗?因为我不这么认为。:)你能告诉我你得到了什么错误或该函数的意外行为吗。“更新教员似乎工作得很好”你怎么能这么说,虽然你的查询有@ID参数,但你没有在command对象中添加该参数。@Allonaz你确定你的第二个查询可以工作吗?因为我不这么认为
:)