0

MySqlサーバーの「ExceptionLog」のテーブルにスローされた例外を挿入するメソッドを作成しました。

ExceptionLog テーブル形式

idExceptionLog int (AI) ユーザー (varchar 45) ErrorMessage (varchart 4000)

問題は、次のエラーが発生し続けることです。誰かが理由を知っていますか?

SqlParameterCollection は、MySqlParameter オブジェクトではなく、null 以外の SqlParameter タイプ オブジェクトのみを受け入れます。

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

2 に答える 2

1

内部のすべてのクラスSystem.Data.SqlClientは、mysql ではなく SQL Server データベースに使用されます。

コードで、SqlCommandMySqlCommandに置き換えます。SqlConnectionMySqlConnection

編集:この場合に使用できるクラスについては、このページを参照してください。

于 2010-07-21T12:32:50.433 に答える
0

少し変更を加えたところ、次のエラーが発生しています。

**

入力文字列が正しくありません

**

これは、私が使用しているストアド プロシージャとメソッドです。

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

-- ルーチン DDL


区切り記号 $$

CREATE DEFINER= Admin@ %PROCEDURE test( IN p_idExceptionLog INT(32) 、 IN p_ExceptionDate DATETIME 、 IN p_User VARCHAR(45) 、 IN p_ExceptionMessage VARCHAR(4000)

 )

始める

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

終わり

  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;
        }
    }
于 2010-07-21T15:16:42.880 に答える