5 Replies - 4612 Views - Last Post: 21 February 2012 - 07:47 PM Rate Topic: -----

#1 plejer_adam  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 90
  • Joined: 17-May 10

Save and load pictures from sql server database

Posted 20 February 2012 - 01:44 AM

I have never done this, so i am in little trouble :)

Hello everyone, i am having trouble finding solution for inserting picture from filedialog control to app , and then saving that picture to sql server database. And when i need , to load that picture again in app.

Anyone have piece of code ? Or willing to help me do that.


And one more thing, i am working with entity framework

Thank you

This post has been edited by plejer_adam: 20 February 2012 - 01:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Save and load pictures from sql server database

#2 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: Save and load pictures from sql server database

Posted 20 February 2012 - 05:12 AM

The OpenFileDialog doesn't really get the actual file you select, but rather the full path to the file. You have to utilize that.

Let's say you open a file dialog, this is how you would catch the file path specified:

string filePath = myOpenFileDialog.FileName;



Then you could get the image from that file:

Image myImage = Image.FromFile(filePath);



NOTE: I'm using the abstract class Image here, because you don't know if it's going to be .bmp or a .jpg.

NOTE 2: You need to watch out if it's the multipleItems property of the openFileDialog is enabled, because in that case the openFileDialog.FileNames returns a string array.

Some References On This Part:
Image.FromFile Method
OpenFileDialog Class

This post has been edited by RexGrammer: 20 February 2012 - 05:18 AM

Was This Post Helpful? 0
  • +
  • -

#3 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: Save and load pictures from sql server database

Posted 20 February 2012 - 05:25 AM

Now for the second part:

To store an image to a database you have to store it in binary format. So you first need to convert the image into binary, and then upload it into the database. When you want to retrieve it you get it in binary format and convert it back into an image.

I would suggest these tutorials:
Inserting and Retrieving images from SQL Server Database using C#
Store or save images in SQL Server database using C#
Was This Post Helpful? 0
  • +
  • -

#4 plejer_adam  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 90
  • Joined: 17-May 10

Re: Save and load pictures from sql server database

Posted 20 February 2012 - 05:26 AM

Thank you so far.
Was This Post Helpful? 0
  • +
  • -

#5 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: Save and load pictures from sql server database

Posted 21 February 2012 - 04:43 PM

Well, I'm not hugely experienced with DBs, just started learning them myself about a week or two ago. But I don't think you would actually need to store an image in the database. You could just store the image file name/ file path and then retrieve it from a database and convert it back into an image.
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4431
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: Save and load pictures from sql server database

Posted 21 February 2012 - 07:47 PM

Why would you say that? There are perfectly valid reasons to store images in the database. For instance, if it's on the file system, anyone could come by and delete it, move it, or rename it. Then you have a path that doesn't point to a file. If it's stored in binary in the database, it's not vulnerable to that.

Of course, there's overhead. You have to consider if it's worth having to query binary data or load it directly from the file system, but there are real reasons to do so.

In fact, you can store any binary data in the Image field in Sql Server. For example, MS Dynamics CRM stores DLL plugins in the database as binary representations.

Image (the t-sql data type) doesn't actually mean picture, btw, it means "a binary image". On the other hand, you shouldn't actually use it anymore, according to this page:

http://msdn.microsof...y/ms187993.aspx

You should use varbinary(max) instead, because Image is being deprecated in future versions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1