Page 2 of 2 FirstFirst 12
Results 41 to 64 of 64

Thread: Saving Images in Databases

  1. #41

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Saving Images in Databases

    Quote Originally Posted by moti barski View Post
    jmcilhinney what about the delete and update of the pictures
    The image is just data in the database like any other. You treat it exactly like any other data. If you have text or numbers in the database and you want to remove or replace them then you set that column of the row to either NULL or a new value. Exactly the same goes here. For instance, if you wanted to edit an image, you would query the database to get the current image, edit in your app, then save the new data, just as you would if you wanted to edit text.

  2. #42
    Banned
    Join Date
    Mar 2009
    Posts
    764

    Re: Saving Images in Databases

    i think it could work with videos not only images

  3. #43

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Saving Images in Databases

    Quote Originally Posted by moti barski View Post
    i think it could work with videos not only images
    Data is data. It would work with anything that needs to be stored in binary form. I chose to write this thread about images because it was a such a common request.

  4. #44
    Junior Member
    Join Date
    Apr 2011
    Location
    QLD, Australia
    Posts
    22

    Re: Saving Images in Databases

    very nice code, works great with my project.

    One thing i was wondering though, if a photo has other data stored against it, like x and y coords etc. Is this able to be extracted to a variable at the point of uploading the image?

  5. #45

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Saving Images in Databases

    Quote Originally Posted by Northy View Post
    very nice code, works great with my project.

    One thing i was wondering though, if a photo has other data stored against it, like x and y coords etc. Is this able to be extracted to a variable at the point of uploading the image?
    Certainly. Exactly how depends on the data format. That is beyond the scope of this thread though. You should start a new thread in the appropriate forum for more info.

  6. #46
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: Saving Images in Databases

    Hi John. I'm here first time to view this spectacular post, you provided. I'm not picking it completely, so starting from first please tell me that in the line No. 2, of vb.net code block, what is update mean there? Will I not insert the image by using insert query? If not, then how i will insert the entire form data, including image.

  7. #47

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Saving Images in Databases

    Quote Originally Posted by ADQUSIT View Post
    Hi John. I'm here first time to view this spectacular post, you provided. I'm not picking it completely, so starting from first please tell me that in the line No. 2, of vb.net code block, what is update mean there? Will I not insert the image by using insert query? If not, then how i will insert the entire form data, including image.
    The actual database is just ADO.NET, the same as any other ADO.NET. You retrieve, insert and update image data in exactly the same way as you do any other data. This thread is purely about the trick of using a MemoryStream to convert between an Image object and a Byte array. As far as ADO.NET is concerned, the Byte array is exactly the same as a String or Integer or any other data you move between your app and a database. Don;t try to make this into something difficult when it's not.

  8. #48
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: Saving Images in Databases

    Its all right, that its an only a trick of using a memoryStream..... right.

    Ok, but please tell me how do I insert image in database. If one is unknown about inserting image (for the first time) so he will definitely face so many problems, so how to solve them? Please John, You are the most senior person, for everyone in the forum. If you will not put the light in the dark roads of newbies, then how they will be professional. Pleasseeee, assist me guide me or refer to me some proper place, from where I can learn it from very beginning that how to insert image, in database, using vb.net.....

    I have very very solid hope from you, being so senior in this forum.

  9. #49
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Saving Images in Databases

    I'm not sure what more you need...
    1) create a sql statement that updates the table, setting the field with binary data... be sure to use a parameter...
    2) add the parameter to the parameters collection
    3) Put the bits of the image into the parameter.... an array of bytes works, as does a stream... could be a memory stream... a file stream... doesn't matter... as long as it's a stream....
    4) execute it...

    you're over thinking it... it's not rocket science... you want an example, post #1 has a very good example... your other post on the same thing, dday provided a LINE by LINE analysis of how it works...


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #50
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: Saving Images in Databases

    I've done all what you wanted me to do, but its now giving me an exception of file not found, in my another post...

  11. #51

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Saving Images in Databases

    Quote Originally Posted by ADQUSIT View Post
    I've done all what you wanted me to do, but its now giving me an exception of file not found, in my another post...
    It should be fairly obvious what the problem is if the exception tells you that it can't find the file you specified.

  12. #52
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: Saving Images in Databases

    but everything is working all right John, then why i'm having error.

  13. #53
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: Saving Images in Databases

    Hmmmmm. Hi John, I have performed all the things correctly, by the great help of all my respected members. I followed your code as well in my problem and i finally got achievement. But there is a little problem, your code, makes me able to select the same image, while i want different image everytime. the path which i have given in code:

    Using picture As Image = Image.FromFile("C:\Documents and Settings\ADQUSIT\Desktop\dwn.jpg")
    It does not select any other picture? please assist me that how to select different pictures?

  14. #54

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Saving Images in Databases

    Quote Originally Posted by ADQUSIT View Post
    Hmmmmm. Hi John, I have performed all the things correctly, by the great help of all my respected members. I followed your code as well in my problem and i finally got achievement. But there is a little problem, your code, makes me able to select the same image, while i want different image everytime. the path which i have given in code:



    It does not select any other picture? please assist me that how to select different pictures?
    That has nothing to do with the topic of this thread.

  15. #55
    Junior Member
    Join Date
    Mar 2013
    Posts
    26

    Re: Saving Images in Databases

    How to do this?? :

    I have a picturebox and a mysql database table named picture where cells > id, picture
    So now i only input Image.Location into my db table where id = something

    and then when i select id from a listview picturebox will show the image from mysql database field where i put image location..
    Please help me on this...

  16. #56

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Saving Images in Databases

    Quote Originally Posted by vbfaint View Post
    How to do this?? :

    I have a picturebox and a mysql database table named picture where cells > id, picture
    So now i only input Image.Location into my db table where id = something

    and then when i select id from a listview picturebox will show the image from mysql database field where i put image location..
    Please help me on this...
    So, you're saying that you want to save the path of an image file to the database? If so then this thread is not the place for it because this thread is specifically about saving images in databases, not text. A file path is just text, so you save it and retrieve it in exactly the same way as any other text. At the other end, loading an image into a PictureBox from a file path is exactly the same regardless of where the file path comes from. If you've done any research on this at all then you already have all the information you need.

  17. #57
    Member
    Join Date
    Jun 2008
    Posts
    41

    Re: Saving Images in Databases

    JM,

    I tried to save images into the database using n-tier. But could not manage. How do I pass an image to the object in the Data Layer. Also, how can i retrieve the image using n-tier. Finally, how to make sure it does not run into an error if there is no image or has been removed (while updating the record).

  18. #58
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Saving Images in Databases

    Firstly, the tiers shouldn't be an issue and are immaterial... you problably mean layers. Layers is logical separation, while tiers is about the physical separation.

    At any rate... don't think of it in terms of an image... it's really an array of bytes you're dealing with. The fact that they represent an image is almost immaterial. so using the file, you get a streamreader and read in all the bytes - Syste.IO.File.ReadAllBytes would likely be a quick shortcut... once you have the array, you simply stream it to the parameter in the SQL.

    To get it back, you read the binary array from the datatable/parameter... then stream it back into an image that you can then assign to the image/picture box.

    And to get around the "error" of no image... don't use it blindly... check the array to see if there is anythign in the first place... if there is, then you proceed.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  19. #59

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Saving Images in Databases

    Quote Originally Posted by tomahawk View Post
    JM,

    I tried to save images into the database using n-tier. But could not manage. How do I pass an image to the object in the Data Layer. Also, how can i retrieve the image using n-tier. Finally, how to make sure it does not run into an error if there is no image or has been removed (while updating the record).
    As tg suggests, data is data. You save image data in exactly the same way as you do any other data. In fact, you treat it the same as any other data everywhere. How would you deal with null text or numeric data? That's how you deal with null image data.

    If you are having issues then you did it wrong. Maybe you should show us what you did so that we can see what's wrong with it, instead of expecting us to explain it from scratch.

  20. #60
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: Saving Images in Databases

    Why we need to convert the image into binary array and then saving it into database. Why we cannot save the image just like we save the value of a textbox or combbox or any other tool?
    What is the reason behind saving and retrieve the image in binary fashion?

  21. #61

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Saving Images in Databases

    Quote Originally Posted by ADQUSIT View Post
    Why we need to convert the image into binary array and then saving it into database. Why we cannot save the image just like we save the value of a textbox or combbox or any other tool?
    What is the reason behind saving and retrieve the image in binary fashion?
    Do you think that a database has a data type that understands what a .NET Image object is? You can save text from a TextBox because text is something universally understood. .NET Images are not universally understood. Databases only have so many data types so you have to make sure the data you save is of one of those data types. Text is universal and there are several text formats: fixed-length, variable-length, single-byte and multi-byte. Numbers are universal and there several numeric types: 8-bit integer, 16-bit integer, 32-bit integer, etc. There's no standard data type for storing Image objects.

    SQL Server actually has a data type named `image` but it doesn't actually store Image objects; it stores binary data. It was originally intended to store images but it's been used to store any data in binary from. It's now deprecated and replaced with varbinary.

    So, in short, you can't just save a .NET Image object because there is no data type in the database that knows how to handle it.

  22. #62
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: Saving Images in Databases

    So are we suppose to use varBinary for storing image, or we can opt any other dataType, suitable for image storing?

  23. #63

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Saving Images in Databases

    Quote Originally Posted by ADQUSIT View Post
    So are we suppose to use varBinary for storing image, or we can opt any other dataType, suitable for image storing?
    You use whatever data your particular database has for storing binary data. In SQL Server that's varbinary; in Access it's OLE Object or the like; in Oracle I believe it's BLOB; etc. The data is going to be stored in the database as binary data so, regardless of the database, that's what you need to create in your .NET app to save.

  24. #64

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width