时间:2021-07-01 10:21:17 帮助过:18人阅读
1.创建实体 注意加Table和Column特性 /// summary /// 课程类 /// /summary [Table] //表示类将成为一个table public class Course : INotifyPropertyChanged, INotifyPropertyChanging { [Column(IsVersion = true)] //table的列 private Binary _version;
1.创建实体 注意加Table和Column特性
2.创建类继承DataContext,而且封装this.GetTable
- /// <summary>
- /// 课程类
- /// </summary>
- <strong><span> [Table]</span></strong> //表示类将成为一个table
- public class Course : INotifyPropertyChanged, INotifyPropertyChanging
- {
- <strong><span> [Column(IsVersion = true)] </span></strong>//table的列
- private Binary _version;
- private int _id;
- <strong><span> [Column(IsPrimaryKey=true,IsDbGenerated=true)] //table的列,主键,自动生成</span></strong>
- public int Id
- {
- get { return _id; }
- set
- {
- if (_id != value)
- {
- RaiseProtertyChanging("Id");
- _id = value;
- RaisePropertyChanged("Id");
- }
- }
- }
- private string _name;
- <strong><span> [Column]</span></strong>
- public string Name
- {
- get { return _name; }
- set
- {
- if (_name != value)
- {
- RaiseProtertyChanging("Name");
- _name = value;
- RaisePropertyChanged("Name");
- }
- }
- }
- private string _location;
- <strong><span> [Column]</span></strong>
- 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));
- }
- }
- }
- public class MyDataContext : DataContext
- {
- //连接字符窜
- public const string ConnectionString = "Data Source=isostore:/MyDb.sdf";
- //构造函数
- public MyDataContext()
- : base(ConnectionString)
- {
- if (!this.DatabaseExists())
- {
- //创建数据库
- this.CreateDatabase();
- }
- }
- <strong><span> //必须存在,否则创建数据库报错:DataContext不存在表
- public Table<course> CourseTable
- {
- get { return this.GetTable<course>(); }
- }</course></course></span></strong>
- }
- <!--ContentPanel - 在此处放置其他内容-->
- <grid x:name="ContentPanel" grid.row="1" margin="12,0,12,0">
- <grid.rowdefinitions>
- <rowdefinition height="Auto">
- <rowdefinition height="*">
- </rowdefinition></rowdefinition></grid.rowdefinitions>
- <stackpanel grid.row="0" orientation="Horizontal">
- <button content="添加" click="Button_Click">
- </button><button content="编辑" click="Button_Click_1">
- </button><button content="删除" click="Button_Click_2">
- <listbox grid.row="1" name="CourseList">
- <strong><span> <listbox.itemtemplate></listbox.itemtemplate></span>
- <span> <datatemplate></datatemplate></span></strong>
- <grid>
- <grid.columndefinitions>
- <columndefinition width="Auto">
- <columndefinition width="Auto">
- <columndefinition width="Auto">
- </columndefinition></columndefinition></columndefinition></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}">
- </textblock></textblock></textblock></grid>
- <strong><span></span>
- <span> </span></strong>
- </listbox>
- </button></stackpanel></grid>
添加:调用GetTable
删除: GetTable
编辑:调用SubmitChanges()
页面绑定的数据源必须是ObservableCollection类型
- public partial class MainPage : PhoneApplicationPage
- {
- <strong><span>private ObservableCollection<course> Courses;</course></span></strong>
- 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)
- {
- <strong><span> _data.GetTable<course>().InsertOnSubmit(c);//插入数据库</course></span></strong>
- }
- <strong><span>_data.SubmitChanges();
- this.CourseList.ItemsSource = Courses;</span></strong>
- }
- //添加事件
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- Course c = new Course()
- {
- Name = "客户关系管理",
- Location = "教学楼401"
- };
- Courses.Add(c);
- <strong><span> _data.GetTable<course>().InsertOnSubmit(c);
- _data.SubmitChanges();</course></span></strong>
- }
- //编辑事件
- private void Button_Click_1(object sender, RoutedEventArgs e)
- {
- Course c = Courses.First(s => s.Name == "网络营销");
- c.Location = "编辑教学楼";
- <span><strong> _data.SubmitChanges();</strong></span>
- }
- //删除事件
- private void Button_Click_2(object sender, RoutedEventArgs e)
- {
- Course c = Courses.First(s => s.Name == "电子商务");
- Courses.Remove(c);
- <strong><span> _data.GetTable<course>().DeleteOnSubmit(c);
- _data.SubmitChanges();</course></span></strong>
- }
- }</course>