c# - 错误: Fatal error encountered during command execution

标签 c# mysql visual-studio-2015

错误:命令执行期间遇到 fatal error 这是我的代码:

 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();
        }

    }
}

}

我哪里出错了?我尝试过其他帖子中推荐的其他更改,但我没有看到任何运气。任何帮助将不胜感激。

最佳答案

尝试先插入,如果成功则执行下一个查询:

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");

关于c# - 错误: Fatal error encountered during command execution,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43728778/

相关文章:

visual-studio - TypeScript build设置 "Compile on Save"在 VS 2015 中不起作用,.js 文件仅在构建期间生成

适用于 Microsoft Windows CE 7.0 的 Visual Studio 2015 中的 Python

c# 使用同一个库的多个版本 (nest)

c# - 如何转换为 simple.data 中的 IEnumerable<Guid>?

c# - InternalEquals(object objA, object objB) 的实现在哪里

Mysql Count 忽略 order by

c++ - 将 SQL 连接到 visual studio 2015 中的 c++ 项目

c# - 无法向电子邮件发送消息

mysql - 为什么这个 strcmp() 语法在 mysql-5.7 中给我一个错误?

MySQL 查询查找给定参数列表中每个值的列中匹配的计数