Saving an image to SQL Server's image datatype

N

Nathan Sokalski

I am using ASP.NET 2.0, and need to know how to save and use an image that
is stored in an SQL Server image datatype. How can I do this using ASP.NET?
Thanks.
 
R

Rahul

A well-kept secret :)








- Show quoted text -

Hi,
Saving image in database is not a good idea, this activity create
unnessary bulky database.
Ok.. If you want to save image in the database, you can use varbinary
datatype insteda of image, microsoft will remove image datatype in
there earlier version.

Rahul
 
L

Linchi Shea

Saving image in database is not a good idea, this activity create
unnessary bulky database.

It depends. If you have a large number of small images, you may be better
off storing them in the database. If you have a relatively small number of
large images, it may be better to keep them in the filesytem. If you want to
stream from these images, keep them outside of the database.

Linchi
 
N

Nathan Sokalski

OK, but regardless of my choice, I need to know what my code should look
like to get them into and out of the DB. I have used the FileUpload control
before to upload a file to the file system, but I have never put an image
(or any other type of file) into a database. If you know of any examples, it
would be extremely helpful. Thanks.

Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/
 
A

Aaron Bertrand [SQL Server MVP]

like to get them into and out of the DB. I have used the FileUpload control
before to upload a file to the file system, but I have never put an image
(or any other type of file) into a database. If you know of any examples, it
would be extremely helpful.

WHICH file upload control? Some of them have a built-in SaveToDatabase-type
method. This is a component I used to use in "Classic ASP" development,
which had this functionality built in:

http://www.aspupload.com/manual_db.html

If you are using .NET then Google yields some results fairly quickly:

http://www.eggheadcafe.com/articles/20030624.asp

http://www.codeproject.com/KB/database/ImageSaveInDataBase.aspx

http://blog.codebeach.com/2008/02/how-to-save-image-in-sql-server.html

(What does this have to do with fulltext / clients?)
 
H

Hans Kesting

Nathan Sokalski laid this down on his screen :
OK, but regardless of my choice, I need to know what my code should look like
to get them into and out of the DB. I have used the FileUpload control before
to upload a file to the file system, but I have never put an image (or any
other type of file) into a database. If you know of any examples, it would be
extremely helpful. Thanks.

I use a store procedure with a parameter of type "image". Inside that
procedure you can use that variable to insert into the table.

When calling that stored procedure from the .Net code, I use parameters
to supply the data. The image-type parameter is then filled with the
byte[] containing that file.

Hans Kesting
 
S

steve

Hey Nathan,

An article on Large Object Storage in a Database or a Filesystem
http://research.microsoft.com/research/pubs/view.aspx?msr_tr_id=MSR-TR-2006-45


If you want to know how I do it:

I use and Image.aspx page and stream the image to that page then in and
other page i use and image controle and set the image.aspx page as image
path.
To get the connection I use linq in the example I make the query with linq
but that is not fast with large tables so you’re better off using stored
procedures with linq.

example:
The page with the image control
imgImageControl.ImageUrl = "Image.aspx?Id=2";


The image.aspx page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Try
Dim objNV As NameValueCollection
Dim pic() As Byte = Nothing
Dim strID As String = ""
objNV = Request.QueryString()
If Not objNV.Count = 0 Then
strID = objNV.Item("ID")
Else
pic = Session("Id")
If pic Is Nothing Then
strID = User.Identity.Name.ToString()
End If
End If
If Not strID = "" Then
Dim db As New
myDB(ConfigurationManager.ConnectionStrings("sql1").ConnectionString)
Dim Updateprofiel = (From p In db.Profiels Where p.Prof_ID =
strID Select p.Foto).ToList()(0)
If Not Updateprofiel Is Nothing Then
pic = Updateprofiel.ToArray()
End If
db.Connection.Close()
End If
If Not pic Is Nothing Then
Response.ContentType = "image/jpeg"
Response.OutputStream.Write(pic, 0, pic.Length)
End If
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Critical, "Big error run
away!!!!!")
End Try

End Sub

steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top