如何在formview中将图像插入数据库?

发布于 2024-11-18 13:50:32 字数 11978 浏览 3 评论 0原文

我想将图像插入数据库,但我不知道如何在 formview 中执行此操作,

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Default5.aspx.vb" Inherits="Default5" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:FormView ID="FormView1" runat="server" AllowPaging="True" BackColor="#DEBA84"
        BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2"
        DataKeyNames="code" DataSourceID="SqlDataSource1" GridLines="Both" Width="324px">
        <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
        <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
        <EditItemTemplate>
            code:
            <asp:Label ID="codeLabel1" runat="server" Text='<%# Eval("code") %>'></asp:Label><br />
            name:
            <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>'>
            </asp:TextBox><br />
            nationality:
            <asp:TextBox ID="nationalityTextBox" runat="server" Text='<%# Bind("nationality") %>'>
            </asp:TextBox><br />
            sex:
            <asp:TextBox ID="sexTextBox" runat="server" Text='<%# Bind("sex") %>'>
            </asp:TextBox><br />
            status:
            <asp:TextBox ID="statusTextBox" runat="server" Text='<%# Bind("status") %>'>
            </asp:TextBox><br />
            degree:
            <asp:TextBox ID="degreeTextBox" runat="server" Text='<%# Bind("degree") %>'>
            </asp:TextBox><br />
            job:
            <asp:TextBox ID="jobTextBox" runat="server" Text='<%# Bind("job") %>'>
            </asp:TextBox><br />
            address:
            <asp:TextBox ID="addressTextBox" runat="server" Text='<%# Bind("address") %>'>
            </asp:TextBox><br />
            phone:
            <asp:TextBox ID="phoneTextBox" runat="server" Text='<%# Bind("phone") %>'>
            </asp:TextBox><br />
            total_salary:
            <asp:TextBox ID="total_salaryTextBox" runat="server" Text='<%# Bind("total_salary") %>'>
            </asp:TextBox><br />
            appoint_date:
            <asp:TextBox ID="appoint_dateTextBox" runat="server" Text='<%# Bind("appoint_date") %>'>
            </asp:TextBox><br />
            disconnrct:
            <asp:TextBox ID="disconnrctTextBox" runat="server" Text='<%# Bind("disconnrct") %>'>
            </asp:TextBox><br />
            personal_image:
            <asp:TextBox ID="personal_imageTextBox" runat="server" Text='<%# Bind("personal_image") %>'>
            </asp:TextBox><br />
            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                Text="Update">
            </asp:LinkButton>
            <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                Text="Cancel">
            </asp:LinkButton>
        </EditItemTemplate>
        <InsertItemTemplate>
            code:
            <asp:TextBox ID="codeTextBox" runat="server" Text='<%# Bind("code") %>'>
            </asp:TextBox><br />
            name:
            <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>'>
            </asp:TextBox><br />
            nationality:
            <asp:TextBox ID="nationalityTextBox" runat="server" Text='<%# Bind("nationality") %>'>
            </asp:TextBox><br />
            sex:
            <asp:TextBox ID="sexTextBox" runat="server" Text='<%# Bind("sex") %>'>
            </asp:TextBox><br />
            status:
            <asp:TextBox ID="statusTextBox" runat="server" Text='<%# Bind("status") %>'>
            </asp:TextBox><br />
            degree:
            <asp:TextBox ID="degreeTextBox" runat="server" Text='<%# Bind("degree") %>'>
            </asp:TextBox><br />
            job:
            <asp:TextBox ID="jobTextBox" runat="server" Text='<%# Bind("job") %>'>
            </asp:TextBox><br />
            address:
            <asp:TextBox ID="addressTextBox" runat="server" Text='<%# Bind("address") %>'>
            </asp:TextBox><br />
            phone:
            <asp:TextBox ID="phoneTextBox" runat="server" Text='<%# Bind("phone") %>'>
            </asp:TextBox><br />
            total_salary:
            <asp:TextBox ID="total_salaryTextBox" runat="server" Text='<%# Bind("total_salary") %>'>
            </asp:TextBox><br />
            appoint_date:
            <asp:TextBox ID="appoint_dateTextBox" runat="server" Text='<%# Bind("appoint_date") %>'>
            </asp:TextBox><br />
            disconnrct:
            <asp:TextBox ID="disconnrctTextBox" runat="server" Text='<%# Bind("disconnrct") %>'>
            </asp:TextBox><br />
            personal_image:
            <asp:TextBox ID="personal_imageTextBox" runat="server" Text='<%# Bind("personal_image") %>'>
            </asp:TextBox><br />
            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                Text="Insert">
            </asp:LinkButton>
            <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                Text="Cancel">
            </asp:LinkButton>
        </InsertItemTemplate>
        <ItemTemplate>
            code:
            <asp:Label ID="codeLabel" runat="server" Text='<%# Eval("code") %>'></asp:Label><br />
            name:
            <asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name") %>'></asp:Label><br />
            nationality:
            <asp:Label ID="nationalityLabel" runat="server" Text='<%# Bind("nationality") %>'></asp:Label><br />
            sex:
            <asp:Label ID="sexLabel" runat="server" Text='<%# Bind("sex") %>'></asp:Label><br />
            status:
            <asp:Label ID="statusLabel" runat="server" Text='<%# Bind("status") %>'></asp:Label><br />
            degree:
            <asp:Label ID="degreeLabel" runat="server" Text='<%# Bind("degree") %>'></asp:Label><br />
            job:
            <asp:Label ID="jobLabel" runat="server" Text='<%# Bind("job") %>'></asp:Label><br />
            address:
            <asp:Label ID="addressLabel" runat="server" Text='<%# Bind("address") %>'></asp:Label><br />
            phone:
            <asp:Label ID="phoneLabel" runat="server" Text='<%# Bind("phone") %>'></asp:Label><br />
            total_salary:
            <asp:Label ID="total_salaryLabel" runat="server" Text='<%# Bind("total_salary") %>'></asp:Label><br />
            appoint_date:
            <asp:Label ID="appoint_dateLabel" runat="server" Text='<%# Bind("appoint_date") %>'></asp:Label><br />
            disconnrct:
            <asp:Label ID="disconnrctLabel" runat="server" Text='<%# Bind("disconnrct") %>'></asp:Label><br />
            personal_image:
            <asp:Label ID="personal_imageLabel" runat="server" Text='<%# Bind("personal_image") %>'></asp:Label><br />
            <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
                Text="Edit"></asp:LinkButton>
            <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                Text="Delete"></asp:LinkButton>
            <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
                Text="New"></asp:LinkButton>
        </ItemTemplate>
        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
    </asp:FormView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:employeedataConnectionString %>"
        DeleteCommand="DELETE FROM [emp] WHERE [code] = @code" InsertCommand="INSERT INTO [emp] ([code], [name], [nationality], [sex], [status], [degree], [job], [address], [phone], [total_salary], [appoint_date], [disconnrct], [personal_image]) VALUES (@code, @name, @nationality, @sex, @status, @degree, @job, @address, @phone, @total_salary, @appoint_date, @disconnrct, @personal_image)"
        SelectCommand="SELECT [code], [name], [nationality], [sex], [status], [degree], [job], [address], [phone], [total_salary], [appoint_date], [disconnrct], [personal_image] FROM [emp]"
        UpdateCommand="UPDATE [emp] SET [name] = @name, [nationality] = @nationality, [sex] = @sex, [status] = @status, [degree] = @degree, [job] = @job, [address] = @address, [phone] = @phone, [total_salary] = @total_salary, [appoint_date] = @appoint_date, [disconnrct] = @disconnrct, [personal_image] = @personal_image WHERE [code] = @code">
        <DeleteParameters>
            <asp:Parameter Name="code" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="nationality" Type="String" />
            <asp:Parameter Name="sex" Type="String" />
            <asp:Parameter Name="status" Type="String" />
            <asp:Parameter Name="degree" Type="String" />
            <asp:Parameter Name="job" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="phone" Type="String" />
            <asp:Parameter Name="total_salary" Type="Decimal" />
            <asp:Parameter Name="appoint_date" Type="String" />
            <asp:Parameter Name="disconnrct" Type="String" />
            <asp:Parameter Name="personal_image" Type="String" />
            <asp:Parameter Name="code" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="code" Type="Int32" />
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="nationality" Type="String" />
            <asp:Parameter Name="sex" Type="String" />
            <asp:Parameter Name="status" Type="String" />
            <asp:Parameter Name="degree" Type="String" />
            <asp:Parameter Name="job" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="phone" Type="String" />
            <asp:Parameter Name="total_salary" Type="Decimal" />
            <asp:Parameter Name="appoint_date" Type="String" />
            <asp:Parameter Name="disconnrct" Type="String" />
            <asp:Parameter Name="personal_image" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>
</asp:Content>

对于给您带来的不便,我深表歉意,

我找到了如何将图像插入数据库的解决方案,但我不知道如何在 formview 中执行此操作? 请帮我? 谢谢

I want to insert image to the database but i don't know how to do that in formview

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Default5.aspx.vb" Inherits="Default5" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:FormView ID="FormView1" runat="server" AllowPaging="True" BackColor="#DEBA84"
        BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2"
        DataKeyNames="code" DataSourceID="SqlDataSource1" GridLines="Both" Width="324px">
        <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
        <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
        <EditItemTemplate>
            code:
            <asp:Label ID="codeLabel1" runat="server" Text='<%# Eval("code") %>'></asp:Label><br />
            name:
            <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>'>
            </asp:TextBox><br />
            nationality:
            <asp:TextBox ID="nationalityTextBox" runat="server" Text='<%# Bind("nationality") %>'>
            </asp:TextBox><br />
            sex:
            <asp:TextBox ID="sexTextBox" runat="server" Text='<%# Bind("sex") %>'>
            </asp:TextBox><br />
            status:
            <asp:TextBox ID="statusTextBox" runat="server" Text='<%# Bind("status") %>'>
            </asp:TextBox><br />
            degree:
            <asp:TextBox ID="degreeTextBox" runat="server" Text='<%# Bind("degree") %>'>
            </asp:TextBox><br />
            job:
            <asp:TextBox ID="jobTextBox" runat="server" Text='<%# Bind("job") %>'>
            </asp:TextBox><br />
            address:
            <asp:TextBox ID="addressTextBox" runat="server" Text='<%# Bind("address") %>'>
            </asp:TextBox><br />
            phone:
            <asp:TextBox ID="phoneTextBox" runat="server" Text='<%# Bind("phone") %>'>
            </asp:TextBox><br />
            total_salary:
            <asp:TextBox ID="total_salaryTextBox" runat="server" Text='<%# Bind("total_salary") %>'>
            </asp:TextBox><br />
            appoint_date:
            <asp:TextBox ID="appoint_dateTextBox" runat="server" Text='<%# Bind("appoint_date") %>'>
            </asp:TextBox><br />
            disconnrct:
            <asp:TextBox ID="disconnrctTextBox" runat="server" Text='<%# Bind("disconnrct") %>'>
            </asp:TextBox><br />
            personal_image:
            <asp:TextBox ID="personal_imageTextBox" runat="server" Text='<%# Bind("personal_image") %>'>
            </asp:TextBox><br />
            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                Text="Update">
            </asp:LinkButton>
            <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                Text="Cancel">
            </asp:LinkButton>
        </EditItemTemplate>
        <InsertItemTemplate>
            code:
            <asp:TextBox ID="codeTextBox" runat="server" Text='<%# Bind("code") %>'>
            </asp:TextBox><br />
            name:
            <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>'>
            </asp:TextBox><br />
            nationality:
            <asp:TextBox ID="nationalityTextBox" runat="server" Text='<%# Bind("nationality") %>'>
            </asp:TextBox><br />
            sex:
            <asp:TextBox ID="sexTextBox" runat="server" Text='<%# Bind("sex") %>'>
            </asp:TextBox><br />
            status:
            <asp:TextBox ID="statusTextBox" runat="server" Text='<%# Bind("status") %>'>
            </asp:TextBox><br />
            degree:
            <asp:TextBox ID="degreeTextBox" runat="server" Text='<%# Bind("degree") %>'>
            </asp:TextBox><br />
            job:
            <asp:TextBox ID="jobTextBox" runat="server" Text='<%# Bind("job") %>'>
            </asp:TextBox><br />
            address:
            <asp:TextBox ID="addressTextBox" runat="server" Text='<%# Bind("address") %>'>
            </asp:TextBox><br />
            phone:
            <asp:TextBox ID="phoneTextBox" runat="server" Text='<%# Bind("phone") %>'>
            </asp:TextBox><br />
            total_salary:
            <asp:TextBox ID="total_salaryTextBox" runat="server" Text='<%# Bind("total_salary") %>'>
            </asp:TextBox><br />
            appoint_date:
            <asp:TextBox ID="appoint_dateTextBox" runat="server" Text='<%# Bind("appoint_date") %>'>
            </asp:TextBox><br />
            disconnrct:
            <asp:TextBox ID="disconnrctTextBox" runat="server" Text='<%# Bind("disconnrct") %>'>
            </asp:TextBox><br />
            personal_image:
            <asp:TextBox ID="personal_imageTextBox" runat="server" Text='<%# Bind("personal_image") %>'>
            </asp:TextBox><br />
            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                Text="Insert">
            </asp:LinkButton>
            <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                Text="Cancel">
            </asp:LinkButton>
        </InsertItemTemplate>
        <ItemTemplate>
            code:
            <asp:Label ID="codeLabel" runat="server" Text='<%# Eval("code") %>'></asp:Label><br />
            name:
            <asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name") %>'></asp:Label><br />
            nationality:
            <asp:Label ID="nationalityLabel" runat="server" Text='<%# Bind("nationality") %>'></asp:Label><br />
            sex:
            <asp:Label ID="sexLabel" runat="server" Text='<%# Bind("sex") %>'></asp:Label><br />
            status:
            <asp:Label ID="statusLabel" runat="server" Text='<%# Bind("status") %>'></asp:Label><br />
            degree:
            <asp:Label ID="degreeLabel" runat="server" Text='<%# Bind("degree") %>'></asp:Label><br />
            job:
            <asp:Label ID="jobLabel" runat="server" Text='<%# Bind("job") %>'></asp:Label><br />
            address:
            <asp:Label ID="addressLabel" runat="server" Text='<%# Bind("address") %>'></asp:Label><br />
            phone:
            <asp:Label ID="phoneLabel" runat="server" Text='<%# Bind("phone") %>'></asp:Label><br />
            total_salary:
            <asp:Label ID="total_salaryLabel" runat="server" Text='<%# Bind("total_salary") %>'></asp:Label><br />
            appoint_date:
            <asp:Label ID="appoint_dateLabel" runat="server" Text='<%# Bind("appoint_date") %>'></asp:Label><br />
            disconnrct:
            <asp:Label ID="disconnrctLabel" runat="server" Text='<%# Bind("disconnrct") %>'></asp:Label><br />
            personal_image:
            <asp:Label ID="personal_imageLabel" runat="server" Text='<%# Bind("personal_image") %>'></asp:Label><br />
            <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
                Text="Edit"></asp:LinkButton>
            <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                Text="Delete"></asp:LinkButton>
            <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
                Text="New"></asp:LinkButton>
        </ItemTemplate>
        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
    </asp:FormView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:employeedataConnectionString %>"
        DeleteCommand="DELETE FROM [emp] WHERE [code] = @code" InsertCommand="INSERT INTO [emp] ([code], [name], [nationality], [sex], [status], [degree], [job], [address], [phone], [total_salary], [appoint_date], [disconnrct], [personal_image]) VALUES (@code, @name, @nationality, @sex, @status, @degree, @job, @address, @phone, @total_salary, @appoint_date, @disconnrct, @personal_image)"
        SelectCommand="SELECT [code], [name], [nationality], [sex], [status], [degree], [job], [address], [phone], [total_salary], [appoint_date], [disconnrct], [personal_image] FROM [emp]"
        UpdateCommand="UPDATE [emp] SET [name] = @name, [nationality] = @nationality, [sex] = @sex, [status] = @status, [degree] = @degree, [job] = @job, [address] = @address, [phone] = @phone, [total_salary] = @total_salary, [appoint_date] = @appoint_date, [disconnrct] = @disconnrct, [personal_image] = @personal_image WHERE [code] = @code">
        <DeleteParameters>
            <asp:Parameter Name="code" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="nationality" Type="String" />
            <asp:Parameter Name="sex" Type="String" />
            <asp:Parameter Name="status" Type="String" />
            <asp:Parameter Name="degree" Type="String" />
            <asp:Parameter Name="job" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="phone" Type="String" />
            <asp:Parameter Name="total_salary" Type="Decimal" />
            <asp:Parameter Name="appoint_date" Type="String" />
            <asp:Parameter Name="disconnrct" Type="String" />
            <asp:Parameter Name="personal_image" Type="String" />
            <asp:Parameter Name="code" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="code" Type="Int32" />
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="nationality" Type="String" />
            <asp:Parameter Name="sex" Type="String" />
            <asp:Parameter Name="status" Type="String" />
            <asp:Parameter Name="degree" Type="String" />
            <asp:Parameter Name="job" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="phone" Type="String" />
            <asp:Parameter Name="total_salary" Type="Decimal" />
            <asp:Parameter Name="appoint_date" Type="String" />
            <asp:Parameter Name="disconnrct" Type="String" />
            <asp:Parameter Name="personal_image" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>
</asp:Content>

I apologize for the inconvenience

I found the solution how to insert image to database but i don't know how to do that in formview?
Please help me?
thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫竹語嫣☆ 2024-11-25 13:50:32

您必须使用 formview ItemInserting 事件。

它应该看起来像...

protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{
    if (fupImage.HasFile)
    {
            byte[] myByte = new byte[fupImage.PostedFile.ContentLength];
            Stream imgStream = fupImage.PostedFile.InputStream;
            imgStream.Read(myByte, 0, fupImage.PostedFile.ContentLength);
           e.Values["personal_image"] = myByte;           
    }
}

其次将参数类型从 String 更改为 Object

这个

应为

You have to use the formview ItemInserting event.

It should look something like...

protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{
    if (fupImage.HasFile)
    {
            byte[] myByte = new byte[fupImage.PostedFile.ContentLength];
            Stream imgStream = fupImage.PostedFile.InputStream;
            imgStream.Read(myByte, 0, fupImage.PostedFile.ContentLength);
           e.Values["personal_image"] = myByte;           
    }
}

Secondly change the parameter type from String to Object

This <asp:Parameter Name="personal_image" Type="String" />

Should be <asp:Parameter Name="personal_image" Type="Object" />

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文