简体   繁体   English

'参数无效。' 尝试从数据库 MySql 检索图像时

[英]'Parameter is not valid.' when trying to retrive image from database MySql

I'm working on winForms a project where the user will be able to draw a signature with his cursor and save it in a database.我正在开发 winForms 一个项目,用户可以在其中使用光标绘制签名并将其保存在数据库中。

This is how I'm saving it:这是我保存它的方式:

ExecuteQuery("INSERT INTO signatures (name, signature) VALUES ('"+ TXTname.Text +"','"+ signature +"')");

(this is not giving me any error) (这没有给我任何错误)

keep in mind signature column type is blob.请记住,签名列类型是 blob。

Then, I want the user to retrive his signature from the database to a picturebox, this is how I'm trying to do it:然后,我希望用户将他的签名从数据库中检索到一个图片框,这就是我尝试这样做的方式:

 DBconnection.Open();

                Image signature;

                string query = "SELECT * FROM signatures WHERE name = '" + CMBnames.Text + "'";

                var queryCmd = new MySqlCommand(query, DBconnection);

                reader = queryCmd.ExecuteReader();

                while (reader.Read())
                {
                    byte[] img = (byte[])(reader["SIGNATURE"]);
                    if (img == null)
                    {
                        CMBnames.Text = "error";
                    }
                    else
                    {
                        MemoryStream ms = new MemoryStream(img);

                        pictureBox2.Image = System.Drawing.Image.FromStream(ms);
                    }
                }

DBconnection.Close();

But I get this error 'Parameter is not valid.'但我收到此错误“参数无效”。 here:这里:

pictureBox2.Image = System.Drawing.Image.FromStream(ms);

What am I doing wrong?我究竟做错了什么?

If signature is a byte[] then '"+ signature +"' will generate the literal string 'System.Byte[]' .如果signaturebyte[]'"+ signature +"'将生成文字字符串'System.Byte[]' This is not a valid image, and reading it back will produce the error you're seeing.这不是一个有效的图像,读回它会产生你看到的错误。

You need to use command parameters, not string concatenation:您需要使用命令参数,而不是字符串连接:

using var cmd = new MySqlCommand(@"INSERT INTO signatures (name, signature)
    VALUES (@name, @signature);", DBconnection);
cmd.Parameters.AddWithValue("@name", TXTname.Text);
cmd.Parameters.AddWithValue("@signature", signature);
cmd.ExecuteNonQuery();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给出“System.ArgumentException:&#39;参数无效。&#39; &quot; 当我从数据库中检索图像时 - Gives "System.ArgumentException: 'Parameter is not valid.' " when i retrieving image from database 从MySQL数据库将图像另存为BLOB时参数无效 - Parameter not valid when getting Image saved as BLOB from MySQL Database 名单 <image> 异常“参数无效”。 - List<image> exception “Parameter is not valid.” 使用保存位图时,“参数无效。” - “Parameter is not valid.” when using saving bitmap 更新 Access 数据库后检索图像时出现问题(System.ArgumentException:'参数无效。') - Problem retrieving the image after updating the Access database (System.ArgumentException: 'Parameter is not valid.') 当我尝试从 mysql 数据库加载图像时,我收到错误“参数无效”,当我尝试从数据库加载图像时 - When I try to load image from mysql database i get error “Parameter is not valid” and when i try to load an image from db 尝试保存从字节转换的图像时出现“参数无效”错误 - “Parameter not valid” error when trying to save the image converted from byte “参数无效。” - 'Parameter is not valid.' 我收到错误:从oledb数据库检索图像时参数无效 - i am getting error : Invalid Parameter when i retrive image from oledb database System.ArgumentException:“参数无效。” 在 C# 中从 SQL Server 获取图像 - System.ArgumentException: 'Parameter is not valid.' in C# get image from SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM