WP7 only supports local storage and does not contain databases. Therefore, to use a database on WP7, you can only use a third-party embedded database. Perst is a simple, fast, and convenient object-oriented Java and. NET database. It is well-known in the field of embedded databases andCodeIs open-source. We can download all the code of the database on its official website.
Official Website www.mcobject.com/perst_eval
The following is a summary of some basic syntaxes used by perst databases on Windows Phone 7:
1. Create a database
Storage storage = storagefactory. instance. createstorage (); // create a perst storage instance
Storage. Open ("perstdemodb. DBs"); // Open Storage
Database DB = new database (storage); // use the storage instance initialized above to create a database
2. Create a database object-oriented class (equivalent to a relational database table)
// The base class of the object-oriented class for creating a database storage must inherit perst. persistent base class
public class user: perst. persistent
{< br> // defines the field
// The value of the object to be obtained using reflection must be marked with [fulltextindexable] before the field.
[fulltextindexable]
Public long ID;
[fulltextindexable]
Public string name;
......
Public long ID
{
Get {return ID ;}
Set {id = value ;}
}
......
Public user (long ID, string name)
{
Id = ID;
Name = Name;
}
Public override void onload ()
{
Base. onload ();
}
// Obtain database objects. Generally, the database is defined in the app.
Protected static database DB
{
Get {return (APP) application. Current). DB ;}
}
Public override void deallocate ()
{
DB. deleterecord (this); // delete a record
}
Public void save ()
{
Store (); // save is equivalent to saving the table
DB. updatefulltextindex (this );
}
}
3. Add record
User user = new user (1, "name ");
DB. addrecord (User );
DB. Storage. Commit ();
4. Modify records
User. ID = 2
User. Save ();
5. delete records
User. deallocate ();
DB. Storage. Commit ();
6. query the database
the query record OID based on the unique oid is a unique value allocated by the perst database for each class object.
User user = dB. select ("oid =" + this. navigationcontext. querystring ["oid"]). firstordefault ();
Fuzzy search
// Query all results that contain tbsearch. Text. tolower () fulltextsearchresult
Fulltextsearchresult prefixes = dB. searchprefix (tbsearch. Text. tolower (), 1000,400 0, false );
Observablecollection <user> searchusers = new observablecollection <user> ();
List <fulltextsearchhit> arrayres = new list <fulltextsearchhit> ();
If (prefixes! = NULL) arrayres. addrange (prefixes. Hits );
Foreach (VAR hit in arrayres)
{
If (hit. Document is user) // if the contact type is fulltextsearchhit. Document, query the matched files.
{
If (! Searchcontacts. Contains (User) hit. Document ))
Searchcontacts. Add (User) hit. document );
}
}
7. Delete all objects of the stored class
DB. droptable (typeof (User ));
DB. Storage. Commit (); // complete
8. delete a database
VaR storage = (APP) app. Current). DB. Storage; // obtain the database storage defined on the app
Storage. Close (); // close it
Using (VAR store = isolatedstoragefile. getuserstoreforapplication () // obtain the current applicationProgramLocal storage file used
{
If (store. fileexists ("perstdemodb. DBs") // find the database storage file perst the database file is stored locally
{
Store. deletefile ("perstdemodb. DBs"); // Delete the local storage file of this database
}
}
Introduction to perst Embedded Database