使用FileUpload上传文件并向数据库插入一条记录

 

 

 

<%@ Page Language="VB" MasterPageFile="~/site.master" AutoEventWireup="false" CodeFile="GalleryUpload.aspx.vb" Inherits="GalleryUpload" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button ID="Button1" runat="server"  Text="Button" />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
    <asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource1" DataTextField="FixtureDate" DataValueField="FixtureID"></asp:ListBox><br /><br />
    <asp:TextBox ID="TextBoxMemberName" runat="server"></asp:TextBox><br /><br />
    <asp:TextBox ID="TextBoxNotes" runat="server"></asp:TextBox>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConflictDetection="CompareAllValues"
        ConnectionString="<%$ ConnectionStrings:WroxUnitedConnectionString %>" 
       InsertCommand="INSERT INTO [Gallery] ([FixtureID], [UploadedByMemberName], [Notes], [PictureURL]) VALUES (@FixtureID, @UploadedByMemberName, @Notes, @PictureURL)"
        OldValuesParameterFormatString="original_{0}" 
        >
        <InsertParameters>
        <asp:ControlParameter Name="FixtureID" ControlID="ListBox1" PropertyName="selectedvalue" Type="Int32" />
        <asp:ControlParameter Name="UploadedByMemberName" ControlID="TextBoxMemberName" PropertyName="text" Type="string" />
        <asp:ControlParameter Name="Notes" ControlID="TextBoxNotes" PropertyName="text" Type="string" />
        <asp:ControlParameter Name="PictureURL" ControlID="fileupload1" PropertyName="filename" Type="string" />
        </InsertParameters>
    </asp:SqlDataSource>
    <asp:Label ID="Label2" runat="server" Style="position: relative" Text="Label"></asp:Label>
    <br />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WroxUnitedConnectionString %>" SelectCommand="SELECT [FixtureID], [FixtureDate] FROM [Fixtures]"></asp:SqlDataSource>
</asp:Content>

注;修改<InsertParameters>集合,使它们从四个控件获取信息。ControlParameters将包含由用户向一些控件(不是容纳这些参数的数据绑定控件)中输入的值。通过在命令中将at(@)符号放在参数的名称前面引用参数中的值。参数按照不同的写命令组织成几个集合。因此当执行insert操作时,ASP.NET 2.0 将在 <InsertParameters>参数集内寻找值。

Partial Class GalleryUpload
    Inherits System.Web.UI.Page
    Dim result As Integer
    Protected path As String = Server.MapPath("MatchImages")
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If FileUpload1.HasFile Then
            Try
                FileUpload1.SaveAs(path & "\" & FileUpload1.FileName)
            Catch ex As Exception
                Label1.Text = "上传失败" & ex.Message
            End Try
            Label1.Text = FileUpload1.FileName & "上传成功"
            SqlDataSource2.Insert()
            Label2.Text = SqlDataSource2.Insert()
        Else
            Label1.Text = "未选择上传文件"
        End If
    End Sub
End Class


注:通过在Button1_Click事件中添加一行触发insert方法的代码来实现SqlDataSource2执行插入新纪录的命令

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值