Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

C# & SQLite – Storing Images

0.00/5 (No votes)
16 Sep 2011CPOL1 min read 20.7K  
This post aims to demostrate how to load images into a SQLite database and retrieve them for viewing.

This post aims to demonstrate how to load images into a SQLite database and retrieve them for viewing.

It is written in VS2010, C#, .NET4.0 and uses a ADO.NET provider System.Data.SQLite to connect to the SQLIte database.

And this all in a Windows XP environment.

First of all, one has to obtain a few files and install them according to the rules.

SQLite ADO.NET provider

I installed the package into my “C:\” directory and chose not to register the DLL files, due to only wanting to include the DLL files to my project.

SQLite

First I created a new database named ImageLib.s3db and added a table and required fields.
[ Lib for Library ].

SQL
CREATE TABLE [ImageStore] (
[ImageStore_Id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[ImageFile] NVARCHAR(20) NULL,
[ImageBlob] BLOB NULL
);
VS2010 – C# – .NET 4.0

Next I created a VS2010 project named StoringImages, changed the default namespace and added a few folders and files.

  • folder: Database
    • file: StoringImages.s3db
      • Property: Copy to Output Directory => Copy Always
  • folder: Model
    • dBFunctions.cs
    • dBHelper.cs
    • Image.cs
    • ImageHelper.cs
  • file: System.Data.SQLite.dll
    • Property: Copy to Output Directory => Copy Always
  • file: SQLite.Interop.dll
    • Property: Copy to Output Directory => Copy Always
  • form: DisplayImages
    • This is the startup form of the project

Both System.Data.SQLite.dll and SQLite.Interop.dll need to be placed just beneath the root (project) StoringImages.

This ensures that both files are installed into the same directory as the projects “*.exe” file.

You can download the source code here.

For the rest of the article and more details, please visit CodeProject.com.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)