在这篇文章中,我们将讨论怎样把图片存入到Sql2000当中。 在这篇文章中我们可以学到以下几个方面的知识: 1. 插入图片的必要条件 2. 使用流对象 3. 查找准备上传的图片的大小和类型 4.怎么使用InputStream方法? 插入图片的必要条件 在我们开始上传之前,有两件重要的事我们需要做: #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