通过GridView从SQL-Server-2012下载文件

时间:2013-04-11 16:01:18

标签: asp.net vb.net gridview sql-server-2012 sqldatasource

如何通过向ASP.NET / VB.NET中的GridView添加按钮来从SQL-Server-2012下载文件?

我知道你可以自动创建DELETE和SELECT字段,但有没有一种方法可以让你从数据库下载文件?

SqlDataSource和GridView ASPX:

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="content_id" ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:CommandField ButtonType="Button" ShowDeleteButton="True" ShowSelectButton="True">
            <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            <ItemStyle BorderStyle="None" HorizontalAlign="Center" VerticalAlign="Middle" />

            </asp:CommandField>
            <asp:BoundField DataField="content_id" HeaderText="#" InsertVisible="False" ReadOnly="True" SortExpression="content_id">
            <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="30px" />
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="30px" />
            </asp:BoundField>
            <asp:BoundField DataField="content_name" HeaderText="Name" SortExpression="content_name">
            <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            </asp:BoundField>
            <asp:BoundField DataField="content_type" HeaderText="Type" SortExpression="content_type">
            <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            </asp:BoundField>
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>


    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" DeleteCommand="DELETE FROM [content] WHERE [content_id] = @content_id" InsertCommand="INSERT INTO [content] ([content_name], [content_type]) VALUES (@content_name, @content_type)" SelectCommand="SELECT [content_name], [content_type], [content_id] FROM [content]" UpdateCommand="UPDATE [content] SET [content_name] = @content_name, [content_type] = @content_type WHERE [content_id] = @content_id">
        <DeleteParameters>
            <asp:Parameter Name="content_id" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="content_name" Type="String" />
            <asp:Parameter Name="content_type" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="content_name" Type="String" />
            <asp:Parameter Name="content_type" Type="String" />
            <asp:Parameter Name="content_id" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>

enter image description here

修改

RowCommand代码:

 Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
        If e.CommandName = "Download" Then
            Dim fileName As String = String.Empty
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow = GridView1.Rows(index)
            Dim contentID As Integer = Convert.ToInt32(GridView1.DataKeys(index).Value)
            Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            Dim cmd As New SqlCommand("SELECT content_name,content_file FROM content WHERE content_id = " & contentID, con)
            con.Open()
            Dim dReader As SqlDataReader = cmd.ExecuteReader()
            While dReader.Read()
                fileName = dReader("content_name").ToString()
                Dim contentBinary As Byte() = DirectCast(dReader("content_file"), Byte())
                Dim fStream As New FileStream(Server.MapPath("Docs") & "\" & fileName, FileMode.Create)
                fStream.Write(contentBinary, 0, contentBinary.Length)
                fStream.Close()
                fStream.Dispose()
            End While
            con.Close()
            Response.Redirect("Docs\" & fileName)
        End If
    End Sub

0 个答案:

没有答案