VB.NET - Store and retrieve images in SQL server
database - April 29, 2009 at 18:00 PM by Amit Satpute
Explain how to store and retrieve images in SQL server database through VB.NET.
Images can be stored in the database as follows:
CREATE TABLE PicImage
(
Picid int
Pic Image
)
These images can be retrieved in VB as follows:
Dim ms As New MemoryStream
pic_photo.Image.Save(ms pic_photo.Image.RawFormat)
arrImage ms.GetBuffer
ms.Flush()
Then an array of images can be passed to the query.
VB.NET - Store and retrieve images in SQL server
database - June 07, 2009 at 10:00 PM by Shuchi Gauri
Explain how to store and retrieve images in SQL server database through VB.NET.
a. Create a table for storing images:
CREATE table MyTable (myImage image)
b. Create procedure for storing image in the table create procedure
SaveImage(@getImage as image) as insert into MyTable (sampleimage) values
(@getImage)
c. Code for storing image.
Private Sub InsertImage()
Try
Dim fileStream As New
FileStream("c:\\abc.jpg", FileMode.Open)
Dim byteData() As Byte = New
[Byte](fileStream.Length) {}
fileStream.Read(byteData, 0,
fileStream.Length)
Dim con As New
System.Data.SqlClient.SqlConnection("data source=mt5;initial catalog=master;
user id=sa;password=password")
con.Open()
Dim cmd As New
System.Data.SqlClient.SqlCommand("SaveImage")
cmd.Connection = con
cmd.CommandType =
CommandType.StoredProcedure
cmd.Parameters.Add("@getImage", byteData)
cmd.ExecuteNonQuery()
con.Close()
fileStream.Close()
Catch ex As
System.Data.SqlClient.SqlException
'handle exception here
End Try
End Sub
d. code for retrieving image
Private Sub GetImage()
Dim con As New
System.Data.SqlClient.SqlConnection("data source=mt5;initial catalog=master;
user id=sa;password=password")
con.Open()
Dim cmd As New
System.Data.SqlClient.SqlCommand("select * from MyTable")
cmd.Connection = con
cmd.CommandType = CommandType.Text
Dim da As New
System.Data.SqlClient.SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
Dim bits As Byte() =
CType(ds.Tables(0).Rows(0).Item(0), Byte())
Dim memoryBits As New MemoryStream(bits)
Dim bitmap As New Bitmap(memoryBits) 'bitmap has the
image now.
End Sub
Also read
What is Anonymous type in
VB.NET? Explain its features.
What is STA in .NET?
NET
Using XML in ADO.NET
NET
Validating User Input
NET
Windows communication foundation
|