如何使用存储过程将图像保存到数据库... [英] how to save image into databse using store proc...

查看:82
本文介绍了如何使用存储过程将图像保存到数据库...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,

我正在尝试使用具有文件上载控件的存储过程m将图像上载到数据库,并在上载按钮上单击上载按钮,但应将图像插入数据库,但是会出现错误..pls帮助,在此先感谢..
收到此错误
过程或函数"upload_image"需要未提供的参数"@Property_Img"."

数据库设计和存储过程:

Sir,

I am trying to upload image into database using store proc m having a fileupload control and upload button on upload button click image should be inserted into database but m getting error..pls help thanks in advance..
getting this error
"Procedure or function ''upload_image'' expects parameter ''@Property_Img'', which was not supplied."

Database design and store proc:

create table tbl_property_img
(
   Image_Id int identity(1,1)primary key,
   Property_Img image null,
)


create procedure dbo.upload_image
   @Property_Img image
as
begin
   insert into tbl_property_img(Property_Img)values(@Property_Img)
end


背后的代码:


Code Behind:

int filelength = FileUpload1.PostedFile.ContentLength;
        byte[] imagebytes = new byte[filelength];
        FileUpload1.PostedFile.InputStream.Read(imagebytes,0,filelength);
        SqlConnection cn = new SqlConnection(str);
        SqlCommand cmdinsimg = new SqlCommand("dbo.upload_image", cn);
        cmdinsimg.CommandType = CommandType.StoredProcedure;
        //cmdinsimg.Parameters.AddWithValue("@Property_Img", FileUpload1.);
        SqlParameter[] prms = new SqlParameter[1];
        prms[0] = new SqlParameter("@Property_Img",SqlDbType.Image);
        prms[0].Value = imagebytes;
        cn.Open();
        cmdinsimg.ExecuteNonQuery();
        cn.Close();

推荐答案

尝试如下更改代码.
Try by changing your code as below.
int filelength = FileUpload1.PostedFile.ContentLength;
            byte[] imagebytes = new byte[filelength];
            FileUpload1.PostedFile.InputStream.Read(imagebytes,0,filelength);
            SqlConnection cn = new SqlConnection(str);
            SqlCommand cmdinsimg = new SqlCommand("dbo.upload_image", cn);
            cmdinsimg.CommandType = CommandType.StoredProcedure;
            //cmdinsimg.Parameters.AddWithValue("@Property_Img", FileUpload1.);
            SqlParameter[] prms = new SqlParameter[1];
            prms[0] = new SqlParameter("@Property_Img",SqlDbType.Image);
            prms[0].Value = imagebytes;
            cmdinsimg.Parameters.Add(prms);
            cn.Open();
            cmdinsimg.ExecuteNonQuery();
            cn.Close();


尽管已经声明了参数,但是在向sqlcommand对象添加参数时却犯了一个错误.
Though you have declared the parameter, you made a mistake in adding parameter to sqlcommand object.

cmdinsimg.Parameters.Add("@Property_Img",SqlDbType.Image);
cmdinsimg.Parameters["@Property_Img"].Value = imagebytes;



希望这会有所帮助.
欢呼



Hope this helps.
cheers


拉吉,

您只是缺少了将参数添加到命令中.

任何解决方案都可以解决您的问题!

您还可以在Command.Parameters集合中使用AddWithValue方法.

问候
Hi Raj,

You are just missing to add the Parameter to the Command.

Any of the solutions solves your problem!

You can also use the AddWithValue method in the Command.Parameters collection.

Regards,


这篇关于如何使用存储过程将图像保存到数据库...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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