文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>在ASP.NET中把图片保存到SQL SERVER数据库(vb.net) _ASP.NET技巧

在ASP.NET中把图片保存到SQL SERVER数据库(vb.net) _ASP.NET技巧

时间:2010-10-13  来源:蔚蓝的大海

介绍
在很多情况下,我们需要把图片保存到数据库中。在某些应用程序中,存在一些敏感信息不能被存储到文件系统中,因为存储在文件系统上的任何图片都很容易被用户非法获得。
本文将讨论在asp.net中怎样把图片保存到SQL SERVER数据库中。
在本文中我们将了解到以下几方面的内容:
l         上载图片文件的要求
l         使用Strem对象
l         获得上载图片大小和类型
l         如何使用InputStream方法?
上载图片文件的要求
在开始上载前我们需要作两件重要的事情
#Form标记的enctype属性需要被设置为如下形式:
enctype="multipart/form-data"
#提供一个让用户选择图片文件的Html控件:
<input type=file>
#还要引用System.IO命名空间来处理Strem对象
上述的三项都要应用到aspx页中。在SQL SERVER中还有以下的一些要求:
#一个至少有一个字段类型为Image的表
#另外有一个用来存储图片类型的Varchar类型的字段就更好了
那么,我们有了一个有Image字段类型的数据表和一个<input type=file>(HTML文件控件)。我们还需要一个提交按钮,当用户选择好图片后可以点击它。在按钮的OnClick事件中我们要获得图片文件的内容并最终把它插入到数据表中。让我们来看看按钮的OnClick事件,它读取图片并把图片插入到数据表中。
提交按钮的OnClick事件代码
http://blog.knowsky.com/
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
它是如何工作的?
对象PersonImage 是HTMLInputFile 控件。首先我们要获得被插入图片的大小,通过如下方法实现:
intImageSize = PersonImage.PostedFile.ContentLength
接着要通过ContenType属性获得图片类型。最后最重要的是要获得图片文件流,通过如下方法实现:
ImageStream = PersonImage.PostedFile.InputStream
我们有一个byte数组ImageContent,准备用来保存图片内容。整个图片通过Stream对象的Read方法读取,这个方法有三个参数,即:
#被复制的图片内容的目标位置
#读的开始位置
#需要被读的子节数
读声明如下:
intStatus = ImageStream.Read(ImageContent, 0, intImageSize)
现在,我们读取了整个图片内容。接着我们需要把图片内容插入SQL数据表中,我们将用用一个存储过程把图片类型和图片插入SQL数据表。如果你看过上面的代码清单,你就知道我们把数据类型设置为SqlDbType.Image.就这样,我们成功地把图片保存到了SQL SERVER数据库。

Replica lv Hangbags

discount Hangbags

Hangbags

Hangbags

replica handbags

replica handbags

replica handbags

handbags

replica handbags

replica handbags

replica handbags

replica handbags

replica handbags

replica handbags

replica handbags

replica handbags

replica handbags replica handbags discount designer handbags handbags handbags handbags replica handbags replica handbags replica handbags replica handbags replica handbags discount designer handbags replica handbags replica handbags replica handbags replica handbags replica handbags replica handbags replica handbags replica handbags replica handbags discount handbags replica designer handbags discount designer handbags

usb cable

usb cable

usb cable

usb cable

usb flash drives

usb flash drives

usb flash drives usb flash drives usb flash drives USB Connectors,HDMI Cable,Custom Usb Flash Drives USB Connector Replica Watches Replica Watches Replica Watches Replica designer Watches Replica Watches Replica Watches cheap Watches replica Watches replica Watches replica Watches replica Watches replica Watches
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载