tags:

views:

632

answers:

5

Hi , i'm working on Linq To Sql,WPF and i have a database now i need to save some picture in the database but i don't know which is the correct datatype to save the pictures Database(this database would be connect from 10 users in the same time). Can you point me in the right way to overcome this step? If i didn't wrong it is not a good idea to save pictures in the database but if you can advice me a better method i will apply it. Thanks so much for your time.

Nice Regards

+3  A: 

Store your picture as a blob, the variable defined in your class containing the image could be a byte[] stream. Alternatively you just store a reference to the picture in the database and store the image on a file server.

Makach
A: 

I've never done it with Linq, but we used a b64 conversion for the image, then a clob datatype. Then reverse the b64 when you want to view the image.

dsrekab
Why would you convert it to base64 and then store it as CLOB? Why not BLOB directly?!?!?!?!
Andrei Rinea
@Andrei:I don't know about dsrekab, but we have an app that must run on multiple back ends. On some servers (Oracle), we have had problems where the Db and/or drivers actually interpreted a binary zero as a null terminator, and truncated the binary image (even though we were saving to a binary-type field). encoding got us around a lot of weird, sometimes hard to repro errors that would turn up in client installations.
JMarsch
JMarsch - That is awesome! We did have truncation issues with the images, but we weren't given the time to figure out why. Someone found a work around and we had to move on. Thanks for solving a 3 year old mystery!
dsrekab
+4  A: 

You can use a 'varbinary(MAX)' or 'image' column type. Linq2Sql will auto-generate a class that uses a Binary object to wrap your image. The Binary object is just a wrapper around a byte[].

myObject.Image = new Binary(imageByteArray);
AgileJon
Make sure you mark the binary property as a delay loaded property in linq-2-sql, so that it is only loaded if it is used. Otherwise everytime you query a row from that table it will load the binary data whether you need it or not.
Frank Schwieterman
+1  A: 

Typically you will use a varbinary(max) -or less than max- on the database side and you will use a byte[] type in your class.

PulsarBlow
A: 

There's a lot of heated debates that occur when people talk about this, I would like to note that you might want to consider storing that path to a network folder in the database.

The disadvantage of storing the actual image in the database is that all those bytes have to get sent back and forth through a sql query and if those images are large you will be increasing the size of your db substantially. along with the weird things that were mentioned above.

Anyways I don't want to open up a can of worms, just wanted to show an alternative.

UPDATE:

Something like this:

public partial class LinqClass
{
    public string ImagePath { get; set; }

    public System.Drawing.Image Picture
    {
        get
        {
            return System.Drawing.Image.FromFile(ImagePath);
        }
    }
}

where ImagePath is the actual column in the db that you are saving the file path to. This doesn't have the code to save the file (something like File.Save(ImagePath) etc. but it's a start.

Jose
Hi Jose ,thanks for your response, can you show me an example of your alternative?Thanks and have a good time.
JayJay
made the updates above
Jose