Images into SQL Server

  • Thread starter Thread starter Steve Peterson
  • Start date Start date
S

Steve Peterson

Hi

I was hoping that someone could point me in the direction of some articles
on how to get images into a SQL Server database. I have an ASP.NET app
where I need to let the users upload images to the server (that's no
problem),
but then I need to get the uploaded image into a table in SQL Server. I then
need to know how to get the binary data out ot the table in SQL & write it
back to the disk as an image file.

If anyone could point me in the right direction to read up on this I would
really appreciate it

TIA
Steve
 
if you search on "BLOB" in sql server books online you'll get instructions
on this. I recommend you not store the images in sql server though. Store
the image paths in sql server and the files on 1 or more file servers or a
san. This is the way a successful .com i just got fired from does it. One of
the reasons for doing this is that sql server may start crawling if you get
it up to about 300 GB which is not that hard to do with images.
 
Hi Steve,

I made a sample for this. It uses a dataset direct to write as XML to disk,
however that is only for the sample

I agree with Alex, however you can use this also to set a thumbnail in a
database, it needs than an extra memory tream to make the thumbnail.

I hope this helps?

Cor

\\\the sample needs only a form with a picturebox and four buttons on it.
Private abyt() As Byte
Private fo As New OpenFileDialog
Private sf As New SaveFileDialog
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Reading a picture and put it in a bytearray
If fo.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(fo.FileName, _
IO.FileMode.Open)
Dim br As New IO.BinaryReader(fs)
abyt = br.ReadBytes(CInt(fs.Length))
br.Close()
'just to show the sample without a fileread error
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Button2.Click
'writing a picture from a bytearray
If sf.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(sf.FileName, _
IO.FileMode.CreateNew)
Dim bw As New IO.BinaryWriter(fs)
bw.Write(abyt)
bw.Close()
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Button3.Click
'writing a bytearray to a dataset
Dim ds As New DataSet
ds.Tables.Add(New DataTable("Photo"))
ds.Tables(0).Columns.Add(New DataColumn("Sample"))
ds.Tables(0).Columns(0).DataType =
System.Type.GetType("System.Byte[]")
ds.Tables(0).Rows.Add(ds.Tables(0).NewRow)
ds.Tables(0).Rows(0)(0) = abyt
Dim sf As New SaveFileDialog
If sf.ShowDialog = DialogResult.OK Then
ds.WriteXml(sf.FileName, XmlWriteMode.WriteSchema)
End If
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button4.Click
'reading a picture from a dataset
Dim ds As New DataSet
If fo.ShowDialog = DialogResult.OK Then
ds.ReadXml(fo.FileName)
End If
abyt = CType(ds.Tables(0).Rows(0)(0), Byte())
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End Sub
///
 
H
Although you intend to store the images into Sql server, I suggest you take a look this imagemanager control in www.smartcodeworks.com, you only need to upload the images into a server folder, the control will generate image thumbnail and display them in pages

Coder191
 
Back
Top