2010-07-21 7 views
0

J'ai créé une méthode qui va insérer toutes les exceptions lancées dans une table sur "ExceptionLog" sur le serveur MySql.SqlParameterCollection n'accepte que les objets de type SqlParameter non-null, pas les objets MySqlParameter

ExceptionLog Table Format

idExceptionLog int (AI) utilisateur (varchar 45) ErrorMessage (varchart 4000)

Le problème est que je continue à obtenir l'erreur suivante. Quelqu'un sait-il pourquoi?

Le SqlParameterCollection ne accepte le type SqlParameter non nul objets, objets non MySqlParameter.

private void showErrorBox(String errorMsg, MessageBoxButtons btnokshow) 
    { 

     MessageBox.Show(errorMsg, "FS Manager Error", MessageBoxButtons.OK); 

     // write to DB 

     string username = System.Environment.UserName.ToString(); 
     string timestamp = DateTime.Now.ToString(); 

      // Locals 
     SqlConnection NasDB = null; 
     SqlCommand inputError = null; 
     int rows = 0; 
     string spName = "ExceptionInsert"; 

     try 
     { 
      //Instantiate the DB connection setting the conn string 
      using (NasDB = new SqlConnection(getConnectionString(ConnectionType.NAS))) 
      { 
       // Instantiate the command object that will fire the SP. 
       using (inputError = new SqlCommand(spName, NasDB)) 
       { 
        // Finish setting up the command object 
        inputError.CommandType = CommandType.StoredProcedure; 

        // Set up the SP params. 


        inputError.Parameters.Add(new MySql.Data.MySqlClient.MySqlParameter("ExceptionDate", MySql.Data.MySqlClient.MySqlDbType.DateTime, (1))); 
        inputError.Parameters[0].Value = timestamp; 

        inputError.Parameters.Add(new MySql.Data.MySqlClient.MySqlParameter("User", MySql.Data.MySqlClient.MySqlDbType.VarChar, (45))); 
        inputError.Parameters[1].Value = System.Environment.UserName.ToString(); 

        inputError.Parameters.Add(new MySql.Data.MySqlClient.MySqlParameter("ExceptionMessage", MySql.Data.MySqlClient.MySqlDbType.VarChar, (4000))); 
        inputError.Parameters[2].Value = errorMsg; 


        // Now that the SP is completely set up and ready to go open the conn and fire the SP. 
        inputError.Connection.Open(); 
        rows = inputError.ExecuteNonQuery(); 

        // Close ASAP 
        inputError.Connection.Close(); 
       } 
      } 

     } 
     catch (Exception ex) 
     { 
      //showErrorBox(ex.ToString()); 
      throw ex; 
     } 

Merci

Répondre

0

J'ai apporté quelques légères modifications et j'obtiens maintenant l'erreur suivante.

**

chaîne d'entrée n'a pas été dans un bon

**

C'est le proceedure stocké i l'utilisation et la méthode.

-- -------------------------------------------------------------------------------- 

- routine LDD


DELIMITER $$

CREATE DEFINER = Admin @% PROCÉDURE test (EN p_idExceptionLog INT (32), EN p_ExceptionDate DATETIME, EN p_User VARCHAR (45), IN p_ExceptionMessage VARCHAR (4000)

) 

BEGIN

INSERT INTO ExceptionLog 
    (
     id     , 
     Date     , 
     User     , 
     Message          
    ) 
VALUES 
    ( 
     p_idExceptionLog     , 
     p_ExceptionDate      , 
     p_User        , 
     p_ExceptionMessage      
    ) ; 

FIN

private void showErrorBox(String errorMsg, MessageBoxButtons btnokshow) 
    { 

     MessageBox.Show(errorMsg, "FS Manager Error", MessageBoxButtons.OK); 

     // write to DB 

     string username = System.Environment.UserName.ToString(); 
     string timestamp = DateTime.Now.ToString(); 

      // Locals 
     MySqlConnection NasDB = null; 
    // MySqlCommand inputError1 = new MySqlCommand(); 
     MySqlCommand inputError = null; 
       int rows = 0; 
     string spName = "test"; 

     try 
     { 
      //Instantiate the DB connection setting the conn string 
      using (NasDB = new MySqlConnection(getConnectionString(ConnectionType.NAS))) 
      { 
       // Instantiate the command object that will fire the SP. 
       using (inputError = new MySqlCommand(spName, NasDB)) 
       { 
        // Finish setting up the command object 
        inputError.CommandType = CommandType.StoredProcedure; 

        // Set up the SP params. 

        inputError.Parameters.Add(new MySql.Data.MySqlClient.MySqlParameter("p_idExceptionLog", MySql.Data.MySqlClient.MySqlDbType.Int32, (1))); 
        inputError.Parameters[0].Value = ""; 

        inputError.Parameters.Add(new MySql.Data.MySqlClient.MySqlParameter("p_ExceptionDate", MySql.Data.MySqlClient.MySqlDbType.DateTime, (1))); 
        inputError.Parameters[1].Value = timestamp; 

        inputError.Parameters.Add(new MySql.Data.MySqlClient.MySqlParameter("p_User", MySql.Data.MySqlClient.MySqlDbType.VarChar, (45))); 
        inputError.Parameters[2].Value = System.Environment.UserName.ToString(); 

        inputError.Parameters.Add(new MySql.Data.MySqlClient.MySqlParameter("p_ExceptionMessage", MySql.Data.MySqlClient.MySqlDbType.VarChar, (4000))); 
        inputError.Parameters[3].Value = errorMsg; 

        // Now that the SP is completely set up and ready to go open the conn and fire the SP. 
        inputError.Connection.Open(); 
        rows = inputError.ExecuteNonQuery(); 

        // Close ASAP 
        inputError.Connection.Close(); 
       } 
      } 

     } 
     catch (Exception ex) 
     { 
      //showErrorBox(ex.ToString()); 
      throw ex; 
     } 
    } 
1

Toutes les classes à l'intérieur System.Data.SqlClient sont utilisés pour la base de données SQL Server & pas mysql.

Dans votre code, remplacez SqlCommand par MySqlCommand et SqlConnection par MySqlConnection.

EDIT: Voir la page this pour les classes, vous pouvez utiliser dans ce cas.

+0

Merci, j'ai eu un abattait ce serait quelque chose de simple comme ça que je ne suis pas habitué à travailler avec MySQL. – Steve