【WP7】本地資料庫使用

weixin_34391854發表於2012-11-07

在WP7開發中,雖然微軟倡導雲服務,出於流量的考慮,對於某些應用需要使用到本地資料庫,下面演示一下本地資料庫的使用

  通過程式碼建立資料庫

  首先,我們定義一個繼承於DataContext的類,用於操作和訪問資料庫

   先引用名稱空間  System.Data.Linq

        public class MyDataContext:DataContext
        {
            public const string ConnectionString = "Data Source='isostore:/MyDatabase.sdf';Password='123456'";
            public MyDataContext() : base(ConnectionString)
            { }
            public Table<Student> Students;
        }

   定義一個資料庫連線字串(資料庫名為MyDatabase.sdf,密碼為123456,可以不設定密碼),同時定義一個Table,關於Table的定義,下面給出

        [Table]
        public class Student : INotifyPropertyChanged, INotifyPropertyChanging
        { 
            string stuNum;
            string stuName;
            int stuScore;

            public event PropertyChangedEventHandler PropertyChanged;
            public event PropertyChangingEventHandler PropertyChanging;

            protected void OnPropertyChanged(PropertyChangedEventArgs e)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, e);
                }
            }
            protected void OnPropertyChanging(PropertyChangingEventArgs e)
            {
                if (PropertyChanging != null)
                {
                    PropertyChanging(this, e);
                }
            }

            [Column(IsPrimaryKey = true, CanBeNull = false)]
            public string StuNum
            {
                get { return stuNum; }
                set
                {
                    if (this.stuNum != value)
                    {
                        OnPropertyChanging(new PropertyChangingEventArgs("StuNum"));
                        this.stuNum = value;
                        OnPropertyChanged(new PropertyChangedEventArgs("StuNum"));
                    }
                }
            }
            [Column]
            public string StuName
            {
                get { return stuName; }
                set
                {
                    if (this.stuName != value)
                    {
                        OnPropertyChanging(new PropertyChangingEventArgs("StuName"));
                        this.stuName = value;
                        OnPropertyChanged(new PropertyChangedEventArgs("StuName"));
                    }
                }
            }
            [Column]
            public int StuScore
            {
                get { return stuScore; }
                set
                {
                    if (this.stuScore != value)
                    {
                        OnPropertyChanging(new PropertyChangingEventArgs("StuScore"));
                        this.stuScore = value;
                        OnPropertyChanged(new PropertyChangedEventArgs("StuScore"));
                    }
                }
            }
        }

  注意,實體類必須要實現INotifyPropertyChanged介面,最好連同INotifyPropertyChanging介面,這樣可以改善效能。一定要實現這兩個介面,而且要觸發PropertyChanging和PropertyChanged事件,不然你提交到資料庫是不能更新資料的。

  

  關於資料庫和表的定義已經做好了,隔離儲存空間一開始是沒有資料庫,所以需要先建立資料庫,在頁面的建構函式中

        public MainPage()
        {
            using (MyDataContext dc = new MyDataContext())
            {
                if (dc.DatabaseExists() == false)
                {
                    dc.CreateDatabase();
                }
            }  
            InitializeComponent();
        }

    資料庫建立完成後,接下來是對資料庫的操作,增刪改

        using (MyDataContext dc = new MyDataContext())
        {
            if (dc.Students.Where(c => c.StuNum == "2010242").Count() > 0)
            {
                MessageBox.Show("輸入的號碼已經存在。");
                return;
            }
            Student stu = new Student()
            {
                StuNum = "2010242",
                StuName = "Bomo",
                StuScore = "50"
            };
          
            dc.Students.InsertOnSubmit(stu);
            dc.SubmitChanges();
        }

        using (MyDataContext dc = new MyDataContext())
        {
            Students stu = dc.Students.FirstOrDefault(s1 => s1.StuNum == "2010242");
            if (stu != null)
            {
                dc.Students.DeleteOnSubmit(stu);
                dc.SubmitChanges();
            }
        }

        using (MyDataContext dc = new MyDataContext())
        {
            Students stu = dc.Students.FirstOrDefault(s => s.StuNum == "2010242");
            if (stu != null)
            {
                stu.StuName = "Toby";
                stu.StuScore = 90;
                dc.SubmitChanges();
            }
        }

資料庫的操作就說到這裡

 

接下來說說講資料庫繫結到ListBox控制元件中,關於ListBox的模板定義和資料繫結請參考下面連結

http://www.cnblogs.com/bomo/archive/2012/11/07/2758657.html

        using (MyDataContext dc = new MyDataContext())
        {
            var res =
                from s in dc.Students
                select s;
            this.lbItemList.ItemsSource = res.ToList();
        }

這裡是取出資料庫的資料關聯到ListBox控制元件的項中

注意:每次對資料庫的資料進行增刪改操作時都要重新關聯資料才能讓ListBox的顯示更新

例如:

 

        using (MyDataContext dc = new MyDataContext())
        {
            Students stu = dc.Students.FirstOrDefault(s => s.StuNum == "2010242");
            if (stu != null)
            {
                stu.StuName = "Toby";
                stu.StuScore = 90;
                dc.SubmitChanges();
                
                var res =
                    from s in dc.Students
                    select s;
                this.lbItemList.ItemsSource = res.ToList();
            }
        }

 

 

 

 

 

 

 

 

 

 

相關文章