Advertisement
Guest User

Untitled

a guest
May 24th, 2013
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.89 KB | None | 0 0
  1.     class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             CreateDatabase("test.db", @"d:\temp\linda.jpg");
  6.         }
  7.     }
  8.  
  9. // dumps this:
  10. // SQLite version: 3.7.17
  11. // System.Data.SQLite version: System.Data.SQLite, Version=1.0.86.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139
  12. // Size of file: 1057881
  13. // Size of db: 1041408
  14.  
  15.  
  16.         static void CreateDatabase(string dbPath, string imagePath)
  17.         {
  18.             if (File.Exists(dbPath))
  19.             {
  20.                 File.Delete(dbPath);
  21.             }
  22.  
  23.             using (System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection("data source=" + dbPath))
  24.             {
  25.                 conn.Open();
  26.                 var cmd = conn.CreateCommand();
  27.                 cmd.CommandText = "SELECT sqlite_version()";
  28.                 Console.WriteLine("SQLite version: " + cmd.ExecuteScalar());
  29.                 Console.WriteLine("System.Data.SQLite version: " + conn.GetType().Assembly.FullName);
  30.  
  31.                 cmd = conn.CreateCommand();
  32.                 cmd.CommandText = "CREATE TABLE IF NOT EXISTS Test(record_id INTEGER PRIMARY KEY ASC, pic)";
  33.                 cmd.ExecuteNonQuery();
  34.                
  35.                 Console.WriteLine("Size of file: " + new FileInfo(imagePath).Length);
  36.                 var photo = new Bitmap(imagePath);
  37.                 var pic = ImageToByte(photo, ImageFormat.Jpeg);
  38.  
  39.                 cmd = conn.CreateCommand();
  40.                 cmd.CommandText = "INSERT INTO Test (record_id, pic) VALUES ('1', @0);";
  41.                 System.Data.SQLite.SQLiteParameter param = new System.Data.SQLite.SQLiteParameter("@0", DbType.Binary);
  42.                 param.Value = pic;
  43.                 cmd.Parameters.Add(param);
  44.                 cmd.ExecuteNonQuery();
  45.             }
  46.             Console.WriteLine("Size of db: " + new FileInfo(dbPath).Length);
  47.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement