Windows 8 Metro uses C # To connect to SQLite and create databases, and add, delete, modify, and query data tables.

Source: Internet
Author: User

  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;

}

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.