尝试通过C#将字符串插入MySQL时截断字符串

时间:2016-01-28 06:57:52

标签: c# mysql stored-procedures insert

我在MySql中编写了存储过程,并尝试使用C#和MySql.Data.MySqlClient调用它来将数据插入数据库,但字符串被截断。

我在C#中使用的代码是:

    public long InsertDataToMain(int aboutType, string pageTitle, string pageText, ImageProperty imgProp)
    {
        long ArticleNumber = 0;
        dt = DateTime.Now;
        try
        {
            var myConnection =
                new MySqlConnection("server=XXXXXXXXXXXXXXX;uid=XXXXXXXX;port=3306;pwd=XXXXXXXX;database=wikitest;charset=utf8");

            MySqlCommand myCommand = new MySqlCommand("Routine_Transfer_Wiki_Data")
            {
                Connection = myConnection,
                CommandType = CommandType.StoredProcedure
            };

            MySqlParameter paramPageType = new MySqlParameter()
            {
                ParameterName = "param_page_type_id",
                MySqlDbType = MySqlDbType.Int16,
                Direction = ParameterDirection.Input,
                Value = aboutType
            };

            MySqlParameter paramPageTitle = new MySqlParameter()
            {
                ParameterName = "param_page_title",
                MySqlDbType = MySqlDbType.VarChar,
                Size = 255,
                Direction = ParameterDirection.Input,
                Value = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(pageTitle.Replace(" ","_")))
            };
            MySqlParameter paramPageLen = new MySqlParameter()
            {
                ParameterName = "param_page_len",
                MySqlDbType = MySqlDbType.Int64,
                Direction = ParameterDirection.Input,
                Value = Encoding.UTF8.GetBytes(pageText).Length
            };

            MySqlParameter paramPageText = new MySqlParameter()
            {
                ParameterName = "param_old_text",
                MySqlDbType = MySqlDbType.MediumBlob,
                //Size = 16777215,
                Direction = ParameterDirection.Input,
                Value = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(pageText))
            };
            myCommand.Parameters.AddRange(new[]
            {
                paramPageType, 
                paramPageTitle, 
                paramPageLen, 
                paramPageText,
            });

            try
            {
                myConnection.Open();
                myCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                myConnection.Close();
            }

       }

因此,当执行该方法时,paramPageText内容将被截断。

0 个答案:

没有答案