问题ASP.NET添加图片到SQL数据库 [英] Problems adding an Image to SQL Database in ASP.NET

查看:188
本文介绍了问题ASP.NET添加图片到SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力了一段时间了。我试图将图像添加到SQL数据库。我知道这是不是最好的方式,但我的老板真的希望这种方式。中,我将添加图像表有和类型的图像列图片

I have been working on that for a while now. I am trying to add an Image to an SQL Database. I know it is not the best way but my boss really wants it that way. The table in which I will add the image has and Image column of type Image.

这是我使用的将其添加到存储过程:

This is the stored procedure I am using to add it:

ALTER PROCEDURE pr_INSRT_Img (@Img image, @name varchar(50))
AS
BEGIN
    UPDATE usr
    SET Img= @Img
    WHERE disp_nm = @name
END

我的应用程序在ASP.NET 4.0的。在它的工作原理是用户选择并用AJAX AsyncFileUpload并在该控件的功能UploadComplete图像,它会调用StoredProcedure的做插入。该ASynchFileUPload是一个手风琴是在一个UpdatePanel是在GridView。

My application is made in ASP.NET 4.0. The it works is that the user chooses and Image with an AJAX AsyncFileUpload and on the UploadComplete function of that control, it calls the StoredProcedure to do the INSERT. The ASynchFileUPload is in an accordion that is in an UpdatePanel that is in a gridview.

UploadComplete:

protected void OnUpdateComplete(object sender, EventArgs e)
        {
            Image ImgUser = new Image();
            AsyncFileUpload asyncSender = new AsyncFileUpload();
            UpdatePanel pnlinfo = new UpdatePanel();
            AsyncFileUpload asyncGv = new AsyncFileUpload();
            Accordion accordion = new Accordion();
            Label lblName = new Label();

            //finding the row of the sender
            asyncSender = (AsyncFileUpload)sender;

            foreach (GridViewRow Row in gvData.Rows)
            {
                accordion = (Accordion)Row.Cells[0].FindControl("Accordion");
                asyncGv = (AsyncFileUpload)accordion.FindControl("AsyncFileUpload");
                if (asyncSender.ClientID == asyncGv.ClientID)
                {
                    ImgUser = (Image)accordion.FindControl("imgUser");
                    ImgUser.ImageUrl = asyncGv.FileName;
                    lblName = (Label)Row.Cells[0].FindControl("lblPnlName");
                    bool IsWorking = InsertImage(asyncGv.FileBytes, lblName.Text);
                    pnlinfo = (UpdatePanel)Row.Cells[0].FindControl("pnlInfo");
                    pnlinfo.Update();
                    break;
                }
            }

InsertImage:

    //pr_INSRT_Img
    SqlCommand cmd = new SqlCommand(ConfigManager.InsertImage);
    cmd.CommandType = CommandType.StoredProcedure;

    //set the parameters
    cmd.Parameters.Add("@Img", SqlDbType.Image).Value = ImgData;
    cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = Name;

    int ifWorks = DBUtils.ExecuteCmdScalar(cmd, ConfigManager.PhonebookSQLConnectionString);

    if (ifWorks != 0)
        return true;
    else
        return false;

在code,其中ASychFileUpload就是 ASPX部分:

<div runat="server" class="divRow" style="text-align:center; width:300px; float:left;">
                                        <asp:Accordion ID="Accordion" runat="server" FadeTransitions="true" FramesPerSecond="40" TransitionDuration="250" 
                                            AutoSize="None" SelectedIndex="-1" RequireOpenedPane="false" SuppressHeaderPostbacks="true" Height="50px" Width="360px">
                                            <Panes>
                                                <asp:AccordionPane ID="AccordionPane" runat="server">
                                                    <Header>
                                                        <asp:Image ID="imgUser" runat="server" ImageAlign="Middle" ImageUrl="~/silhouette.jpg"
                                                            Width="100px" Height="100px" EnableViewState="true"/>
                                                    </Header>
                                                    <Content>
                                                        <asp:AsyncFileUpload ID="AsyncFileUpload" runat="server" OnUploadedComplete="OnUpdateComplete" />
                                                    </Content>
                                                </asp:AccordionPane>
                                            </Panes>
                                        </asp:Accordion>
                                        <asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Visible="false"  />
                                    </div><br /><br />

我的问题是,当我上传图片,在的ExecuteScalar()返回0,这意味着影像不是在表插入。我试过把一个的varbinary(最大)字段,而不是图像类型,但没有奏效。所有我能找到在网络上告诉我,使用code或变种,code的(其他方式获得的字节数组,我只是把最简单的)。

My problem is that when I upload the image, the ExecuteScalar() returns a 0 which means that the Image was not inserted in the Table. I've tried putting a varbinary(max) Field instead of an Image type but it did not work. All I can find on the Net tells me to use that code, or variant of that code (other ways to get the byte array, I just took the simplest).

总之,为什么是更新失败,我怎样才能使它发挥作用。

Overall, why is the UPDATE failing and how can I make it work.

感谢

推荐答案

本文说明一切:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=129

希望,当你得到它的工作和你的老板认为这是多么慢,你会被允许做的正确方法,并存储在磁盘上的形象。

Hopefully, when you get it to work and your boss sees how slow it is, you'll be allowed to do it the right way and store the image on disk.

这篇关于问题ASP.NET添加图片到SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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