c# Serializing Object to and From SQLite DB
===

Can you take a look at this code and see if it's okay?  When I have to do something complicated like this, I scour Google and piece together code like Frankenstein's Monster.  It works.  I'm a little concerned about how I put the byte array back together for deserialization.  Thanks to all who post code examples to the Internet!

	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using System.IO;
	using System.Data;
	using System.Data.SQLite;
	using System.Runtime.Serialization;
	using System.Runtime.Serialization.Formatters.Binary;


	namespace SQLitePhoto
	{
	    class Program
	    {
		static void Main(string[] args)
		{
		    Employee mp = new Employee();
		    mp.EmpId = 10014;
		    mp.EmpName = "J. Omkumar";

		    if (File.Exists("test.db3"))
		    {
			File.Delete("test.db3");
		    }
		    using (var connection = new SQLiteConnection("Data Source=test.db3;Version=3"))
		    using (var command = new SQLiteCommand("CREATE TABLE PHOTOS(ID INTEGER PRIMARY KEY AUTOINCREMENT, PHOTO BLOB)", connection))
		    {
			connection.Open();
			command.ExecuteNonQuery();

			byte[] arData;
			using (MemoryStream stream = new MemoryStream())
			{
			    BinaryFormatter formatter = new BinaryFormatter();
			    formatter.Serialize(stream, mp);
			    arData = stream.ToArray();
			    stream.Close();
			}
			//byte[] photo = new byte[] { 1, 2, 3, 4, 5 };

			command.CommandText = "INSERT INTO PHOTOS (PHOTO) VALUES (@photo)";
			command.Parameters.Add("@photo", DbType.Binary, 20).Value = arData;
			command.ExecuteNonQuery();

			mp = null;

			command.CommandText = "SELECT PHOTO FROM PHOTOS WHERE ID = 1";
			using (var reader = command.ExecuteReader())
			{
			    MemoryStream memStream = new MemoryStream();
			    BinaryFormatter binForm = new BinaryFormatter();
			    while (reader.Read())
			    {
				byte[] buffer = GetBytes(reader);
				memStream.Write(buffer, 0, buffer.Length);
			    }
			    memStream.Seek(0, SeekOrigin.Begin);
			    mp = (Employee)binForm.Deserialize(memStream);
			    memStream.Close();


			    Console.WriteLine("Employee Id: {0}", mp.EmpId.ToString());
			    Console.WriteLine("Employee Name: {0}", mp.EmpName);
			    Console.ReadLine();
			}

		    }
		}

		static byte[] GetBytes(SQLiteDataReader reader)
		{
		    const int CHUNK_SIZE = 2 * 1024;
		    byte[] buffer = new byte[CHUNK_SIZE];
		    long bytesRead;
		    long fieldOffset = 0;
		    using (MemoryStream stream = new MemoryStream())
		    {
			while ((bytesRead = reader.GetBytes(0, fieldOffset, buffer, 0, buffer.Length)) > 0)
			{
			    stream.Write(buffer, 0, (int)bytesRead);
			    fieldOffset += bytesRead;
			}
			return stream.ToArray();
		    }

		}

		[Serializable()]	//Set this attribute to all the classes that you define to be serialized
		public class Employee : ISerializable
		{
		    public int EmpId;
		    public string EmpName;

		    //Default constructor
		    public Employee()
		    {
			EmpId = 0;
			EmpName = null;
		    }

		    //Deserialization constructor.
		    public Employee(SerializationInfo info, StreamingContext ctxt)
		    {
			//Get the values from info and assign them to the appropriate properties
			EmpId = (int)info.GetValue("EmployeeId", typeof(int));
			EmpName = (String)info.GetValue("EmployeeName", typeof(string));
		    }

		    //Serialization function.
		    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
		    {
			//You can use any custom name for your name-value pair. But make sure you
			// read the values with the same name. For ex:- If you write EmpId as "EmployeeId"
			// then you should read the same with "EmployeeId"
			info.AddValue("EmployeeId", EmpId);
			info.AddValue("EmployeeName", EmpName);
		    }
		}



	    }

	}

