如何从我的网站更新现有图像? [英] how to update a existing image from my website?
本文介绍了如何从我的网站更新现有图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
海
我想从我的网站更新数据库图像.在我的网页中,我具有文件上传,下拉列表和一个按钮字段.这是我的代码.
受保护的 无效 Button1_Click(对象发件人,EventArgs e)
{
如果(FileUpload1.PostedFile!= 空&& FileUpload1.PostedFile.FileName!= " )
{
字节 [] imagesize = 新 字节 [ FileUpload1.PostedFile.ContentLength];
HttpPostedFile UploadedImage = FileUpload1.PostedFile;
uploadImage.InputStream.Read(imagesize, 0 ,( int )FileUpload1.PostedFile.ContentLength);
SqlConnection con = 新 SqlConnection(ConfigurationManager.ConnectionStrings [" ].ConnectionString);
con.Open();
int i = Convert.ToInt32(DropDownList1.SelectedItem.Text);
SqlCommand cmd = 新 SqlCommand(" + i,con);
SqlParameter param = 新 SqlParameter(" ,SqlDbType.Image,imagesize.Length);
cmd.Parameters.Add(uploadedImage);
cmd.ExecuteNonQuery();
MessageBox.Show(" );
}
}
实际上我不知道参数...而我的错误是
SqlParameterCollection仅接受非null的SqlParameter类型对象,而不接受HttpPostedFile对象.
请帮助我
解决方案
您不能将HttpPostadFile作为参数值发送到数据库.您将必须先将其转换为字节数组.
本文介绍了您需要了解的所有内容:
C#从数据库保存和加载图像 [ ^ ]
您的传递参数不正确.您创建了param参数,但是将uploadedImage添加到了命令cmd.
代替这个.
SqlParameter param = 新 SqlParameter(" ,SqlDbType.Image,imagesize.Length); cmd.Parameters.Add(uploadedImage);
试试,
SqlParameter param = 新 SqlParameter(" ,SqlDbType.Image,imagesize); cmd.Parameters.Add(param);
Hai
I want to update a database image from my website. in my web page i have file upload, dropdownlist,one button field. this is my code..
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{
byte[] imagesize = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile uploadedImage = FileUpload1.PostedFile;
uploadedImage.InputStream.Read(imagesize, 0, (int)FileUpload1.PostedFile.ContentLength);
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["wavoo"].ConnectionString);
con.Open();
int i =Convert.ToInt32(DropDownList1.SelectedItem.Text);
SqlCommand cmd = new SqlCommand("update picture set image=@image where ID=" + i, con);
SqlParameter param = new SqlParameter("@Image", SqlDbType.Image, imagesize.Length);
cmd.Parameters.Add(uploadedImage);
cmd.ExecuteNonQuery();
MessageBox.Show("sdfdf");
}
}
actually i don''t know about parameters... and my error is
The SqlParameterCollection only accepts non-null SqlParameter type objects, not HttpPostedFile objects.
Please help me
解决方案
You can''t send HttpPostadFile as a parameter value to your database. You will have to convert it to a bytearray first.
This article explains everything you''ll need to know:
C# Save and Load Image from Database[^]
Your passing parameter is incorrect. You created param parameter but adding uploadedImage to the command cmd.
Instead of this.
SqlParameter param = new SqlParameter("@Image", SqlDbType.Image, imagesize.Length); cmd.Parameters.Add(uploadedImage);
try,
SqlParameter param = new SqlParameter("@Image", SqlDbType.Image, imagesize); cmd.Parameters.Add(param );
这篇关于如何从我的网站更新现有图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文