zoukankan      html  css  js  c++  java
  • ASP.NET数据库图片存储到Sql2000中

    上篇:图片存入到Sql Server
    
    在很多时候,我们有这样的需求:把图片存入到数据库当中。在一些应用程序中,我们可能有一些敏感的资料,由于存储在文件系统(file system)中的东西,将很容易被某些用户盗取,所以这些数据不能存放在文件系统中。
    
    在这篇文章中,我们将讨论怎样把图片存入到Sql2000当中。
    
    在这篇文章中我们可以学到以下几个方面的知识:
    
    1. 插入图片的必要条件
    
    2. 使用流对象
    
    3. 查找准备上传的图片的大小和类型
    
    4.怎么使用InputStream方法?
    
    ASP.NET数据库图片存储:插入图片的必要条件
    
    在我们开始上传之前,有两件重要的事我们需要做:
    
    #Form 标记的 enctype 属性应该设置成 enctype="multipart/form-data"
    
    # 需要一个<input type=file>表单来使用户选择他们要上传的文件,同时我们需要导入 System.IO名称空间来处理流对象
    
    把以上三点应用到aspx页面。同时我们需要对SqlServer做以下的准备。
    
    # 需要至少含有一个图片类型的字段的表
    
    # 如果我们还有另外一个变字符类型的字段来存储图片类型,那样会更好一些。
    
    现在,我们准备了一个Sql表(包含了一个image数据类型的字段),还有<input type=file>标记。当然我们还得准备Submit按钮,以便用户在选择了图片以后提交。在这个按钮的Onclick事件里,我们需要读取选取图片的内容,然后把它存入到表里。那我们先来看看这个Onclick事件。
    
    提交按钮的Onclick事件的代码:
    
    Dim intImageSize As Int64  
         Dim strImageType As String  
         Dim ImageStream As Stream 
     
        ’ Gets the Size of the Image  
        intImageSize = PersonImage.PostedFile.ContentLength 
     
        ’ Gets the Image Type  
        strImageType = PersonImage.PostedFile.ContentType 
     
        ’ Reads the Image  
        ImageStream = PersonImage.PostedFile.InputStream 
     
        Dim ImageContent(intImageSize) As Byte  
        Dim intStatus As Integer  
        intStatus = ImageStream.Read(ImageContent, 0, intImageSize) 
     
        ’ Create Instance of Connection and Command Object  
        Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))  
        Dim myCommand As New SqlCommand("sp_person_isp", myConnection) 
     
        ’ Mark the Command as a SPROC  
        myCommand.CommandType = CommandType.StoredProcedure 
     
        ’ Add Parameters to SPROC  
        Dim prmPersonImage As New SqlParameter("@PersonImage", SqlDbType.Image)  
        prmPersonImage.Value = ImageContent  
        myCommand.Parameters.Add(prmPersonImage) 
     
        Dim prmPersonImageType As New SqlParameter("@PersonImageType", SqlDbType.VarChar, 255)  
        prmPersonImageType.Value = strImageType  
        myCommand.Parameters.Add(prmPersonImageType) 
     
        Try  
            myConnection.Open()  
            myCommand.ExecuteNonQuery()  
            myConnection.Close() 
           Response.Write("New person successfully added!")  
        Catch SQLexc As SqlException  
            Response.Write("Insert Failed. Error Details are: " & SQLexc.ToString())  
        End Try 
    这是怎么实现ASP.NET数据库图片存储的呢?
    
    PersonImage是HTMLInputFile控件的对象。首先需要获得图片的大小,可以使用下面的代码实现:
    
    intImageSize = PersonImage.PostedFile.ContentLength
    
    然后返回图片的类型使用ContenType属性。最后,也是最重要的事就是取得Image Stream,这可以用以下代码实现:
    
    ImageStream = PersonImage.PostedFile.InputStream
    
    我们需要一个字节型数组来存储image 内容。读取整个图片可以使用Stream对象的Read方法来实现。Read(in byte[] buffer,int offset,int count)方法有三个参数。他们是:
    
    buffer
    
    字节数组。此方法返回时,该缓冲区包含指定的字符数组,该数组的 offset 和 (offset + count) 之间的值由从当前源中读取的字节替换。
    
    offset
    
    buffer 中的从零开始的字节偏移量,从此处开始存储从当前流中读取的数据。
    
    count
    
    要从当前流中最多读取的字节数。
    
    这个Read方法用以下代码实现: 
    intStatus = ImageStream.Read(ImageContent, 0, intImageSize) 
    .
    
    现在,我们已经读取了整个图片的内容,下一步,我们要把这些内容存入到sql 表。我们将使用存储过程来完成插入图片类型和图片内容到sql 表。如果你浏览了上面的代码,你将会发现我们使用了sqldbtype.image的数据类型(datatype)。Ok了,完成了这些,我们也就成功的把图片存入到SqlServer中了。
    
    下面是我们编写的aspx页面。
    
    ASP.NET数据库图片存储:图片存入数据库结论
    
    我们已经讨论了如何把图片存入到Sql Server,那么我们如何从SqlServer中读取图片呢?
    
     
    
    下篇:从SqlServer中读取图片
    
     
    
     
    
    这篇文章是我写的"如何把图片存入 sqlServer中"的后续。 
    和存储图片相比,读取图片就要简单多了。输出一副图片我们要做的就是使用 Response对象的BinaryWrite方法。 
    同时设置图片的格式。在这篇文章中,我们将讨论如何从SqlServer中检索图片。 
    并将学习以下几个方面的知识. 
    ·如何设置图片的格式? 
    ·如何使用BinaryWrite方法。 
    我们已经在Person表中存储了数据,那么我们就写些代码来从表中读取数据。 
    下面的代码检索了所有的值从Person表中。 
    从sqlserver中读取图片的代码. 
    Public Sub Page_Load(sender As Object, e As EventArgs) 
            Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString")) 
            Dim myCommand As New SqlCommand("Select * from Person", myConnection) 
            Try 
                myConnection.Open() 
                Dim myDataReader as SqlDataReader 
                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection) 
                Do While (myDataReader.Read()) 
                    Response.ContentType = myDataReader.Item("PersonImageType") 
                    Response.BinaryWrite(myDataReader.Item("PersonImage")) 
                Loop 
                myConnection.Close() 
                Response.Write("Person info successfully retrieved!") 
            Catch SQLexc As SqlException 
                Response.Write("Read Failed : " & SQLexc.ToString()) 
            End Try 
        End Sub 
    看看他是怎么工作的? 
    上面的例子很简单。我们所作的就是执行一个sql语句,再循环读取所有的记录(looping through all the records). 
    在显示图片之前,我们先设置了图片的contentType,然后我们使用BinaryWrite方法把图片输出到浏览器。
    源代码: 
    /// retriving.aspx 
    <%@ Page Language="vb" %> 
    <%@ Import Namespace="System.Data" %> 
    <%@ Import Namespace="System.Data.SqlClient" %> 
    <HTML> 
      <HEAD> 
        <title>Retrieving Image from the Sql Server</title> 
            <script runat=server> 
                            Public Sub Page_Load(sender As Object, e As EventArgs) 
                                        ' Create Instance of Connection and Command Object 
                                        Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString")) 
                                        Dim myCommand As New SqlCommand("Select * from Person", myConnection) 
                                         Try 
                                                   myConnection.Open() 
                                                    Dim myDataReader as SqlDataReader 
                                                    myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection) 
                                                   Do While (myDataReader.Read()) 
                                                                Response.ContentType = myDataReader.Item("PersonImageType") 
                                                                Response.BinaryWrite(myDataReader.Item("PersonImage")) 
                                                   Loop                                             
                                                    myConnection.Close() 
                                                    Response.Write("Person info successfully retrieved!") 
                                        Catch SQLexc As SqlException 
                                                    Response.Write("Read Failed : " & SQLexc.ToString()) 
                                        End Try 
                            End Sub     
        </script> 
      </HEAD> 
      <body style="font: 10pt verdana"> 
      </body> 
    </HTML>
    
  • 相关阅读:
    psql: FATAL: index "pg_opclass_oid_index" contains unexpected zero page at block 0 以及错误 rm: cannot remove ‘base/90112/95992_fsm’: Structure needs cleaning
    rabbitmq centos 7.4 主备安装,加入主节点失败Error: unable to perform an operation on node 'rabbit@mq2'. Please see diagnostics information and suggestions below.
    Centos 7 查看显卡型号
    ORA-600 16703 SYS.TAB$表被删除
    linux orcal启动
    windows 下redis配置一主两从三哨兵模式以及springboot集成
    Spring定时任务service无法注入问题
    web缓存欺骗
    Automate Cache Poisoning Vulnerability
    bugbounty
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/1725434.html
Copyright © 2011-2022 走看看