Windows Phone 學習 創建和使用數據庫

   using (MyDataContex dc = new MyDataContex())
            {
                if (dc.DatabaseExists() == false)
                {
                    dc.CreateDatabase();
                    System.Diagnostics.Debug.WriteLine(DateTime.Now.ToLongTimeString() + " 數據庫創建完成。");
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine(DateTime.Now.ToLongTimeString() + " 數據庫已經存在。");
                }
            }
 public class TestEntity : INotifyPropertyChanging, INotifyPropertyChanged
    {
        private string _cityName;

        /// <summary>
        /// 公共屬性,對應於表中的列
        /// </summary>
        [Column]
        public string CityName
        {
            get
            {
                return this._cityName;
            }
            set
            {
                if (this._cityName != value)
                {
                    OnPropertyChanging("CityName");
                    this._cityName = value;
                    OnPropertyChanged("CityName");
                }
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;
        // 引發PropertyChanging事件
        protected void OnPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        // 引發PropertyChanged事件
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    /// <summary>
    /// 從DataContext派生的類
    /// </summary>
    public class MyDataContex:DataContext
    {
        /// <summary>
        /// 連接字符串
        /// </summary>
        public const string CONNECT_STRING = "Data Source='isostore:/db.sdf';Password='123456'";

        /// <summary>
        /// 構造函數
        /// </summary>
        public MyDataContex()
            : base(CONNECT_STRING)
        {

        }

        /// <summary>
        /// 公共屬性,對應於數據庫中的表
        /// </summary>
        public Table<TestEntity> MyTestEntity;
    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章