Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 参数无效。在c中用sql检索图像_C# - Fatal编程技术网

C# 参数无效。在c中用sql检索图像

C# 参数无效。在c中用sql检索图像,c#,C#,我正在尝试用sql检索图像。我尝试了各种解决办法。什么都不管用 SqlCommand command2 = new SqlCommand(query2, connection); command2.Parameters.Add(new SqlParameter("prodName", lvItem)); SqlDataReader reader; connection.Open(); reader = command2.ExecuteReader(); while (reader.R

我正在尝试用sql检索图像。我尝试了各种解决办法。什么都不管用

SqlCommand command2 = new SqlCommand(query2, connection);
command2.Parameters.Add(new SqlParameter("prodName", lvItem));     

SqlDataReader reader;
connection.Open();
reader = command2.ExecuteReader();
while (reader.Read())
{
    byte[] image = (byte[])reader["productImage"]; // converting image to byte
    if (image == null)
    {
        pbPicture.Image = null;
    }
    else
    {
        MemoryStream memory = new MemoryStream(image);
        pbPicture.Image = `System.Drawing.Image.FromStream(memory);  //Parameter is not valid
    }

}

我尝试了各种解决办法。什么都不管用。请帮忙!我仍在学习

我认为您的参数应该像@prodName一样,并指定一些大小

command2.Parameters.Add(new SqlParameter("prodName", lvItem));  


问题可能来自insert语句。我有一个类似的问题,它将插入,但永远不会检索,告诉我参数无效

插入时尝试使用参数化查询

像这样

scom.CommandText = "insert into Quest ( [TopicT_Id], [Quest], [Image1] ) values (  '" + Guid.NewGuid() + "', '" + quest + "', @image)";
scom.Parameters.AddWithValue("@image", imagebyte);
而不是

scom.CommandText = "insert into Quest ( [TopicT_Id], [Quest], [Image1] ) values (  '" + Guid.NewGuid() + "', '" + quest + "','"+imagebyte+"')";

我意识到,在不使用参数插入图像时,强制在“”之间的图像值都将变为varchar或其他字符串格式。

string query2=从product中选择productImage,其中productName=@prodName;SqlCommand command2=新的SqlCommandquery2,连接;command2.Parameters.Addnew SQLParameterPerpodName,lvItem;这是我的查询..它是否在while循环中出现?pbPicture.Image是什么数据类型?我猜您的图像是原始rgba数据。这就是为什么你会有例外。请参阅页面底部的评论..不,它不起作用。。请帮帮我。我两天来一直在寻找解决方案。我在youtube和stackoverflow上尝试了各种解决方案。不起任何作用。SqlCommand command2=新的SqlCommandquery2,连接;SqlParameter=newSqlParameter@prodName,SqlDbType.VarChar,50;参数值=lvItem;reader=command2.ExecuteReader;//必须声明标量变量。
scom.CommandText = "insert into Quest ( [TopicT_Id], [Quest], [Image1] ) values (  '" + Guid.NewGuid() + "', '" + quest + "','"+imagebyte+"')";