praeclarum / sqlite-net

Simple, powerful, cross-platform SQLite client and ORM for .NET
MIT License
4.09k stars 1.42k forks source link

How to insert blob in SQLite in Windows store app developed in c#/XAML #116

Open Xyroid opened 12 years ago

Xyroid commented 12 years ago

I am developing an app which has some media files like images/audio/video clip. I want to insert those media files as datatype via SQLite in my C#/XAML windows store app. I can't find any example showing the blob datatype. How can I do that ?

mattleibow commented 12 years ago

Storing media in the DB is not very good as the media can increase the size of the DB quite quickly. This may degrade performance. You can maybe try storing the media in a logical file structure. Such as:

Data
    database.db
    Images
        People -- table
            001.bmp -- <id>.bmp
            002.bmp
        Houses -- table
            001.bmp
            002.bmp
    Audio
        Cars -- table
            001.mp3
            002.mp3

But if you still want to use the DB:

http://www.codeproject.com/Articles/15460/C-Image-to-Byte-Array-and-Byte-Array-to-Image-Conv

In short:

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
     MemoryStream ms = new MemoryStream();
     imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
     return  ms.ToArray();
}

Serialization can be used to store custom media or objects as well.