2017-05-02 16 views
0

エラーが発生しました致命的なエラー:このコマンドの実行 中に発生した致命的なエラーが私のコードです:エラー:コマンドの実行中に

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

追加機能と更新機能の両方で失敗します。 – mace

+1

あなたのエラーは何ですか? –

答えて

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

この実行後にレコードが挿入されますか? –

+0

いいえ、これは動作しませんでした – mace

+0

もう一度致命的なエラーですか? –

関連する問題