下面的代碼實現向SQL Server數據庫添加圖片和文字的功能。
首先,在SQL查詢分析器中執行下面的語句,以創建表和存儲過程。
Drop Table Person
Go
Create Table Person
(
PersonID Int Identity,
PersonEmail Varchar(255),
PersonName Varchar(255),
PersonSex Char(1),
PersonDOB DateTime,
PersonImage Image,
PersonImageType Varchar(255)
)
Drop Proc sp_person_isp
Go
Create Proc sp_person_isp
@PersonEmail Varchar(255),
@PersonName Varchar(255),
@PersonSex Char(1),
@PersonDOB DateTime,
@PersonImage Image,
@PersonImageType Varchar(255)
As
Begin
Insert into Person
(PersonEmail, PersonName, PersonSex,
PersonDOB, PersonImage, PersonImageType)
Values
(@PersonEmail, @PersonName, @PersonSex,
@PersonDOB, @PersonImage, @PersonImageType)
End
Go
下面就是完整的代碼,拷貝即可運行:
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data.SqlClIEnt" %>
<%@ Import Namespace="System.Data" %>
<%@ Page Language="vb" %>
<Html>
<HEAD>
<title>向SQL Server插入圖片</title>
<script runat="server">
Public Sub AddPerson(sender As Object, e As EventArgs)
Dim intImageSize As Int64
Dim strImageType As String
Dim ImageStream As Stream
’ 獲得圖片的大小
intImageSize = PersonImage.PostedFile.ContentLength
’ 獲得圖片類型
strImageType = PersonImage.PostedFile.ContentType
’讀取圖片
ImageStream = PersonImage.PostedFile.InputStream
Dim ImageContent(intImageSize) As Byte
Dim intStatus As Integer
intStatus = ImageStream.Read(ImageContent, 0, intImageSize)
’ 創建Connection和Command對象
Dim strCnn As String = "Data Source=.;Initial Catalog=mxh;User Id=sa;PassWord=;"
Dim myConnection As New SqlConnection(strCnn)
Dim myCommand As New SqlCommand("sp_person_isp", myConnection)
’ 使用存儲過程
myCommand.CommandType = CommandType.StoredProcedure
’ 向存儲過程添加參數
Dim prmEmail As New SqlParameter("@PersonEmail", SqlDbType.VarChar, 255)
prmEmail.Value = txtPersonEmail.Text
myCommand.Parameters.Add(prmEmail)
Dim prmName As New SqlParameter("@PersonName", SqlDbType.VarChar, 255)
prmName.Value = txtPersonName.Text
myCommand.Parameters.Add(prmName)
Dim prmSex As New SqlParameter("@PersonSex", SqlDbType.Char, 1)
If sexMale.Checked Then
prmSex.Value = "M"
Else
prmSex.Value = "F"
End If
myCommand.Parameters.Add(prmSex)
Dim prmPersonDOB As New SqlParameter("@PersonDOB", SqlDbType.DateTime)
prmPersonDOB.Value = txtPersonDob.Text
myCommand.Parameters.Add(prmPersonDOB)
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("添加成功!")
Catch SQLexc As SqlException
Response.Write("添加失敗,原因:" & SQLexc.ToString())
End Try
End Sub
</script>
</HEAD>
<body style="FONT: 9pt 宋體">
<form enctype="multipart/form-data" runat="server" ID="Form1">
</ASP:TableCell>
</ASP:TableRow>
</ASP:TableCell>
</ASP:TableCell>
</ASP:TableRow>
</ASP:TableCell>
</ASP:TableCell>
</ASP:TableRow>
</ASP:TableCell>
</ASP:TableCell>
</ASP:TableRow>
</ASP:TableCell>
</ASP:TableCell>
</ASP:TableRow>
</ASP:TableCell>
<input type="file" id="PersonImage" runat="server" NAME="PersonImage" /></ASP:TableCell>
</ASP:TableRow>
</ASP:TableCell>
</ASP:TableRow>
</ASP:Table>
</form>
</body>
</Html>