Re: Minimalist, data bound fruit basket with SQLite.NET

  • From: "Octavian Rasnita" <orasnita@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Thu, 6 Dec 2007 17:25:16 +0200

Hi,

But I saw a .mdb file in the archive, and not a sqlite file.

I was able to open it with MS Access, and without any password at all.
Am I missing something?

Octavian

----- Original Message ----- From: "Jamal Mazrui" <empower@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Thursday, December 06, 2007 4:06 PM
Subject: Minimalist, data bound fruit basket with SQLite.NET


I have updated the archive at
http://www.EmpowermentZone.com/db_fruit.zip
to include a minimalist C# 3.0 version with basic security -- also pasted
below.  Two files are required to run the program (besides .NET 2.0):
sqlt_fruit.exe -- about 8K and System.Data.SQLite.dll -- about 670K.

Jamal

/*
content of sqlt_fruit.cs
Fruit Basket program in C# 3.0 with SQLite.NET
Public domain by Jamal Mazrui
*/

// Import namespaces
using System;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Windows.Forms;

// Define class
class FruitBasket {

// Define entry point of program
static void Main() {
// Define names of database file, table, and column
var sDb = Path.Combine(Application.StartupPath, "sqlt_fruit.db");
var sTable = "Basket";
var sColumn = "Fruit";

// Define connection
var sConnect = "Data Source=" + sDb;
using (var connect = new SQLiteConnection(sConnect)) {

// Create database if not found
if (!File.Exists(sDb)) {
connect.Open();
// Protect with password
connect.ChangePassword("MyCode");
var sCreate = "create table " + sTable + " (ID integer primary key
autoincrement, " + sColumn + " text);";
var createCommand = new SQLiteCommand(sCreate, connect);
createCommand.ExecuteNonQuery();
connect.Close();
} // if database exists

// Provide password
connect.SetPassword("MyCode");
connect.Open();
// Define table adapter
var sSelect = "select * from " + sTable;
var selectCommand = new SQLiteCommand(sSelect, connect);
var adapter = new SQLiteDataAdapter(selectCommand);
var builder = new SQLiteCommandBuilder(adapter);
var table = new DataTable(sTable);
adapter.Fill(table);

// Create controls;
var tlp = new TableLayoutPanel {ColumnCount = 3, RowCount = 2};
var lblFruit = new Label {Text = "&Fruit:", Parent = tlp};
var txtFruit = new TextBox {Parent = tlp};
var btnAdd = new Button {Text = "&Add", Parent = tlp};
var lblBasket = new Label {Text = "&Basket:", Parent = tlp};
var lstBasket = new ListBox {DataSource = table, DisplayMember = sColumn,
Parent = tlp};
var btnDelete = new Button {Text = "&Delete", Parent = tlp};

// Define Add event handler;
btnAdd.Click += (o, e) => {
var sFruit = txtFruit.Text.Trim();
if (sFruit.Length == 0) MessageBox.Show("No fruit to add!", "Alert");
else {
table.Rows.Add(null, sFruit);
adapter.Update(table);
lstBasket.SelectedIndex = lstBasket.Items.Count - 1;
txtFruit.Clear();
} // if fruit to add
}; // add

// Update ID in table to unique one generated by database
adapter.RowUpdated += (o, e) => {
if(e.StatementType != StatementType.Insert) return;

var sTransfer = "select last_insert_rowid()";
var transferCommand = new SQLiteCommand(sTransfer, connect);
e.Row["ID"] = transferCommand.ExecuteScalar( );
}; // update

// Define Delete event handler;
btnDelete.Click += (o,e) => {
var iFruit = lstBasket.SelectedIndex;
if (iFruit == -1) MessageBox.Show("No fruit to delete!", "Alert");
else {
table.Rows[iFruit].Delete();
//using (var transact = connect.BeginTransaction()) {
adapter.Update(table);
//}
if (iFruit == lstBasket.Items.Count) iFruit--;
lstBasket.SelectedIndex = iFruit;
} // if fruit to delete
}; // delete

// Finalize dialog;
var dlg = new Form {Text = "Fruit Basket", AcceptButton = btnAdd,
StartPosition = FormStartPosition.CenterScreen, AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink};
dlg.Controls.Add(tlp);
// Define closing event handler
dlg.Closing += (o, e) => e.Cancel = (MessageBox.Show("Close program?",
"Confirm", MessageBoxButtons.YesNo) == DialogResult.No);
dlg.ShowDialog();
connect.Close();
} // using connect
} // Main method
} // FruitBasket class

// End of sqlt_fruit.cs

__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: