1. Take the following steps to use the SQLite database in Metro:
1) download SQLite for WinRT
Address: http://www.sqlite.org/download.html
Download Precompiled Binaries for Windows Runtime, which is an extension of Visual Studio. The file is suffixed with vsix and you can simply double-click it to run it. (For example)
2). Add reference for the project
Create a project, select reference> Add reference in solution, select Windows> extension in the list on the left of reference manager, and then select as shown in the list on the right:
Note: Choose SQLite for Windows Runtime and Microsoft Visual C ++ Runtime Package
3) Add a C # driver to the Project
In solution, right-click a project and choose manage NuGet packages from the shortcut menu to perform the following operations in the Manager:
After the installation is complete, there will be two more files in the root directory of your project: SQLite. cs and SQLiteAsync. cs. We can use these two classes to operate SQLite.
2. Create a database
1). First, declare a MemberInfoClass, that is, automatic growth of Table Primary keys
Copy codeThe Code is as follows: public class MemberInfo
{
[SQLite. AutoIncrement, SQLite. PrimaryKey]
Public int ID {set; get ;}
Public string Name {set; get ;}
Public int Age {set; get ;}
Public string Address {set; get ;}
}
2) Write a method to create the database Member. sqlite and the table MemberInfo.
Copy codeThe Code is as follows :{
String path = Path. Combine (Windows. Storage. ApplicationData. Current. LocalFolder. Path, "Member. sqlite"); // location where the data file is saved
Using (var db = new SQLite. SQLiteConnection (path) // open the create database and table
{
Db. CreateTable <MemberInfo> ();
}
}
3). Simple operations on the sqlite database (add, delete, modify, and query)
Copy codeThe Code is as follows: public void Insert (MemberInfo data)
{
Try
{
Using (var db = newSQLiteConnection (path ))
{
Db. Insert (data );
}
}
Catch (Exception e)
{
Throw e;
}
}
Publicvoid Delete (int id)
{
Try
{
T data = Select (id );
Using (var db = newSQLiteConnection (path ))
{
Db. Delete (data );
}
}
Catch (Exception e)
{
Throw e;
}
}
Public void Insert (T data)
{
Try
{
Using (var db = newSQLiteConnection (path ))
{
Db. Insert (data );
}
}
Catch (Exception e)
{
Throw e;
}
}
Publicvoid Delete (int id)
{
Try
{
T data = Select (id );
Using (var db = newSQLiteConnection (path ))
{
Db. Delete (data );
}
}
Catch (Exception e)
{
Throw e;
}
}
Public MemberInfo Select (int id)
{
Try
{
MemberInfo data = null;
Using (var db = newSQLiteConnection (path ))
{
List <object> obj = db. Query (newTableMapping (typeof (MemberInfo), string. Format ("Select * from MemberInfo where ID = {0}", id ));
If (obj! = Null & obj. Count> 0)
{
Data = obj [0] as MemberInfo;
}
}
Return data;
}
Catch (Exception e)
{
Throw e;
}
}
Publicvoid Updata (MemberInfo data)
{
Try
{
Using (var db = newSQLiteConnection (path ))
{
Db. Update (data );
}
}
Catch (Exception e)
{
Throw e;
}
}
PublicObservableCollection <MemberInfo> SelectAll ()
{
ObservableCollection <MemberInfo> list = newObservableCollection <MemberInfo> ();
Using (var db = newSQLiteConnection (path ))
{
List <object> query = db. Query (newTableMapping (typeof (MemberInfo), "select * from MemberInfo ");
Foreach (var mem in query)
{
MemberInfo info = mem asMemberInfo;
List. Add (info );
}
}
Return list;
}