当前位置:Gxlcms > mysql > 操作本地数据库

操作本地数据库

时间: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特性

  1. /// <summary>
  2. /// 课程类
  3. /// </summary>
  4. <strong><span> [Table]</span></strong> //表示类将成为一个table
  5. public class Course : INotifyPropertyChanged, INotifyPropertyChanging
  6. {
  7. <strong><span> [Column(IsVersion = true)] </span></strong>//table的列
  8. private Binary _version;
  9. private int _id;
  10. <strong><span> [Column(IsPrimaryKey=true,IsDbGenerated=true)] //table的列,主键,自动生成</span></strong>
  11. public int Id
  12. {
  13. get { return _id; }
  14. set
  15. {
  16. if (_id != value)
  17. {
  18. RaiseProtertyChanging("Id");
  19. _id = value;
  20. RaisePropertyChanged("Id");
  21. }
  22. }
  23. }
  24. private string _name;
  25. <strong><span> [Column]</span></strong>
  26. public string Name
  27. {
  28. get { return _name; }
  29. set
  30. {
  31. if (_name != value)
  32. {
  33. RaiseProtertyChanging("Name");
  34. _name = value;
  35. RaisePropertyChanged("Name");
  36. }
  37. }
  38. }
  39. private string _location;
  40. <strong><span> [Column]</span></strong>
  41. public string Location
  42. {
  43. get { return _location; }
  44. set
  45. {
  46. if (_location != value)
  47. {
  48. RaiseProtertyChanging("Location");
  49. _location = value;
  50. RaisePropertyChanged("Location");
  51. }
  52. }
  53. }
  54. public event PropertyChangedEventHandler PropertyChanged;
  55. private void RaisePropertyChanged(string propertyName)
  56. {
  57. if (PropertyChanged != null)
  58. {
  59. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  60. }
  61. }
  62. public event PropertyChangingEventHandler PropertyChanging;
  63. private void RaiseProtertyChanging(string propertyName)
  64. {
  65. if (PropertyChanging != null)
  66. {
  67. PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
  68. }
  69. }
  70. }
2.创建类继承DataContext,而且封装this.GetTable()方法,否则创建数据库时报错
  1. public class MyDataContext : DataContext
  2. {
  3. //连接字符窜
  4. public const string ConnectionString = "Data Source=isostore:/MyDb.sdf";
  5. //构造函数
  6. public MyDataContext()
  7. : base(ConnectionString)
  8. {
  9. if (!this.DatabaseExists())
  10. {
  11. //创建数据库
  12. this.CreateDatabase();
  13. }
  14. }
  15. <strong><span> //必须存在,否则创建数据库报错:DataContext不存在表
  16. public Table<course> CourseTable
  17. {
  18. get { return this.GetTable<course>(); }
  19. }</course></course></span></strong>
  20. }

3.页面前台代码
  1. <!--ContentPanel - 在此处放置其他内容-->
  2. <grid x:name="ContentPanel" grid.row="1" margin="12,0,12,0">
  3. <grid.rowdefinitions>
  4. <rowdefinition height="Auto">
  5. <rowdefinition height="*">
  6. </rowdefinition></rowdefinition></grid.rowdefinitions>
  7. <stackpanel grid.row="0" orientation="Horizontal">
  8. <button content="添加" click="Button_Click">
  9. </button><button content="编辑" click="Button_Click_1">
  10. </button><button content="删除" click="Button_Click_2">
  11. <listbox grid.row="1" name="CourseList">
  12. <strong><span> <listbox.itemtemplate></listbox.itemtemplate></span>
  13. <span> <datatemplate></datatemplate></span></strong>
  14. <grid>
  15. <grid.columndefinitions>
  16. <columndefinition width="Auto">
  17. <columndefinition width="Auto">
  18. <columndefinition width="Auto">
  19. </columndefinition></columndefinition></columndefinition></grid.columndefinitions>
  20. <textblock grid.column="0" text="{Binding Id,Mode=TwoWay}">
  21. <textblock grid.column="1" text="{Binding Name,Mode=TwoWay}">
  22. <textblock grid.column="2" text="{Binding Location,Mode=TwoWay}">
  23. </textblock></textblock></textblock></grid>
  24. <strong><span></span>
  25. <span> </span></strong>
  26. </listbox>
  27. </button></stackpanel></grid>

4.页面后台代码

添加:调用GetTable().InsertOnSubmit(T)方法,最后调用SubmitChanges()向数据库提交数据。

删除: GetTable().DeleteOnSubmit(T)方法,最后调用SubmitChanges()向数据库提交数据。

编辑:调用SubmitChanges()

页面绑定的数据源必须是ObservableCollection类型

  1. public partial class MainPage : PhoneApplicationPage
  2. {
  3. <strong><span>private ObservableCollection<course> Courses;</course></span></strong>
  4. private DataContext _data;
  5. // 构造函数
  6. public MainPage()
  7. {
  8. InitializeComponent();
  9. //创建数据库
  10. //CreateDatabase();
  11. _data = new MyDataContext();
  12. //初始化数据
  13. InitData();
  14. }
  15. private void CreateDatabase()
  16. {
  17. _data = new MyDataContext();
  18. if (!_data.DatabaseExists())
  19. {
  20. _data.CreateDatabase();
  21. }
  22. }
  23. private void InitData()
  24. {
  25. Courses = new ObservableCollection<course>
  26. {
  27. new Course{Name="电子商务",Location="教学楼101"},
  28. new Course{Name="心理学",Location="教学楼101"},
  29. new Course{Name="高等数学",Location="教学楼101"},
  30. new Course{Name="网络营销",Location="教学楼101"},
  31. };
  32. foreach (var c in Courses)
  33. {
  34. <strong><span> _data.GetTable<course>().InsertOnSubmit(c);//插入数据库</course></span></strong>
  35. }
  36. <strong><span>_data.SubmitChanges();
  37. this.CourseList.ItemsSource = Courses;</span></strong>
  38. }
  39. //添加事件
  40. private void Button_Click(object sender, RoutedEventArgs e)
  41. {
  42. Course c = new Course()
  43. {
  44. Name = "客户关系管理",
  45. Location = "教学楼401"
  46. };
  47. Courses.Add(c);
  48. <strong><span> _data.GetTable<course>().InsertOnSubmit(c);
  49. _data.SubmitChanges();</course></span></strong>
  50. }
  51. //编辑事件
  52. private void Button_Click_1(object sender, RoutedEventArgs e)
  53. {
  54. Course c = Courses.First(s => s.Name == "网络营销");
  55. c.Location = "编辑教学楼";
  56. <span><strong> _data.SubmitChanges();</strong></span>
  57. }
  58. //删除事件
  59. private void Button_Click_2(object sender, RoutedEventArgs e)
  60. {
  61. Course c = Courses.First(s => s.Name == "电子商务");
  62. Courses.Remove(c);
  63. <strong><span> _data.GetTable<course>().DeleteOnSubmit(c);
  64. _data.SubmitChanges();</course></span></strong>
  65. }
  66. }</course>

人气教程排行