错误:命令执行期间遇到致命错误 这是我的代码:
public static int AddCustomer(Customer customer)
{
MySqlConnection connection = MySqlCommand.GetConnection();
string insertStatement =
"INSERT INTO customer (CUSTOMER_FIRST_NAME, CUSTOMER_LAST_NAME, CUSTOMER_CITY, CUSTOMER_STATE, CUSTOMER_STREET_NUMBER, CUSTOMER_STREET, CUSTOMER_PHONE) " +
"VALUES (@CUSTOMER_FIRST_NAME, @CUSTOMER_LAST_NAME, @CUSTOMER_CITY, @CUSTOMER_STATE, @CUSTOMER_STREET_NUMBER, @CUSTOMER_STREET, @CUSTOMER_PHONE)";
MySql.Data.MySqlClient.MySqlCommand insertCommand =
new MySql.Data.MySqlClient.MySqlCommand(insertStatement, connection);
insertCommand.Parameters.AddWithValue(
"@CUSTOMER_FIRST_NAME", customer.FirstName);
insertCommand.Parameters.AddWithValue(
"@CUSTOMER_LAST_NAME", customer.LastName);
insertCommand.Parameters.AddWithValue(
"@CUSTOMER_STREET_NUMBER", customer.StreetNumber);
insertCommand.Parameters.AddWithValue(
"@CUSTOMER_STREET_NAME", customer.Street);
insertCommand.Parameters.AddWithValue(
"@CUSTOMER_CITY", customer.City);
insertCommand.Parameters.AddWithValue(
"@CUSTOMER_STATE", customer.State);
insertCommand.Parameters.AddWithValue(
"@CUSTOMER_PHONE", customer.Phone);
try
{
connection.Open();
insertCommand.ExecuteNonQuery();
string selectStatement =
"SELECT MAX(CUST_ID) FROM customer";
MySql.Data.MySqlClient.MySqlCommand selectCommand =
new MySql.Data.MySqlClient.MySqlCommand(selectStatement, connection);
int customerID = Convert.ToInt32(selectCommand.ExecuteScalar());
return customerID;
}
catch (MySqlException ex)
{
throw ex;
}
finally
{
connection.Close();
}
}
/// <summary>
///
/// </summary>
/// <param name="oldCustomer"></param>
/// <param name="newCustomer"></param>
/// <returns></returns>
public static bool UpdateCustomer(Customer oldCustomer,
Customer newCustomer)
{
MySqlConnection connection = MySqlCommand.GetConnection();
string updateStatement =
"UPDATE customer SET " +
"CUSTOMER_FIRST_NAME = @NewCUSTOMER_FIRST_NAME, " +
"CUSTOMER_LAST_NAME = @NewCUSTOMER_LAST_NAME, " +
"CUSTOMER_STREET = @NewCUSTOMER_STREET, " +
"CUSTOMER_STREET_NUMBER = @vCUSTOMER_STREET_NUMBER" +
"CUSTOMER_CITY = @NewCUSTOMER_CITY, " +
"CUSTOMER_STATE = @NewCUSTOMER_STATE, " +
"CUSTOMER_PHONE = @NewCUSTOMER_PHONE " +
"WHERE " +
"CUSTOMER_FIRST_NAME = @CUSTOMER_FIRST_NAME, " +
"CUSTOMER_LAST_NAME = @CUSTOMER_LAST_NAME, " +
"CUSTOMER_STREET = @OldCUSTOMER_STREET, " +
"CUSTOMER_STREET_NUMBER = @OldCUSTOMER_STREET_NUMBER" +
"AND CUSTOMER_CITY = @OldCUSTOMER_CITY " +
"AND CUSTOMER_STATE = @OldCUSTOMER_FIRST_NAME " +
"AND CUSTOMER_FIRST_NAME = @OldCUSTOMER_FIRST_NAME;";
MySql.Data.MySqlClient.MySqlCommand updateCommand =
new MySql.Data.MySqlClient.MySqlCommand(updateStatement, connection);
updateCommand.Parameters.AddWithValue(
"@NewFirstName", newCustomer.FirstName);
updateCommand.Parameters.AddWithValue(
"@NewLastName", newCustomer.LastName);
updateCommand.Parameters.AddWithValue(
"@NewStreet", newCustomer.Street);
updateCommand.Parameters.AddWithValue(
"@NewStreetNumber", newCustomer.StreetNumber);
updateCommand.Parameters.AddWithValue(
"@CUSTOMER_CITY", newCustomer.City);
updateCommand.Parameters.AddWithValue(
"@CUSTOMER_STATE", newCustomer.State);
updateCommand.Parameters.AddWithValue(
"@NEW_PHONE", newCustomer.Phone);
updateCommand.Parameters.AddWithValue(
"@OldFirstName", oldCustomer.FirstName);
updateCommand.Parameters.AddWithValue(
"@OldLastName", oldCustomer.LastName);
updateCommand.Parameters.AddWithValue(
"@OldStreet", oldCustomer.Street);
updateCommand.Parameters.AddWithValue(
"@OldStreetNumber", oldCustomer.StreetNumber);
updateCommand.Parameters.AddWithValue(
"@OldCity", oldCustomer.City);
updateCommand.Parameters.AddWithValue(
"@OldState", oldCustomer.State);
updateCommand.Parameters.AddWithValue(
"@OldCUSTOMER_PHONE", oldCustomer.Phone);
try
{
connection.Open();
int count = updateCommand.ExecuteNonQuery();
if (count > 0)
return true;
else
return false;
}
catch (MySqlException ex)
{
throw ex;
}
finally
{
connection.Close();
}
}
}
}
我哪里错了?我尝试过其他帖子推荐的其他更改,我没有运气。任何帮助将不胜感激。
答案 0 :(得分:0)
首先尝试插入,如果成功则按照下一个查询:
public static int AddCustomer(Customer customer)
{
MySqlConnection connection = MySqlCommand.GetConnection();
connection.Open();
string insertStatement =
"INSERT INTO customer (CUSTOMER_FIRST_NAME, CUSTOMER_LAST_NAME, CUSTOMER_CITY, CUSTOMER_STATE, CUSTOMER_STREET_NUMBER, CUSTOMER_STREET, CUSTOMER_PHONE) " +
"VALUES (@CUSTOMER_FIRST_NAME, @CUSTOMER_LAST_NAME, @CUSTOMER_CITY, @CUSTOMER_STATE, @CUSTOMER_STREET_NUMBER, @CUSTOMER_STREET, @CUSTOMER_PHONE)";
MySql.Data.MySqlClient.MySqlCommand insertCommand =
new MySql.Data.MySqlClient.MySqlCommand(insertStatement, connection);
insertCommand.Parameters.AddWithValue(
"@CUSTOMER_FIRST_NAME", customer.FirstName);
insertCommand.Parameters.AddWithValue(
"@CUSTOMER_LAST_NAME", customer.LastName);
insertCommand.Parameters.AddWithValue(
"@CUSTOMER_STREET_NUMBER", customer.StreetNumber);
insertCommand.Parameters.AddWithValue(
"@CUSTOMER_STREET_NAME", customer.Street);
insertCommand.Parameters.AddWithValue(
"@CUSTOMER_CITY", customer.City);
insertCommand.Parameters.AddWithValue(
"@CUSTOMER_STATE", customer.State);
insertCommand.Parameters.AddWithValue(
"@CUSTOMER_PHONE", customer.Phone);
insertCommand.ExecuteNonQuery();
connection.Close();
替换:
发件人:强>
MySqlConnection connection = MySqlCommand.GetConnection();
要强>
MySqlConnection connection = new MySqlConnection("YourConnectionStringHere");