2010-07-21 25 views
0

我已经创建了一个方法来插入在MySql服务器上的“ExceptionLog”表中引发的任何异常。SqlParameterCollection只接受非空的SqlParameter类型对象,而不是MySqlParameter对象

ExceptionLog表格式

idExceptionLog INT(AI) 用户(VARCHAR 45) 的ErrorMessage(VARCHART 4000)

问题是我不断收到以下错误。有谁知道为什么?

的SqlParameterCollection仅 接受非空的SqlParameter型 对象,而不是了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; 
     } 

感谢

回答

0

我做了一些细微的更改,现在出现以下错误。

**

输入字符串的不正确的

**

这是我使用所存储的proceedure和方法。

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

- 例行DDL


DELIMITER $$

CREATE DEFINER = Admin @% PROCEDURE test( IN p_idExceptionLog INT(32), IN p_ExceptionDate DATETIME, IN 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      
    ) ; 

END

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

所有内部System.Data.SqlClient类用于SQL Server数据库&不是MySQL的。

在您的代码中,将SqlCommand替换为MySqlCommandSqlConnection以及MySqlConnection

编辑:请参阅this页面的类,你可以在这种情况下使用。

+0

谢谢,我有一个砍伐这将是一些简单的像这样,因为我不习惯使用MySQL。 – Steve 2010-07-21 15:13:25

相关问题