How to store and retrieve images in sql server database through VB.NET

Showing Answers 1 - 3 of 3 Answers

harsha

  • Aug 7th, 2006
 

SQL Server supports the ability for clients to store objects within tables

  Was this answer useful?  Yes

tapasvi darji

  • Aug 16th, 2006
 

To store image in database 1st u need to make table like this

CREATE TABLE PicImage
   (
    Picid int,
    Pic Image
    )

Now in vb.net coding u have to write code like this

Dim ms As New MemoryStream

pic_photo.Image.Save(ms, pic_photo.Image.RawFormat)

arrImage = ms.GetBuffer

ms.Flush()

Now pass arrImage in ur insert query.

  Was this answer useful?  Yes

geocheru

  • Jun 14th, 2009
 

Dim con As New SqlConnection _
("Server=YourServer;uid=;pwd=;database=northwind")
Dim da As New SqlDataAdapter _
("Select * From MyImages", con)
Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
Dim ds As New DataSet()


da.MissingSchemaAction = MissingSchemaAction.AddWithKey


Dim fs As New FileStream _
("C:winntGone Fishing.BMP", FileMode.OpenOrCreate, _
FileAccess.Read)
Dim MyData(fs.Length) As Byte
fs.Read(MyData, 0, fs.Length)
fs.Close()
con.Open()
da.Fill(ds, "MyImages")
Dim myRow As DataRow
myRow = ds.Tables("MyImages").NewRow()


myRow("Description") = "This would be description text"
myRow("imgField") = MyData
ds.Tables("MyImages").Rows.Add(myRow)
da.Update(ds, "MyImages")


fs = Nothing
MyCB = Nothing
ds = Nothing
da = Nothing


con.Close()
con = Nothing
MsgBox ("Image saved to database")


Double-click Button2, and then add the following code to the Button2_Click
event handler:
Note You must change uid and pwd = to the correct values before you run this
code. Make sure that User ID has the appropriate permissions to perform this
operation on the database.


Dim con As New SqlConnection _
("Server=YourServer;uid=;pwd=;database=northwind")
Dim da As New SqlDataAdapter _
("Select * From MyImages", con)
Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
Dim ds As New DataSet()


con.Open()
da.Fill(ds, "MyImages")
Dim myRow As DataRow
myRow = ds.Tables("MyImages").Rows(0)


Dim MyData() As Byte
MyData = myRow("imgField")
Dim K As Long
K = UBound(MyData)


Dim fs As New FileStream _
("C:winntGone Fishing2.BMP", FileMode.OpenOrCreate, _
FileAccess.Write)
fs.Write(MyData, 0, K)
fs.Close()


fs = Nothing
MyCB = Nothing
ds = Nothing
da = Nothing


con.Close()
con = Nothing
MsgBox ("Image retrieved")

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions