之前用蹩脚的英文尝试翻译了一些简单的英文基础教程,估计错误不少,在这里将其整理好并给出一个简单的示例程序,请多指教。
- Windows Phone本地数据库(SQLCE):1、介绍(翻译)
- Windows Phone本地数据库(SQLCE):2、LINQ to SQL(翻译)
- Windows Phone本地数据库(SQLCE):3、[table]attribute(翻译)
- Windows Phone本地数据库(SQLCE):4、[Column]attribute(翻译)
- Windows Phone本地数据库(SQLCE):5、[Association]attribute(翻译)
- Windows Phone本地数据库(SQLCE):6、[Index] attribute(翻译)
- Windows Phone本地数据库(SQLCE):7、Database mapping(翻译)
- Windows Phone本地数据库(SQLCE):8、DataContext(翻译)
- Windows Phone本地数据库(SQLCE):9、Connection Strings(翻译)
- Windows Phone本地数据库(SQLCE):10、创建数据库(翻译)
- Windows Phone本地数据库(SQLCE):11、使用LINQ查询数据库(翻译)
- Windows Phone本地数据库(SQLCE):12、插入数据(翻译)
- Windows Phone本地数据库(SQLCE):13、更新数据(翻译)
- Windows Phone本地数据库(SQLCE):14、删除数据(翻译)
这里我给出了一个简单的增删改查的示例
先看基本的界面,下面3个button分别是添加,删除,修改
下面是添加信息的页面
我总结主要有以下几个步骤
1、根据实际情况需要建立一张数据表,这里建立StudentTable.cs,写好需要数据表列,定义相关的属性。
在此之前,我们需要添加引用 System.Data.Linq ,并在cs代码中using System.Data.Linq.Mapping。
StudentTable.cs代码如下:
1 [Table]//define a table 2 public class StudentTable 3 { 4 private string schoolId; 5 6 [Column(IsPrimaryKey = true, CanBeNull = false, DbType = "NVarChar(20) NOT NULL", AutoSync = AutoSync.OnInsert)] 7 public string SchoolId 8 { 9 get { return schoolId; } 10 set { schoolId = value; } 11 } 12 13 private string name; 14 15 [Column] 16 public string Name 17 { 18 get { return name; } 19 set { name = value; } 20 } 21 22 23 private string address; 24 [Column] 25 public string Address 26 { 27 get { return address; } 28 set { address = value; } 29 } 30 }
数据表的介绍请看Windows Phone本地数据库(SQLCE):3、[table]attribute(翻译)
2、再定义一个数据库,新建一个类命名为StudentDataContext.cs,这里要using System.Data.Linq,StudentDataContext要继承于DataContext
StudentDataContext.cs代码如下:
1 public class StudentDataContext : DataContext 2 { 3 public static string ConnectionString = "Data Source=isostore:/StudentInfo.sdf"; 4 //"Data Source=isostore:/StudentInfo.sdf"; 5 public StudentDataContext(string connectionString) 6 : base(connectionString) 7 { } 8 9 public Table<StudentTable> Students 10 { 11 get 12 { 13 return this.GetTable<StudentTable>(); 14 } 15 } 16 17 }
这里需要注意到的是ConnectionString的格式,基本的格式是String format: "Data Source=isostore:/DIRECTORY/FILE.sdf";
DIRECTORY/FILE.sdf是自定义的路径及数据库名称
详情请查看MSDN文档http://msdn.microsoft.com/zh-cn/library/hh202861(v=vs.92).aspx
3、创建数据库,一般在程序启动时就创建好数据库,我们选在App.xaml.cs的Application_Launching方法里创建,创建之前需要先检测数据库是否存在
1 private void Application_Launching(object sender, LaunchingEventArgs e) 2 { 3 using (StudentDataContext context = new StudentDataContext(StudentDataContext.ConnectionString)) 4 { 5 if (!context.DatabaseExists()) 6 { 7 context.CreateDatabase(); 8 } 9 } 10 }
4、接下来就可以做基本的数据库操作了,我定义一个类LINQHelper进行这些操作,代码如下
1 public class LINQHelper 2 { 3 public static List<StudentTable> GetStudentInfo() //查 4 { 5 List<StudentTable> ls = new List<StudentTable>(); 6 using (StudentDataContext context = new StudentDataContext(StudentDataContext.ConnectionString)) 7 { 8 var query = from p in context.Students select p; 9 ls = query.ToList(); 10 } 11 12 return ls; 13 } 14 15 public static void AddStudentInfo(string schoolId, string name, string address)//增 16 { 17 using (StudentDataContext context = new StudentDataContext(StudentDataContext.ConnectionString)) 18 { 19 StudentTable s = new StudentTable(); 20 s.SchoolId = schoolId; 21 s.Name = name; 22 s.Address = address; 23 context.Students.InsertOnSubmit(s); 24 25 context.SubmitChanges(); 26 } 27 } 28 29 public static void DelStudentInfo(string schoolId)//删 30 { 31 using (StudentDataContext context = new StudentDataContext(StudentDataContext.ConnectionString)) 32 { 33 var query = from p in context.Students where p.SchoolId == schoolId select p; 34 StudentTable studentToDelete = query.FirstOrDefault(); 35 36 context.Students.DeleteOnSubmit(studentToDelete); 37 38 context.SubmitChanges(); 39 } 40 } 41 42 public static void UpdateStudentInfo(string oldSchoolId, string name, string address)//改 43 { 44 using (StudentDataContext context = new StudentDataContext(StudentDataContext.ConnectionString)) 45 { 46 var query = from p in context.Students where p.SchoolId == oldSchoolId select p; 47 StudentTable studentToUpdate = query.FirstOrDefault(); 48 49 studentToUpdate.Name = name; 50 studentToUpdate.Address = address; 51 52 context.SubmitChanges(); 53 } 54 } 55 }
在各个button的事件处理函数里添加好相关的方法及参数即可。
这里有个问题请教各位,我这里采用数据绑定来绑定数据到ListBox,有什么方法可以重新刷新当前页面,在做删除操作后需要的刷新页面才能看到ListBox的项被删除,我这里采用的方法是导航到本页面
1 NavigationService.Navigate(new Uri("/MainPage.xaml?guid=" + Guid.NewGuid(), UriKind.Relative));
感觉这种方法不是很好,不知道有没有更好的解决方法,谢谢!!!