尝试使用来自asp.net页的输入将图像文件上传到数据库时出现错误

时间:2019-06-19 09:16:17

标签: c# mysql asp.net

我正在尝试将图像上传到mysql数据库。我将图像上传到asp.net页面上。 但是,每当我尝试将图像上传到数据库时,都会出现此错误。

System.FormatException: 'Input string was not in a correct format.'

此代码

 MySqlDataReader MyReader2 = MyCommand2.ExecuteReader();

这是我尝试过的完整代码

protected void upload_files(object sender, EventArgs e)
{
            if (FileUpload_UploadFile.HasFile)
            {
                HttpPostedFile img = FileUpload_UploadFile.PostedFile;
                // IMPLEMENT YOUR CODE FOR SAVING THE FILE
                Console.Write(img);
                Stream stream = img.InputStream;
                BinaryReader binaryReader = new BinaryReader(stream);
                bytes = binaryReader.ReadBytes((int)stream.Length);
                result = System.Text.Encoding.UTF8.GetString(bytes);
                myLabel2.Text = "file has been uploaded";



                MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);


                string sql_command = "Insert INTO Recognized_person1 (Name,Image,Department,Department_id) VALUES (@Name, @Image, @Department, @Department_id )";



                conn.Open();
                MySqlCommand MyCommand2 = new MySqlCommand(sql_command, conn);
                MyCommand2.Parameters.AddWithValue("@Name", MySqlDbType.VarChar).Value = Name.Text;
                MyCommand2.Parameters.AddWithValue("@Image", MySqlDbType.Blob).Value = bytes;
                MyCommand2.Parameters.AddWithValue("@Department", MySqlDbType.VarChar).Value = Department.Text ;
                MyCommand2.Parameters.AddWithValue("@Department_id", MySqlDbType.Int32).Value = Int32.Parse((Department_id1.Text));
               //MyCommand2.ExecuteNonQuery();

                MySqlDataReader MyReader2 = MyCommand2.ExecuteReader();
                while (MyReader2.Read())
                {
                }

               conn.Close();
           }
}

关于如何纠正此错误的任何想法?

0 个答案:

没有答案