Codeplex Source: https://sqlitewindowsphone.codeplex.com/releases
1. Create a database
Step 1: Create a Windows Phone application, and select wpos 7.1 for the target platform.TestSQLite)
Step 2: Add Community. CsharpSqlite. WP. dll reference in the compressed package:
(Reference, right-click and choose --> Add reference ...)
Step 3: add four buttons: Pay attention to naming (btnOpen creates and opens the database, btnPopulate creates the table, btnClear clears the data, btnClose, close the connection)
Step 4: Add a reference to SQLite:
Using SQLiteClient;
Step 5: add the SQLite database connection variable:
SQLiteConnection mySQLiteDB = null;
Public partial class MainPage: PhoneApplicationPage {SQLiteConnection mySQLiteDB = null; // constructor public MainPage () {InitializeComponent ();}
Step 6: add an event to the "Open" button to create and Open the database:
private void btnOpen_Click(object sender, RoutedEventArgs e) 2 3 { 4 5 if (mySQLiteDB == null) 6 7 { 8 9 mySQLiteDB = new SQLiteConnection("TestSQLiteDB");10 11 mySQLiteDB.Open();12 13 14 15 btnOpen.IsEnabled = false;16 17 btnClose.IsEnabled = true;18 19 btnClear.IsEnabled = false;20 21 btnPopulate.IsEnabled = true;22 23 }24 25 }
Step 7: Create a table and fill in data in the table:
Create a data table and add the data private void btnPopulate_Click (object sender, RoutedEventArgs e) {// create the RegisteredStudents table with three attributes: id, name, and student id SQLiteCommand cmd = mySQLiteDB. createCommand ("Create table RegisteredStudents (id int primary key, name text, zipcode numeric (7)"); int I = cmd. executeNonQuery (); int id = 0; string name = "Name" + id; int zip code = 98000; for (int j = 0; j <10; j ++) {id ++; name = "Name" + id; zipcode = 98000 + id; cmd. commandText = "Insert into RegisteredStudents (id, name, zipcode) values (" + id + ", \" "+ name +" \ "," + zipcode + ")"; I = cmd. executeNonQuery ();} btnPopulate. isEnabled = false; btnClear. isEnabled = true ;}
Step 8: Clear the data in the table:
Clear the table data private void btnClear_Click (object sender, RoutedEventArgs e) {SQLiteCommand cmd = mySQLiteDB. createCommand ("drop table RegisteredStudents"); int I = cmd. executeNonQuery (); btnPopulate. isEnabled = true; btnClear. isEnabled = false ;}
Step 9: disconnect the database and close the database:
Disconnect and close the database private void btnClose_Click (object sender, RoutedEventArgs e) {if (mySQLiteDB! = Null) {mySQLiteDB. Dispose (); mySQLiteDB = null; btnOpen. IsEnabled = true; btnPopulate. IsEnabled = false; btnClear. IsEnabled = false; btnClose. IsEnabled = false ;}}
Run the program. Click open to create a database named "TestSQLiteDB" in the independent storage space of the WP simulator. Click populate to fill the database with data. Click clear to clear the data in the database, close to close the database connection;