win8 应用商店程序使用SQLITE数据库

http://www.cnblogs.com/zhuzhenyu/archive/2012/11/27/2790193.html

using SQLite;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍

namespace beelinechinese
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class sqldemo : Page
    {
        public sqldemo()
        {
            this.InitializeComponent();
        }
        
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            using (var db = CreateSQLiteConnection())
            {

                //创建表

                db.CreateTable<Person>();



            }
        }

        private SQLiteConnection CreateSQLiteConnection()
        {

            //数据文件保存的位置

            var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db3.sqlite");

            //打开创建数据库和表

          return new SQLite.SQLiteConnection(dbPath);
            }
   
        private void dgfgf_Click(object sender, RoutedEventArgs e)
        {
          
            var db =CreateSQLiteConnection();

        
            //单条插入语句

            db.Insert(new Person() { ID=System.Guid.NewGuid().ToString(), FirstName = "liufei", LastName = "Sky" });
            db.Close();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var db = CreateSQLiteConnection();
            List<object> list = db.Query(new TableMapping(typeof(Person)), "select *  from  Person");
            db.Close();
            List<Person> ml = list.Cast<Person>().ToList();
            txtmsg.Text = ml[0].FirstName;
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            try
            {
                var db = CreateSQLiteConnection();
           //     db.Delete<Person>(new Person() { FirstName = "lisa" });

                SQLiteCommand cmd = db.CreateCommand("delete from  person where FirstName = 'lisa'");

                cmd.ExecuteNonQuery();
              db.Close(); }
            catch (Exception ex)
            {
                txtmsg.Text = ex.ToString();
            }
          
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            var db = CreateSQLiteConnection();
            SQLiteCommand cmd = db.CreateCommand("update person set FirstName='lisa'where FirstName='liufei'");

            cmd.ExecuteNonQuery();
            db.Close();
        }
    }

  public  class Person
    {

        [SQLite.PrimaryKey]

        public string ID { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }
        public DateTime LastLogin { get; set; }

        public string NickName { get; set; }

        public bool ShowPic { get; set; }


    }
}
using SQLite;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍

namespace beelinechinese
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class sqldemo : Page
    {
        public sqldemo()
        {
            this.InitializeComponent();
        }
        
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            using (var db = CreateSQLiteConnection())
            {

                //创建表

                db.CreateTable<Person>();



            }
        }

        private SQLiteConnection CreateSQLiteConnection()
        {

            //数据文件保存的位置

            var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db3.sqlite");

            //打开创建数据库和表

          return new SQLite.SQLiteConnection(dbPath);
            }
   
        private void dgfgf_Click(object sender, RoutedEventArgs e)
        {
          
            var db =CreateSQLiteConnection();

        
            //单条插入语句

            db.Insert(new Person() { ID=System.Guid.NewGuid().ToString(), FirstName = "liufei", LastName = "Sky" });
            db.Close();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var db = CreateSQLiteConnection();
            List<object> list = db.Query(new TableMapping(typeof(Person)), "select *  from  Person");
            db.Close();
            List<Person> ml = list.Cast<Person>().ToList();
            txtmsg.Text = ml[0].FirstName;
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            try
            {
                var db = CreateSQLiteConnection();
           //     db.Delete<Person>(new Person() { FirstName = "lisa" });

                SQLiteCommand cmd = db.CreateCommand("delete from  person where FirstName = 'lisa'");

                cmd.ExecuteNonQuery();
              db.Close(); }
            catch (Exception ex)
            {
                txtmsg.Text = ex.ToString();
            }
          
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            var db = CreateSQLiteConnection();
            SQLiteCommand cmd = db.CreateCommand("update person set FirstName='lisa'where FirstName='liufei'");

            cmd.ExecuteNonQuery();
            db.Close();
        }
    }

  public  class Person
    {

        [SQLite.PrimaryKey]

        public string ID { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }
        public DateTime LastLogin { get; set; }

        public string NickName { get; set; }

        public bool ShowPic { get; set; }


    }
}