windows phone 本地数据库操作

效果图:

  

创建数据库:

View Code
 1 using System;
2 using System.Net;
3 using System.Windows;
4 using System.Windows.Controls;
5 using System.Windows.Documents;
6 using System.Windows.Ink;
7 using System.Windows.Input;
8 using System.Windows.Media;
9 using System.Windows.Media.Animation;
10 using System.Windows.Shapes;
11 using System.Data.Linq;
12 using System.Data.Linq.Mapping;
13 namespace DBHelper
14 {
15 public class MyDataContext : DataContext
16 {
17 public const string ConnectionStr = "Data Source=isostore:/testdb.sdf";
18 public MyDataContext() :
19 base(ConnectionStr)
20 {
21
22 }
23 public Table<MyTable.Td_People> Td_People;
24
25
26
27
28 }
29
30 }


建立表:

View Code
 1 using System;
2 using System.Net;
3 using System.Windows;
4 using System.Windows.Controls;
5 using System.Windows.Documents;
6 using System.Windows.Ink;
7 using System.Windows.Input;
8 using System.Windows.Media;
9 using System.Windows.Media.Animation;
10 using System.Windows.Shapes;
11 using System.Data.Linq;
12 using System.Data.Linq.Mapping;
13 namespace DBHelper
14 {
15 public class MyTable
16 {
17 #region
18 /// <summary>
19 /// 人物
20 /// </summary>
21 [Table]
22 public class Td_People
23 {
24 private int _id;
25 [Column(IsPrimaryKey = true, IsDbGenerated = true)]
26 public int ID
27 {
28 get { return _id; }
29 set { _id = value; }
30 }
31 private string _name;
32 [Column]
33 public string Name
34 {
35 get { return _name; }
36 set { _name = value; }
37 }
38 private string _age;
39 [Column]
40 public string Age
41 {
42 get { return _age; }
43 set { _age = value; }
44 }
45 }
46 #endregion
47 }
48 }

数据库操作类:

View Code
  1 using System;
2 using System.Net;
3 using System.Windows;
4 using System.Windows.Controls;
5 using System.Windows.Documents;
6 using System.Windows.Ink;
7 using System.Windows.Input;
8 using System.Windows.Media;
9 using System.Windows.Media.Animation;
10 using System.Windows.Shapes;
11 using System.Collections.ObjectModel;
12 using System.Collections.Generic;
13 using System.Linq;
14 using System.Data.Linq;
15 namespace DBHelper
16 {
17 public class Helper
18 {
19 MyDataContext DB;
20 /// <summary>
21 /// 创建数据库
22 /// </summary>
23 public Helper()
24 {
25 if (DB == null)
26 {
27 DB = new MyDataContext();
28 }
29 if (!DB.DatabaseExists()) {
30 DB.CreateDatabase();
31 }
32 }
33 /// <summary>
34 /// 新增数据
35 /// </summary>
36 /// <param name="Table"></param>
37 /// <returns></returns>
38 public bool AddDataRows(DBHelper.MyTable.Td_People Td_People)
39 {
40 try
41 {
42 DB.Td_People.InsertOnSubmit(Td_People);
43 DB.SubmitChanges();
44 return true;
45 }
46 catch (DataMisalignedException)
47 {
48 return false;
49 }
50 }
51 /// <summary>
52 /// 返回所有数据
53 /// </summary>
54 /// <returns></returns>
55 public ObservableCollection<MyTable.Td_People> GetDataRows()
56 {
57 ObservableCollection<MyTable.Td_People> Td_People = new ObservableCollection<MyTable.Td_People>();
58 if (DB.DatabaseExists())
59 {
60 IEnumerator<MyTable.Td_People> _IEnumerable = DB.Td_People.GetEnumerator();
61 while (_IEnumerable.MoveNext())
62 {
63 Td_People.Add(_IEnumerable.Current);
64 }
65 }
66 return Td_People;
67 }
68 /// <summary>
69 /// 返回指定ID数据
70 /// </summary>
71 /// <param name="ID"></param>
72 /// <returns></returns>
73 public MyTable.Td_People GetIDRows(int ID) {
74 MyTable.Td_People Td_People = new MyTable.Td_People();
75 if (DB.DatabaseExists()) {
76 ///查询数据
77 var t = from r in GetDataRows()
78 where (int)r.ID == (int)ID
79 select r;
80 foreach (MyTable.Td_People mt in t)
81 {
82 Td_People = mt;
83 break;
84 }
85 }
86 return Td_People;
87 }
88 /// <summary>
89 /// 更新数据
90 /// </summary>
91 /// <param name="SourceTd"></param>
92 /// <param name="NewTd"></param>
93 /// <returns></returns>
94 public bool UpdataRows(MyTable.Td_People SourceTd, MyTable.Td_People NewTd) {
95 try
96 {
97 //更新本地数据库中的数据有三个步骤。
98 //第一,要更新的对象数据库中查询。
99 //第二,修改所需的对象。
100 //最后,调用SubmitChanges方法,以将所做的更改保存到本地数据库。
101
102 ///第一步
103 var t = from r in GetDataRows()
104 where r.ID == SourceTd.ID
105 select r;
106 foreach (MyTable.Td_People mt in t) {
107 mt.Age = NewTd.Age;
108 mt.Name = NewTd.Name;
109 }
110 ///第二步
111 MyTable.Td_People Td = DB.Td_People.GetOriginalEntityState(SourceTd);
112 Td.Age = NewTd.Age;
113 Td.Name = NewTd.Name;
114 ///第三步
115 DB.SubmitChanges();
116 return true;
117 }
118 catch (DataMisalignedException) {
119 return false;
120 }
121 }
122
123 /// <summary>
124 /// 删除指定数据
125 /// </summary>
126 /// <param name="SourceTd"></param>
127 /// <returns></returns>
128 public bool DeleteDataRows(MyTable.Td_People SourceTd)
129 {
130 try {
131
132 DB.Td_People.DeleteOnSubmit(SourceTd);
133 DB.SubmitChanges();
134 GetDataRows().Remove(SourceTd);
135 return true;
136 }
137 catch (DataMisalignedException) {
138 return false;
139 }
140 }
141
142 }
143 }



posted @ 2011-10-21 16:19  十年一刻·i  阅读(900)  评论(8编辑  收藏  举报