server| Data | Database The following code implements the ability to add pictures and text to a SQL Server database.
First, execute the following statement in SQL Query Analyzer to create tables and stored procedures.
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
The following is the complete code, and the copy can be run:
<%@ Import namespace= "System.IO"%>
<%@ Import namespace= "System.Data.SqlClient"%>
<%@ Import namespace= "System.Data"%>
<%@ Page language= "vb"%>
<HTML>
<HEAD>
<title> Insert Picture </title> to SQL Server
<script runat= "Server" >
Public Sub Addperson (sender as Object, E as EventArgs)
Dim Intimagesize as Int64
Dim Strimagetype as String
Dim ImageStream as Stream
' Get the size of the picture
Intimagesize = PersonImage.PostedFile.ContentLength
' Get picture type
Strimagetype = PersonImage.PostedFile.ContentType
' Read the picture
ImageStream = PersonImage.PostedFile.InputStream
Dim imagecontent (intimagesize) as Byte
Dim Intstatus as Integer
Intstatus = Imagestream.read (imagecontent, 0, Intimagesize)
' Create connection and command objects
Dim strcnn as String = "Data source=.;i Nitial catalog=mxh; User Id=sa; password=; "
Dim MyConnection as New SqlConnection (STRCNN)
Dim mycommand as New SqlCommand ("Sp_person_isp", MyConnection)
' Use stored procedures
myCommand.CommandType = CommandType.StoredProcedure
' Add parameters to a stored procedure
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)
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.