Implementation code of connecting WP to SQLite database

Source: Internet
Author: User
Tags sqlite sqlite database
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;

The code is as follows: Copy code

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:

Open button click event

The code is as follows: Copy code
Private void btnOpen_Click (object sender, RoutedEventArgs e)
 
         {
 
If (mySQLiteDB = null)
 
             {
 
MySQLiteDB = new SQLiteConnection ("TestSQLiteDB ");
 
MySQLiteDB. Open ();
 
 
 
BtnOpen. IsEnabled = false;
 
BtnClose. IsEnabled = true;
 
BtnClear. IsEnabled = false;
 
BtnPopulate. IsEnabled = true;
 
             }
 
         }

Step 7: create a table and fill in data in the table:

Create a data table and add data

The code is as follows: Copy code

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 zipcode= 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 table data

The code is as follows: Copy code

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

The code is as follows: Copy code

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;
 
            }
 
        }

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;

2. Get database files,
Step 1: Double-click the WindowsPhonePowerTools. application file,
           
Wait a moment and the following interface appears. There are three options: Select (MB), select the first option for the real machine, and then click CONNECT.
           
Step 2: Click CONNECT: The following page appears: BROWSEClick to select the xap file under the Windows Phone project you created
(Xap under Debug after compilation, for example, my XAP location: C: UsersRYDocumentsVisual Studio 2010 ProjectsFirstWPTestSQLiteBinDebug)
Step 3: click INSTALLWait for a moment. Here, I got 2, I thought I would click INSTALLOK,
You actually need to click :,
The following figure is displayed:... O (& cap; _ & cap;) O haha ~ You see it --> TestSQLite,
              
Click it to see the database... TestSQLiteDB, Click GETButton to save the database file to the desktop (note: If you are still in the debugging status, stop debugging at this time; otherwise, an error will be reported ,..),
3. Operate databases
Install SQLiteManagerSetup tool
After installation, double-click and then click Use Demo--> ContinuE. Find TestSQLiteDB, Open:
          
Above, this is the database you created. Now you can operate on it ....
 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.