Windows Phone (2) WP7 database connection (SQLite database)

Source: Internet
Author: User
Tags visual studio 2010

This article is a collection of blog posts from arcgis_mobile and Jin Yanyun:

Address: http://blog.csdn.net/arcgis_mobile/article/details/7597519

References

This article is a brief WP7 database connection (SQLite database) operation process, illustrated in Chinese, adapted to a variety of people, O (∩) O ~...

Preparation: Download my compressed package http://115.com/file/an0tifop#SQLite.rar (115 Network Disk temporarily exist, open the link directly click "normal download" OK)

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:

Open button click event

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

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: \ Users \ ry \ Documents ents \ Visual Studio 2010 \ projects \ firstwp \ testsqlite \ bin \ Debug)

Step 3: ClickInstallWait for a moment. Here, I got 2, I thought I would clickInstallOK,

You actually need to click :,

Then, the following error occurs:... O (∩ _ ∩) O Haha ~ You see it -->Testsqlite,

Click it to see the database...Testsqlitedb, ClickGetButton 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

InstallSqlitemanagersetup Tool

After installation, double-click and then clickUse demo-->ContinuE. FindTestsqlitedb, Open:

Above, this is the database you created. Now you can operate on it ....

4. Add some SQLite stuff:

1. SQLite data type: No data type (typelessness) means that any data type can be saved, but it is best to add the data type to facilitate maintenance.
2. SQL statement:
Create a table:
Create students (ID int, Name text, age INT)
Insert data:
Insert into students (ID, name, age) values (3, "ee", 23 );
Insert into students (ID, name, age) values (4, "AA", 24 );
Insert into students (ID, name, age) values (5, "VV", 25 );
Data Query:
Select * from students where name = 'ee 'order by ID

If you have any questions, please leave me a message. O (∩ _ ∩) O thank you!

Welcome to all kinds of bricks,... O (solid _ blocks) O Haha ~...

 

 

 

 

 

 

 

 

 

 

 

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.