输入字符串的格式不正确

时间:2010-07-21 16:55:00

标签: mysql insert

任何人都可以通过以下方法/存储过程告知我哪里出错了吗?我一直收到以下错误......

**

> Input string was not in a correct

**

这是我使用的存储过程和方法。

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

-- Routine 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;
        }
    }

由于

2 个答案:

答案 0 :(得分:1)

您是否将参数[0](idExceptionLog)声明为Int32,然后将其设置为脚本中的字符串?也许不设置它会有所帮助,如果它是自动生成的,或者如果你手动生成它就给它一个有效的int值。

答案 1 :(得分:-1)

解决了问题。

 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 inputError = null;
               int rows = 0;
        string spName = "ExceptionInsert";


        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, (0)));
                    inputError.Parameters[0].Value = null;

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