usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SQLite;usingSystem.Data.Common;usingSystem.IO;namespacesqlitedemo{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } stringSqlitepath = Application.startuppath +"/test.db"; System.Data.SQLite.SQLiteConnection Conn=NewSystem.Data.SQLite.SQLiteConnection (); System.Data.SQLite.SQLiteCommand cmd=NewSystem.Data.SQLite.SQLiteCommand (); stringSQL; /// <summary> ///Create a database/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidBtncreat_click (Objectsender, EventArgs e) { if(File.exists (Sqlitepath)) {MessageBox.Show ("database already exists","Error", Messageboxbuttons.ok,messageboxicon.error); } Else { Try { //Create a database fileSystem.Data.SQLite.SQLiteConnection.CreateFile (Sqlitepath); } Catch(Exception ex) {MessageBox.Show ("failed to create database"+Ex. ToString ()); } } } Private voidBtnconnect_click (Objectsender, EventArgs e) { //connecting to a database Try{System.Data.SQLite.SQLiteConnectionStringBuilder connstr=NewSystem.Data.SQLite.SQLiteConnectionStringBuilder (); ConnStr. DataSource=Sqlitepath; //ConnStr. Password = "admin";//set Password, SQLite ADO. NET implements the database password protectionConn. ConnectionString =ConnStr. ToString (); Conn. Open (); Cmd. Connection=Conn; } Catch(Exception ex) {MessageBox.Show ("connecting to a database"+Ex. ToString ()); } } Private voidBtncreattable_click (Objectsender, EventArgs e) { Try { //Create a tablesql ="CREATE TABLE Test (username varchar, password varchar )"; Cmd.commandtext=SQL; Cmd. Connection=Conn; Cmd. ExecuteNonQuery (); } Catch(Exception ex) {MessageBox.Show ("failed to create table"+Ex. ToString ()); } } Private voidbtnAdd_Click (Objectsender, EventArgs e) { Try{SQL="INSERT INTO Test (Username,password) VALUES ('"+"123"+"', '"+"456"+"')"; Cmd.commandtext=SQL; Cmd. ExecuteNonQuery (); } Catch(Exception ex) {MessageBox.Show ("Increase Failure"+Ex. ToString ()); } } Private voidBtnselect_click (Objectsender, EventArgs e) { Try{listView1.Items.Clear (); SQL="SELECT * FROM Test"; Cmd.commandtext=SQL; System.Data.SQLite.SQLiteDataReader Reader=cmd. ExecuteReader (); while(reader. Read ()) {ListViewItem LVI=NewListViewItem (); Lvi. Text= (ListView1.Items.Count +1). ToString (); Lvi. SubItems.Add (reader. GetString (0)); Lvi. SubItems.Add (reader. GetString (1)); LISTVIEW1.ITEMS.ADD (LVI); } reader. Close (); } Catch(Exception ex) {MessageBox.Show ("Read failed"+Ex. ToString ()); } } Private voidBtndeleteall_click (Objectsender, EventArgs e) {SQL="Delete from Test"; Cmd.commandtext=SQL; Cmd. ExecuteNonQuery (); } Private voidBtndelete_click (Objectsender, EventArgs e) {SQL="DELETE from test WHERE username = '"+textbox1.text.trim () +"'"; Cmd.commandtext=SQL; Cmd. ExecuteNonQuery (); } }}
SQLite Create and delete changes