- 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)";
- 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";
- cmd.Connection = sqLiteConnection1;
- try
- {
- da.Fill(ds);
- DataTable dt = ds.Tables[0];
- this.dataGridView1.DataSource = dt;
- }
- catch (Exception ex)
- {
- }
- finally
- {
- cmd.Dispose();
- sqLiteConnection1.Close();
- }
- }
Wednesday, 7 July 2010
BASIC INSERT AND SELECT COMMANDS FOR SQLITE IN .NET
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment