System.ArgumentException:参数无效。 [英] System.ArgumentException: Parameter is not valid.

查看:276
本文介绍了System.ArgumentException:参数无效。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我在sql server中存储图像。数据类型是Image。



从数据库中检索图像时出现类似System.ArgumentException的错误:参数无效。



此行中的错误img = Image.FromStream(ms)。





Hi am storing images in sql server. The datatype is Image.

while retrieving the Images from the database i getting error like System.ArgumentException: Parameter is not valid.

The error coming in this line "img = Image.FromStream(ms)".


private void RetriveImage_Click(object sender, EventArgs e)
       {
           SqlConnection con = new SqlConnection(constr);
           con.Open();
           Image img;
           try
           {
               cmd = new SqlCommand("select Pic from emguimg where ID =" + cbxID.SelectedItem.ToString() + "", con);
               SqlDataReader dr = cmd.ExecuteReader();
               DataTable dt = new DataTable();
               dt.Load(dr);
               byte[] bytes = (byte[])((byte[])dt.Rows[0]["Pic"]);
               MemoryStream ms = new MemoryStream(bytes);
               ms.Write(bytes, 0, bytes.Length);
               img = Image.FromStream(ms);

               PicBox.Image = img;
               PicBox.SizeMode = PictureBoxSizeMode.StretchImage;
               PicBox.BorderStyle = BorderStyle.Fixed3D;
               ms.Flush();
               ms.Close();
}
           catch(Exception ex)
           {
               WriteLogMessage(ex.ToString());
           }
       }

推荐答案

很可能,它是您存储图像的位置:您的代码容易受到SQL注入的影响,这意味着yoiu总是连接字符串以形成SQL命令。这不仅会让您的用户有机会损坏或破坏您的数据库,还意味着图像存储不起作用。请参阅此处:为什么我会得到一个参数无效。我从数据库中读取图像时出现异常? [ ^ ]



Hello Griff感谢您的回复我收到{byte [21]}



是的......这是什么意思?

Most likely, it's where you stored the image: your code is vulnerable to SQL Injection which implies that yoiu always concatenate strings to form SQL commands. Not only does that give your users the chance to damage or destroy your database, it also means that image storage doesn't work. See here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^]

"Hello Griff thanks for your reply. I receive the {byte[21]}"

Yes...and what does that spell out?
[0] 83    S
[1] 121   y
[2] 115   s
[3] 116   t
[4] 101   e

[5] 109   m
[6] 46    .
[7] 68    D
[8] 114   r
[9] 97    a
[10] 119  w
[11] 105  i
[12] 110  n
[13] 103  g
[14] 46   .





请点击链接,阅读它所说的内容,并注意章节:在我关闭之前......



So follow the link, read what it says, and pay attention to the Section: "Before I close..."


这篇关于System.ArgumentException:参数无效。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆