在ASP.NET中向SQL数据库添加图像时出现问题

本文关键字:图像 问题 添加 数据库 ASP NET 中向 SQL | 更新日期: 2023-09-27 17:50:22

我已经研究了一段时间了。我正试图将图像添加到SQL数据库。我知道这不是最好的办法,但我老板确实希望这样。我将在其中添加图像的表中有一个类型为 image 的图像列。

这是我用来添加的存储过程:

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来做INSERT。ASynchFileUPload位于gridview中的UpdatePanel中的accordion中。

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;

ASPX代码中ASychFileUpload所在的部分:

<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(max)字段代替图像类型,但它不起作用。我能在网上找到的所有信息都告诉我使用该代码,或该代码的变体(其他获得字节数组的方法,我只使用最简单的方法)。

总的来说,为什么UPDATE失败,我如何使它工作。

谢谢

在ASP.NET中向SQL数据库添加图像时出现问题

这篇文章展示了一切:

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

希望,当你的老板看到它有多慢时,你会被允许用正确的方法把图像存储在磁盘上。