Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Oct 24th, 2010  |  syntax: C#  |  size: 2.48 KB  |  views: 255  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Data.SQLite;
  11.  
  12. namespace SmartDeviceProject1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private string dbName = "test.db";
  22.         private string dbPassword = "testPa55word";
  23.         private SQLiteConnection cnn;
  24.  
  25.         private void Form1_Load(object sender, EventArgs e)
  26.         {
  27.             if (!File.Exists(dbName))
  28.             {
  29.                 // Opens an unencrypted database
  30.                 SQLiteConnection.CreateFile(dbName);
  31.                 cnn = new SQLiteConnection("Data Source=|DataDirectory|" + dbName);
  32.                 cnn.Open();
  33.  
  34.                 // Create initial table structure
  35.                 StringBuilder query = new StringBuilder();
  36.                 query.Append("CREATE TABLE \"test\" (");
  37.                 query.Append("\"ID\" INTEGER, ");
  38.                 query.Append("\"TEXT\" TEXT");
  39.                 query.Append(");");
  40.                 using (SQLiteCommand cmd = new SQLiteCommand(query.ToString(), cnn))
  41.                 {
  42.                     cmd.ExecuteNonQuery();
  43.                 }
  44.  
  45.                 // Encrypts the database
  46.                 cnn.ChangePassword(dbPassword);
  47.             }
  48.             else
  49.             {
  50.                 // Opens an encrypted database
  51.                 cnn = new SQLiteConnection("Data Source=|DataDirectory|" + dbName + ";Password=" + dbPassword);
  52.                 cnn.Open();
  53.             }
  54.         }
  55.  
  56.         private void addButton_Click(object sender, EventArgs e)
  57.         {
  58.             using (SQLiteCommand cmd = new SQLiteCommand("INSERT INTO \"test\" VALUES("+ int.Parse(id.Value.ToString()) +", \"" + text.ToString() + "\")", cnn))
  59.             {
  60.                 cmd.ExecuteNonQuery();
  61.             }
  62.         }
  63.  
  64.         private void reloadDataButton_Click(object sender, EventArgs e)
  65.         {
  66.             using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM \"test\"", cnn))
  67.             {
  68.                 using (SQLiteDataReader sdr = cmd.ExecuteReader())
  69.                 {
  70.                     while (sdr.Read())
  71.                     {
  72.                         listBox.Items.Add(sdr.GetValue(0) + " - " + sdr.GetValue(1));
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.     }
  78. }
clone this paste RAW Paste Data