Return to Snippet

Revision: 24558
at March 3, 2010 17:38 by pckujawa


Initial Code
Our "SAVE" button click code looks like the following:

private void button1_Click(object sender, EventArgs e)
{
SQLiteTransaction trans;
string SQL = "INSERT INTO PERSONS (ID, FIRSTNAME,LASTNAME,EMAIL,PHONE) VALUES";
SQL += "(@ID, @firstname, @lastname, @email, @phone)";

SQLiteCommand cmd = new SQLiteCommand(SQL);
cmd.Parameters.AddWithValue("@ID", Guid.NewGuid());
cmd.Parameters.AddWithValue("@firstname", this.txtFirst.Text);
cmd.Parameters.AddWithValue("@lastname", this.txtLast.Text);
cmd.Parameters.AddWithValue("@email", this.txtEmail.Text);
cmd.Parameters.AddWithValue("@phone", this.txtPhone.Text);

cmd.Connection = sqLiteConnection1;
sqLiteConnection1.Open();
trans = sqLiteConnection1.BeginTransaction();
int retval = 0;
try
{
retval= cmd.ExecuteNonQuery();
if (retval == 1)
MessageBox.Show("Row inserted!");
else
MessageBox.Show("Row NOT inserted.");
}
catch (Exception ex)
{
trans.Rollback();
}
finally
{
trans.Commit();
cmd.Dispose();
sqLiteConnection1.Close();
}

}

Finally, our "DISPLAY" button click handler code looks like this:

private void button2_Click(object sender, EventArgs e)
{
string SQL = "SELECT * FROM PERSONS";
SQLiteCommand cmd = new SQLiteCommand(SQL);
cmd.Connection = sqLiteConnection1;
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
da.Fill(ds);
DataTable dt = ds.Tables[0];
this.dataGridView1.DataSource = dt;
}
catch (Exception ex)
{

}
finally
{
cmd.Dispose();
sqLiteConnection1.Close();
}

}

Initial URL
http://www.eggheadcafe.com/tutorials/aspnet/20f7912e-6fa7-40eb-b31b-b6f46d4f2c6a/get-started-with-sqlite-a.aspx

Initial Description


Initial Title
Basic insert and select commands for SQLite in .NET

Initial Tags
Net

Initial Language
C#