Save and retrieve image from sql server 2000 in asp.net

Imports System.Drawing

Imports System.Data

Imports System.IO

Imports System.Drawing.Imaging

Private Sub btnUploadImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUploadImage.Click

Dim intImgLen As Integer = MyFile.PostedFile.ContentLength

Dim picbyte(intImgLen) As Byte

MyFile.PostedFile.InputStream.Read(picbyte, 0, intImgLen)

SqlCon = New SqlConnection("server=172.16.1.22;uid=sa;pwd=;database=test;")

SqlCon.Open()

SSQL = "Insert into ImageTable (Image) values(@pic)"

sCommand = New SqlCommand(SSQL, SqlCon)

sCommand.Parameters.Add("@pic", picbyte)

sCommand.ExecuteNonQuery()

SqlCon.Close()

End Sub

Private Sub btnFetchImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFetchImage.Click

Dim imag() As Byte

Dim Img As Image

SqlCon = New SqlConnection("server=172.16.1.22;uid=sa;pwd=;database=test;")

SqlCon.Open()

SSQL = "select Image from ImageTable"

sCommand = New SqlCommand(SSQL, SqlCon)

SReader = sCommand.ExecuteReader()

While (SReader.Read())

Dim byt As Byte()

byt = SReader.Item("Image")

'Dim bmp As New Bitmap(New System.IO.MemoryStream(byt))

'Img = bmp

Response.BinaryWrite(byt)

End While

SqlCon.Close()

End Sub




Get the new Windows Live Messenger!
Try it!

No comments: