asp.net資料庫裡存照片/ASP.NET存取 SQLServer資料庫圖片的簡單方法

來源:互聯網
上載者:User

ASP.NET存取 SQLServer資料庫圖片可以說是很頭痛的問題,而我要做的一個系統恰好要顯示使用者的照片,555,不同的人的照片肯定不同!哎,沒人指導只有靠自己去摸索咯。嘿嘿,經過一天的努力,我終於做把這個問題解決啦。

我在網上搜尋了一些相關的內容,方法大致相同,就是把圖片以二進位的方法存取,看到那麼多代碼我頭都大了,有沒有更簡單的方法呢,我問了一下表姐,她是專門搞軟體開發的,她說可以把照片放在一個檔案夾裡面,然後在資料庫裡調用,具體方法她沒說,但有個思路就好了。

我做了兩個簡單的頁面實驗了一下:
首先是upfile.aspx頁面,用來上傳圖片的
代碼如下:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="upfile.aspx.vb" Inherits="upfile" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://hi.baidu.com/highadmin">

<html xmlns="http://hi.baidu.com/highadmin" >
<head runat="server">
    <title>upfile</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues"

            ConnectionString="<%$ ConnectionStrings:MSISConnectionString %>"             InsertCommand="INSERT INTO [Gallery] ([uploadByMemberName], [Notes], [pictureURL]) VALUES (@uploadByMemberName, @Notes, @pictureURL)"
            OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [Gallery]"
            <InsertParameters>
                <asp:ControlParameter Name="uploadByMemberName" ControlID="TextBox1" PropertyName="Text" Type="String" />
                <asp:ControlParameter Name="Notes" ControlID="TextBox2" PropertyName="Text" Type="String" />
                <asp:ControlParameter Name="pictureURL" ControlID="FileUpload1" PropertyName="FileName" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
          <br />
        <asp:Button ID="Button1" runat="server" Text="Button" /></div>
    </form>
</body>
</html>

 

upfile.aspx.vb的代碼如下:

Partial Class upfile
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If FileUpload1.HasFile Then
            Try
                FileUpload1.SaveAs("D:\學生資訊管理系統\網站\WebSite\images\" & FileUpload1.FileName)
            Catch ex As Exception

            End Try

            SqlDataSource1.Insert()

        End If
    End Sub
End Class

 

資料顯示的頁面做得很簡單,用了一個DATALIST控制項。

datalist.aspx 代碼如下:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="datalist.aspx.vb" Inherits="datalist" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://hi.baidu.com/highadmin">

<html xmlns="http://hi.baidu.com/highadmin" >
<head runat="server">
    <title>datalist</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" DataKeyField="PictureID" DataSourceID="SqlDataSource1">
            <ItemTemplate>
                PictureID:
                <asp:Label ID="PictureIDLabel" runat="server" Text='<%# Eval("PictureID") %>'></asp:Label><br />
                uploadByMemberName:
                <asp:Label ID="uploadByMemberNameLabel" runat="server" Text='<%# Eval("uploadByMemberName") %>'></asp:Label><br />
                Notes:
                <asp:Label ID="NotesLabel" runat="server" Text='<%# Eval("Notes") %>'></asp:Label><br />
                <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("pictureURL", "~/images/{0}") %>' /><br />
                <br />
            </ItemTemplate>
        </asp:DataList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MSISConnectionString %>"
            SelectCommand="SELECT * FROM [Gallery]"></asp:SqlDataSource>
  
    </div>
    </form>
</body>
</html>

 

主要原理:把圖片上傳到一個指定的檔案夾裡,上傳的同時把所上傳圖片的檔案名稱(非路徑,只是檔案名稱)也寫進了相關使用者的資料庫,然後顯示圖片的時候,在DATALIST裡添加一個IMAGIN控制項,進行資料繫結就好了!

儲存:  
  (query1.FieldByName('img')   as   TBlobField).LoadFromFile('e:\temp.bmp');  
  取出:  
  (query1.FieldByName('img')   as   TBlobField).Savetofile('e:\temp.bmp');  
  試試,應該能儲存比較大的圖的。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.