您好,欢迎访问一九零五行业门户网

上传图片到SQLServer

代码下载 : csaspnetimageeditupload.zip 此示例演示了在 asp.net formview 中如何上传和显示图片,如何添加,编辑,删除和分页。 formview 默认显示了从数据库中取出的数据以及基本操作的界面 图片是从 sql server 数据库中取出,并显示在网页上面。 实现步
代码下载: csaspnetimageeditupload.zip
此示例演示了在asp.net formview中如何上传和显示图片,如何添加,编辑,删除和分页。
formview默认显示了从数据库中取出的数据以及基本操作的界面
图片是从sql server 数据库中取出,并显示在网页上面。
实现步骤:
步骤1:在visual studio 中创建web应用程序项目 csaspnetformviewupload。
步骤2:拖拽formview控件到default.aspx页面。
(1) 重命名formview为fvperson。
itemtemplate:重定义formview的记录显示。
edititemtemplate:自定义编辑视图
insertitemtemplate:自定义插入视图
步骤3:从示例代码复制粘贴下面的方法到我们的项目文件default.aspx.cs
page_load方法:当页面加载的时候初始化对象。
bindformview方法:绑定formview控件和sql server中的表。
步骤4:打开fvperson的属性面板,切换到事件。双击下列事件生成事件处理程序。然后,按照示例所示代码完成事件处理程序。
(1) modechanging 事件:当formview在edit,insert和readonly模式之间切换时,重新绑定formview控件,以便在新的模式下显示数据。
////////////////////////////////////////////////////////////////
    fvperson.changemode(e.newmode);
    bindformview();
    ////////////////////////////////////////////////////////////////
(2) pageindexchanging事件:当单击翻页按钮的时候引发此事件。为了在新的页面显示的数据,我们需要设置新的一页的索引,然后重新绑定formview控件。
////////////////////////////////////////////////////////////////   
    fvperson.pageindex = e.newpageindex;
    bindformview();
    ////////////////////////////////////////////////////////////////
(3) iteminserting 事件:当插入按钮被单击是引发,但是处理的是插入数据之前的操作。当单击按钮后,我们需要从insertitemtemplate的formview控件中获取first name,last name和指定图片文件(bytes)。
////////////////////////////////////////////////////////////////
    string strlastname = ((textbox)fvperson.row.findcontrol(tblastname)).text;
    string strfirstname = ((textbox)fvperson.row.findcontrol(tbfirstname)).text;
cmd.parameters.add(@lastname, sqldbtype.nvarchar, 50).value = strlastname;
    cmd.parameters.add(@firstname, sqldbtype.nvarchar, 50).value = strfirstname;
fileupload uploadpicture = (fileupload)fvperson.findcontrol(uploadpicture);
if (uploadpicture.hasfile)
    {
        cmd.parameters.add(@picture, sqldbtype.varbinary).value = uploadpicture.filebytes;
    }
    else
    {
        cmd.parameters.add(@picture, sqldbtype.varbinary).value = dbnull.value;
    }
    ////////////////////////////////////////////////////////////////
(4) itemupdating 事件:当单击更新按钮的时候引发的事件。但是处理的是在更新数据之前的操作。当单击更新按钮后,我们需要从edititemtemplate中的formview控件获取和传递person id,first name, last name和指定的图像文件(bytes)。
 ////////////////////////////////////////////////////////////////
    string strpersonid = ((label)fvperson.row.findcontrol(lblpersonid)).text;   
    ////////////////////////////////////////////////////////////////
步骤5:创建一个新的asp.net handle处理程序imagehandler.ashx,用来处理从数据库中获取图片文件并输出到网页。
///
    /// connected to the database,
    /// if the specific record has image,read out the specific row's
    /// image byte collection as well as image type, output it.
    /// if not, output the default image instead.
    ///
    public class imagehandler : ihttphandler
    {
        public void processrequest(httpcontext context)
        {
            using (sqlcommand cmd = new sqlcommand())
            {
                cmd.connection = new sqlconnection(
                    configurationmanager.connectionstrings[db_personsconnectionstring].connectionstring);
                cmd.connection.open();
                cmd.commandtext = select personimage,personimagetype from tb_personinfo +
                    where id= + context.request.querystring[id];
sqldatareader reader = cmd.executereader(
                    commandbehavior.closeconnection | commandbehavior.singlerow);
if (reader.read())
                {
                    byte[] imgbytes = null;
                    string imgtype = null;
if (reader.getvalue(0) != dbnull.value)
                    {
                        imgbytes = (byte[])reader.getvalue(0);
                        imgtype = reader.getstring(1);
// if bmp, convert to jpg and show because of the different formation type.
                        if (imgtype.equals(image/bmp, stringcomparison.ordinalignorecase))
                        {
                            using (memorystream ms = new memorystream(imgbytes))
                            {
                                using (bitmap bm = new bitmap(image.fromstream(ms)))
                                {
                                    bm.save(context.response.outputstream, imageformat.jpeg);
                                }
                            }
                        }
                        else
                        {
                            context.response.contenttype = imgtype;
                            context.response.binarywrite(imgbytes);
                        }
                    }
                    else
                    {
                        imgbytes = file.readallbytes(context.server.mappath
                            (~/defaultimage/defaultimage.jpg));
                        imgtype = image/pjpeg;
                        context.response.contenttype = imgtype;
                        context.response.binarywrite(imgbytes);
                    }
                }
                reader.close();
                context.response.end();
            }
        }
public bool isreusable
        {
            get
            {
                return false;
            }
        }
    }
其它类似信息

推荐信息