操作本地數據庫

1.創建實體 注意加Table和Column特性

 /// <summary>
    /// 課程類
    /// </summary>
    [Table] //表示類將成爲一個table
    public class Course : INotifyPropertyChanged, INotifyPropertyChanging
    {
        [Column(IsVersion = true)] //table的列
        private Binary _version;

        private int _id;
        [Column(IsPrimaryKey=true,IsDbGenerated=true)] //table的列,主鍵,自動生成
        public int Id
        {
            get { return _id; }
            set 
            {
                if (_id != value)
                {
                    RaiseProtertyChanging("Id");
                    _id = value;
                    RaisePropertyChanged("Id");
                }
            }
        }

        private string _name;
        [Column]
        public string Name
        {
            get { return _name; }
            set
            {
                if (_name != value)
                {
                    RaiseProtertyChanging("Name");
                    _name = value;
                    RaisePropertyChanged("Name");
                }
            }
        }

        private string _location;
        [Column]
        public string Location
        {
            get { return _location; }
            set
            {
                if (_location != value)
                {
                    RaiseProtertyChanging("Location");
                    _location = value;
                    RaisePropertyChanged("Location");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;

        private void RaiseProtertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }
    }
2.創建類繼承DataContext,而且封裝this.GetTable<Course>()方法,否則創建數據庫時報錯

 public class MyDataContext : DataContext
    {
        //連接字符竄
        public const string ConnectionString = "Data Source=isostore:/MyDb.sdf";

        //構造函數
        public MyDataContext()
            : base(ConnectionString)
        {
            if (!this.DatabaseExists())
            {
                //創建數據庫
                this.CreateDatabase();
            }
        }
        //必須存在,否則創建數據庫報錯:DataContext不存在表
        public Table<Course> CourseTable
        {
            get { return this.GetTable<Course>(); }
        }
    }

3.頁面前臺代碼

<!--ContentPanel - 在此處放置其他內容-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0" Orientation="Horizontal">
                <Button Content="添加" Click="Button_Click" />
                <Button Content="編輯" Click="Button_Click_1" />
                <Button Content="刪除" Click="Button_Click_2" />
            </StackPanel>
            <ListBox Grid.Row="1" Name="CourseList">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Column="0" Text="{Binding Id,Mode=TwoWay}"/>
                            <TextBlock Grid.Column="1" Text="{Binding Name,Mode=TwoWay}"/>
                            <TextBlock Grid.Column="2" Text="{Binding Location,Mode=TwoWay}"/>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
               
            </ListBox>
        </Grid>

4.頁面後臺代碼

添加:調用GetTable<T>().InsertOnSubmit(T)方法,最後調用SubmitChanges()向數據庫提交數據。

刪除:  GetTable<T>().DeleteOnSubmit(T)方法,最後調用SubmitChanges()向數據庫提交數據。

編輯:調用SubmitChanges()

頁面綁定的數據源必須是ObservableCollection類型

 public partial class MainPage : PhoneApplicationPage
    {
        private ObservableCollection<Course> Courses;
        private DataContext _data;
        // 構造函數
        public MainPage()
        {
            InitializeComponent();
            //創建數據庫
            //CreateDatabase();
            _data = new MyDataContext();
            //初始化數據
            InitData();
        }

        private void CreateDatabase()
        {
           _data = new MyDataContext();
            if (!_data.DatabaseExists())
            {
                _data.CreateDatabase();
            }
        }

        private void InitData()
        {
            Courses = new ObservableCollection<Course>
           {
            new Course{Name="電子商務",Location="教學樓101"},
            new Course{Name="心理學",Location="教學樓101"},
            new Course{Name="高等數學",Location="教學樓101"},
            new Course{Name="網絡營銷",Location="教學樓101"},
           };
            foreach (var c in Courses)
            {
                _data.GetTable<Course>().InsertOnSubmit(c);//插入數據庫
            }
            _data.SubmitChanges();
            this.CourseList.ItemsSource = Courses;
           
        }
        //添加事件
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Course c = new Course()
            {
                Name = "客戶關係管理",
                Location = "教學樓401"
            };
            Courses.Add(c);
            _data.GetTable<Course>().InsertOnSubmit(c);
            _data.SubmitChanges();
        }
        //編輯事件
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Course c = Courses.First(s => s.Name == "網絡營銷");
            c.Location = "編輯教學樓";
            _data.SubmitChanges();
            
        }
        //刪除事件
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Course c = Courses.First(s => s.Name == "電子商務");
            Courses.Remove(c);
            _data.GetTable<Course>().DeleteOnSubmit(c);
            _data.SubmitChanges();
        }
    }




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