Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Data.SQLite;
- namespace SmartDeviceProject1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private string dbName = "test.db";
- private string dbPassword = "testPa55word";
- private SQLiteConnection cnn;
- private void Form1_Load(object sender, EventArgs e)
- {
- if (!File.Exists(dbName))
- {
- // Opens an unencrypted database
- SQLiteConnection.CreateFile(dbName);
- cnn = new SQLiteConnection("Data Source=|DataDirectory|" + dbName);
- cnn.Open();
- // Create initial table structure
- StringBuilder query = new StringBuilder();
- query.Append("CREATE TABLE \"test\" (");
- query.Append("\"ID\" INTEGER, ");
- query.Append("\"TEXT\" TEXT");
- query.Append(");");
- using (SQLiteCommand cmd = new SQLiteCommand(query.ToString(), cnn))
- {
- cmd.ExecuteNonQuery();
- }
- // Encrypts the database
- cnn.ChangePassword(dbPassword);
- }
- else
- {
- // Opens an encrypted database
- cnn = new SQLiteConnection("Data Source=|DataDirectory|" + dbName + ";Password=" + dbPassword);
- cnn.Open();
- }
- }
- private void addButton_Click(object sender, EventArgs e)
- {
- using (SQLiteCommand cmd = new SQLiteCommand("INSERT INTO \"test\" VALUES("+ int.Parse(id.Value.ToString()) +", \"" + text.ToString() + "\")", cnn))
- {
- cmd.ExecuteNonQuery();
- }
- }
- private void reloadDataButton_Click(object sender, EventArgs e)
- {
- using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM \"test\"", cnn))
- {
- using (SQLiteDataReader sdr = cmd.ExecuteReader())
- {
- while (sdr.Read())
- {
- listBox.Items.Add(sdr.GetValue(0) + " - " + sdr.GetValue(1));
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement