1

Just starting a project for a course I'm doing and to get data for the database we must create our accessor methods which I found fine but I must create these for pictures that are blobs in my database anyone know how to best do this?

these are what I have so far and the way our lecturer wants them...

namespace ClassLibrary
{
    class Advert
    {
        public String Name
        {
            get { return Name; }
            set { Name = value; }
        }

        public String Genre
        {
            get { return Genre; }
            set { Genre = value; }
        }

        public String Console
        {
            get { return Console; }
            set { Console = value; }
        }

        public String Description
        {
            get { return Description; }
            set { Description = value; }
        }

        public double Price
        {
            get { return Price; }
            set { Price = value; }
        }
    }
}
  • How about a `byte[]`? Then convert it to an image in code... Essentially what a blob is is a `byte[]`. – Sani Huttunen Nov 22 '14 at 22:27
  • 9
    Your getters and setters are infinitely recursive. – ach Nov 22 '14 at 22:28
  • 3
    This is not going towork. All your properties use the same property as their backing fields. You will get a stack overflow... – Sjips Nov 22 '14 at 22:29
  • ...but for images, you can use the Bitmap type. When reading and writing to the database, you could use a memory stream. The buffer property of memory stream is an array of byte. – Sjips Nov 22 '14 at 22:32

1 Answers1

2

Essentially what a blob is is a byte[]:

namespace ClassLibrary {
  public class Advert
  {
    public String Name { get; set; }
    public String Genre { get; set; }
    public String Console { get; set; }
    public String Description { get; set; }
    public double Price { get; set; }
    public byte[] MyImage { get; set; }
  }
}
Sani Huttunen
  • 23,620
  • 6
  • 72
  • 79
  • thanks i was actually looking at that but since this is the first time i was using images in a database i had to ask before i got down to the hard stuff and was totally wrong so thanks for your help – user3433399 Nov 22 '14 at 22:35
  • this link: http://stackoverflow.com/questions/7350679/convert-a-bitmap-into-a-byte-array might help you with conversion of bitmap to/from byte[]. – Sjips Nov 22 '14 at 22:35