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

时间:2011-07-06 08:43:50

标签: c# .net asp.net formview

我想将图像插入数据库,但我不知道如何在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中执行此操作? 请帮我? 谢谢

1 个答案:

答案 0 :(得分:0)

您必须使用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 to Object

更改参数类型

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

应为<asp:Parameter Name="personal_image" Type="Object" />