Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 错误:在命令执行期间遇到致命错误_C#_Mysql_Visual Studio 2015 - Fatal编程技术网

C# 错误:在命令执行期间遇到致命错误

C# 错误:在命令执行期间遇到致命错误,c#,mysql,visual-studio-2015,C#,Mysql,Visual Studio 2015,错误:在命令执行期间遇到致命错误 这是我的密码: public static int AddCustomer(Customer customer) { MySqlConnection connection = MySqlCommand.GetConnection(); string insertStatement = "INSERT INTO customer (CUSTOMER_FIRST_NAME, CUSTOMER_LAST

错误:在命令执行期间遇到致命错误 这是我的密码:

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

    }
}
publicstaticintaddcustomer(客户)
{
MySqlConnection=MySqlCommand.GetConnection();
字符串插入语句=
“插入客户(客户名、客户姓、客户城市、客户州、客户街道号码、客户街道、客户电话)”+
“值(@CUSTOMER\u FIRST\u NAME、@CUSTOMER\u LAST\u NAME、@CUSTOMER\u CITY、@CUSTOMER\u STATE、@CUSTOMER\u STREET\u NUMBER、@CUSTOMER\u STREET、@CUSTOMER\u PHONE)”;
MySql.Data.MySqlClient.MySqlCommand插入命令=
新的MySql.Data.MySqlClient.MySqlCommand(insertStatement,connection);
insertCommand.Parameters.AddWithValue(
“@CUSTOMER\u FIRST\u NAME”,CUSTOMER.FirstName);
insertCommand.Parameters.AddWithValue(
“@CUSTOMER\u LAST\u NAME”,CUSTOMER.LastName);
insertCommand.Parameters.AddWithValue(
“@CUSTOMER_STREET_NUMBER”,CUSTOMER.STREET NUMBER);
insertCommand.Parameters.AddWithValue(
“@CUSTOMER\u STREET\u NAME”,CUSTOMER.STREET);
insertCommand.Parameters.AddWithValue(
“@CUSTOMER_CITY”,CUSTOMER.CITY);
insertCommand.Parameters.AddWithValue(
“@CUSTOMER_STATE”,CUSTOMER.STATE);
insertCommand.Parameters.AddWithValue(
“@CUSTOMER_PHONE”,CUSTOMER.PHONE);
尝试
{
connection.Open();
insertCommand.ExecuteOnQuery();
字符串选择语句=
“从客户中选择最大值(客户ID)”;
MySql.Data.MySqlClient.MySqlCommand selectCommand=
新建MySql.Data.MySqlClient.MySqlCommand(selectStatement,connection);
int customerID=Convert.ToInt32(selectCommand.ExecuteScalar());
返回客户ID;
}
捕获(MySqlException-ex)
{
掷骰子;
}
最后
{
connection.Close();
}
}
/// 
/// 
/// 
/// 
/// 
/// 
公共静态bool UpdateCustomer(客户oldCustomer,
客户(新客户)
{
MySqlConnection=MySqlCommand.GetConnection();
字符串更新语句=
“更新客户集”+
CUSTOMER\u FIRST\u NAME=@NewCUSTOMER\u FIRST\u NAME+
CUSTOMER\u LAST\u NAME=@NewCUSTOMER\u LAST\u NAME+
“客户街=@NewCUSTOMER街,”+
“客户街道号码=@V客户街道号码”+
客户城市=@NewCUSTOMER城市+
CUSTOMER\u STATE=@NewCUSTOMER\u STATE+
“客户电话=@NewCUSTOMER电话”+
“哪里”+
CUSTOMER\u FIRST\u NAME=@CUSTOMER\u FIRST\u NAME+
客户姓氏=@CUSTOMER\u LAST\u NAME+
“客户街=@OldCUSTOMER街,”+
“客户街号码=@OldCUSTOMER街号码”+
“和客户城市=@OldCUSTOMER城市”+
“和客户状态=@OldCUSTOMER\u FIRST\u NAME”+
“客户名=@OldCUSTOMER\u FIRST\u NAME;”;
MySql.Data.MySqlClient.MySqlCommand updateCommand=
新建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\u 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\u PHONE”,OldCUSTOMER.PHONE);
尝试
{
connection.Open();
int count=updateCommand.ExecuteOnQuery();
如果(计数>0)
返回true;
其他的
返回false;
}
捕获(MySqlException-ex)
{
掷骰子;
}
最后
{
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");
至:

MySqlConnection connection = MySqlCommand.GetConnection();
MySqlConnection connection = new MySqlConnection("YourConnectionStringHere");

它在更新或插入过程中失败了吗?在添加和更新功能过程中都失败了。您的错误到底是什么?@reds当我尝试在程序中运行添加或更新功能时,出现一个错误,读取“命令执行期间发生致命错误”。您的代码正在插入一条记录,并且在插入数据库后也将获得一条记录?但您只打开了一次连接,而没有关闭第一个连接。执行后是否插入了记录?