博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows Phone 本地数据库的操作
阅读量:6494 次
发布时间:2019-06-24

本文共 10967 字,大约阅读时间需要 36 分钟。

App.xaml

#region 创建数据库using (MyDataContext dc = new MyDataContext()) {  if (dc.DatabaseExists() == false)  {    dc.CreateDatabase();  }}#endregion

 

MainPage.xaml

using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using Microsoft.Phone.Controls;namespace LocalDataBase{    public partial class MainPage : PhoneApplicationPage    {        // 构造函数           public MainPage()        {            InitializeComponent();        }        // 编辑           private void OnDataEdit(object sender, RoutedEventArgs e)        {            Button btn = e.OriginalSource as Button;            if (btn != null)            {                string no = btn.Tag as string;                NavigationService.Navigate(new Uri("/EditPage.xaml?sno=" + no, UriKind.Relative));            }        }        // 删除数据           private void OnDataDelete(object sender, RoutedEventArgs e)        {            Button btn = e.OriginalSource as Button;            if (btn != null)            {                string sNo = btn.Tag.ToString();                using (MyDataContext dc = new MyDataContext())                {                    Students stu = dc.Students.FirstOrDefault(s => s.StuNo == sNo);                    if (stu != null)                    {                        dc.Students.DeleteOnSubmit(stu);                        dc.SubmitChanges();                        BindList();                    }                }            }        }        private void onRefresh(object sender, EventArgs e)        {            BindList();        }        // 新增           private void onNew(object sender, EventArgs e)        {            NavigationService.Navigate(new Uri("/AddPage.xaml", UriKind.Relative));        }        ///            /// 把数据绑定到ListBox           ///            private void BindList()        {            using (MyDataContext dc = new MyDataContext())            {                var res =                    from s in dc.Students                    select s;                this.stuList.ItemsSource = res.ToList();            }        }        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)        {            base.OnNavigatedTo(e);            BindList();        }    }}

AddPage.xaml

using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using Microsoft.Phone.Controls;namespace LocalDataBase{    public partial class AddPage : PhoneApplicationPage    {        public AddPage()        {            InitializeComponent();        }        private void onCancel(object sender, EventArgs e)        {            //NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));               if (NavigationService.CanGoBack)            {                NavigationService.GoBack();            }        }        private void onSave(object sender, EventArgs e)        {            if (txtNo.Text == "")            {                MessageBox.Show("学号不能为空。"); return;            }            if (txtName.Text == "")            {                MessageBox.Show("姓名不能为空。"); return;            }            using (MyDataContext dc = new MyDataContext())            {                if (dc.Students.Where(c => c.StuNo == txtNo.Text).Count() > 0)                {                    MessageBox.Show("输入的学号已经存在。"); return;                }                Students stu = new Students()                {                    StuNo = txtNo.Text,                    Name = txtName.Text,                    Email = txtEmail.Text                };                dc.Students.InsertOnSubmit(stu);                dc.SubmitChanges();            }            //NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));               if (NavigationService.CanGoBack)            {                NavigationService.GoBack();            }        }    }}

EditPage.xaml

using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using Microsoft.Phone.Controls;namespace LocalDataBase{    public partial class EditPage : PhoneApplicationPage    {        public EditPage()        {            InitializeComponent();        }        private void onSave(object sender, EventArgs e)        {            if (txtName.Text == "")            {                MessageBox.Show("姓名不能为空。"); return;            }            using (MyDataContext dc = new MyDataContext())            {                Students stu = dc.Students.FirstOrDefault(s => s.StuNo == txtNo.Text);                if (stu != null)                {                    stu.Name = txtName.Text;                    stu.Email = txtEmail.Text;                    dc.SubmitChanges();                }            }            //NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));               if (NavigationService.CanGoBack)            {                NavigationService.GoBack();            }        }        private void onCancel(object sender, EventArgs e)        {            //NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));               if (NavigationService.CanGoBack)            {                NavigationService.GoBack();            }        }        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)        {            base.OnNavigatedTo(e);            if (NavigationContext.QueryString.ContainsKey("sno"))            {                txtNo.Text = NavigationContext.QueryString["sno"];                using (MyDataContext dc = new MyDataContext())                {                    Students stu = dc.Students.FirstOrDefault(s => s.StuNo == txtNo.Text);                    if (stu != null)                    {                        txtName.Text = stu.Name;                        txtEmail.Text = stu.Email;                    }                }            }        }    }}

MyDataContext.cs

using System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using Microsoft.Phone.Data.Linq;using System.Data.Linq;using System.Data.Linq.Mapping;using System.ComponentModel;namespace LocalDataBase{    public class MyDataContext : DataContext    {        ///            /// 连接字符串           ///            public const string ConnectionString = "Data Source='isostore:/MyDb.sdf';Password='123456'";        ///            /// 构造函数           ///            public MyDataContext() : base(ConnectionString) { }        public Table
Students; } [Table] public class Students : INotifyPropertyChanged, INotifyPropertyChanging { string _stuNo; ///
/// 学号 /// [Column(CanBeNull = false, IsPrimaryKey = true)] public string StuNo { get { return this._stuNo; } set { if (_stuNo != value) { OnPropertyChanging(new PropertyChangingEventArgs("StuNo")); this._stuNo = value; OnPropertyChanged(new PropertyChangedEventArgs("StuNo")); } } } string _name; ///
/// 姓名 /// [Column] public string Name { get { return this._name; } set { if (_name != value) { OnPropertyChanging(new PropertyChangingEventArgs("Name")); _name = value; OnPropertyChanged(new PropertyChangedEventArgs("Name")); } } } string _email; ///
/// 电邮 /// [Column] public string Email { get { return this._email; } set { if (_email != value) { OnPropertyChanging(new PropertyChangingEventArgs("Email")); _email = value; OnPropertyChanged(new PropertyChangedEventArgs("Email")); } } } public event PropertyChangingEventHandler PropertyChanging; public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanging(PropertyChangingEventArgs e) { if (PropertyChanging != null) { PropertyChanging(this, e); } } protected void OnPropertyChanged(PropertyChangedEventArgs e) { if (PropertyChanged != null) { PropertyChanged(this, e); } } }}

 

转载地址:http://jpkyo.baihongyu.com/

你可能感兴趣的文章
RookeyFrame 通用页面 加载数据 原理
查看>>
hbuilder APP服务器端(C#)推送
查看>>
统计c盘的PE文件的个数 (遍历所有文件)
查看>>
大白话Vue源码系列目录
查看>>
EffectKeyMap系列1(Ubuntu)
查看>>
iOS手势
查看>>
Webpack源码基础-Tapable从使用Hook到源码解析
查看>>
【转载】NBU异机恢复oracle
查看>>
魅族mx5详细打开usb调试模式的步骤
查看>>
php里关于文件下载的方法(两种)
查看>>
数据绑定(数据源控件 -- ObjectDataSource)
查看>>
微信点单
查看>>
selenium操作页面元素总结
查看>>
vim 命令
查看>>
Ubuntu 16.04 LTS安装sogou输入法详解
查看>>
计算几何专题
查看>>
GNU/Linux 正则表达式与三剑侠(grep,sed,awk)(精)
查看>>
36、自定义控件详解(一)-- 自定义属性
查看>>
bootstrap弹窗居中处理
查看>>
DOM学习笔记二
查看>>