WP之Sql Server CE数据库
如何在WP8中进行数据存储,你首先想到应该是独立存储,但是独立存储似乎存储文件更方便,如果我们希望像处理对象的形式,该怎么办呢,答案就是Sql Server CE。
Sql Server CE并不是新鲜东西,它是专门用在移动端的一个工具,它和SQLServer有很大的不同,SQLServer中我们使用Sql语句,而Sql Server CE则要使用Linq处理。
一、创建表
/// <summary>
/// 员工表
/// </summary>
[Table]
class EmployeeTable : INotifyPropertyChanged, INotifyPropertyChanging
{ private int _employeeId;
/// <summary>
/// 员工Id
/// </summary>
[Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
public int EmployeeId
{
get { return _employeeId; }
set
{
if (_employeeId != value)
{
OnPropertyChanging("EmployeeId");
_employeeId = value;
OnPropertyChanged("EmployeeId");
}
}
} private string _employeeName;
/// <summary>
/// 员工名称
/// </summary>
[Column]
public string EmployeeName
{
get { return _employeeName; }
set
{
if (_employeeName != value)
{
OnPropertyChanging("EmployeeName");
_employeeName = value;
OnPropertyChanged("EmployeeName");
}
}
} private string _employeeDesc;
/// <summary>
/// 员工简介
/// </summary>
[Column]
public string EmployeeDesc
{
get { return _employeeDesc; }
set
{
if (_employeeDesc != value)
{
OnPropertyChanging("EmployeeDesc");
_employeeDesc = value;
OnPropertyChanged("EmployeeDesc");
}
}
} public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangingEventHandler PropertyChanging;
private void OnPropertyChanging(string propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
}
一张员工表,有三个属性。类需要加上Table特性,如果你使用过Linq to sql,那么整个特性应该是见过的;另外就是对每个属性需要使用Column特性进行标注;最最重要的一点,类要实现自INotifyPropertyChanged, INotifyPropertyChanging,这两个接口主要是为了通知,同时在类中实现着两个接口的时间即可,并且在属性的Set中进行调用就ok了。
二、上下文对象类(用于操作数据,和数据库交互)
/// <summary>
/// 上下文对象类
/// </summary>
class EmployeeDataContext:DataContext
{
//数据库连接字符串
public static string DBConnectionString = "Data Source=isostore:/Employee.sdf";
public EmployeeDataContext(string connectionString)
: base(connectionString) { }
//员工信息表
public Table<EmployeeTable> Employees;
}
就是一般的类,只不过继承自DataContext,这个和Linq to sql中的上下文对象同一个基类。类中有一个连接字符串属性,同时构造函数支持传入一个连接字符串,另外就是有我们上文中创建的表的一个Table类型的集合,表示一个集合类型。
三、数据列表类(此类大家也可以没有,因为对于集合的处理,大家可以根据自己的需要进行修改)
/// <summary>
/// 列表集合
/// </summary>
class EmployeeCollection : INotifyPropertyChanged
{
private ObservableCollection<EmployeeTable> _employeeTables;
/// <summary>
/// 员工列表集合
/// </summary>
public ObservableCollection<EmployeeTable> EmployeeTables
{
get { return _employeeTables; }
set
{
if (_employeeTables != value)
{
_employeeTables = value;
OnPropertyChanged("EmployeeTables");
}
}
} public void UpdateEmployee(EmployeeTable employee)
{
var oldEmployee = EmployeeTables.Where(e => e.EmployeeId == employee.EmployeeId).FirstOrDefault();
oldEmployee.EmployeeName = employee.EmployeeName;
oldEmployee.EmployeeDesc = employee.EmployeeDesc;
} public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
类中只有一个Employee的集合和一个用于更新对象的方法,以及实现自INotifyPropertyChanged的内容。
四、使用
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid Margin="0,0,0,385">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock FontSize="30" Height="37" HorizontalAlignment="Left" Margin="12,18,0,0" Name="textBlock1" Text="员工名字:" VerticalAlignment="Top" />
<TextBox Name="name" Text="" Margin="145,0,6,144" /> <TextBlock FontSize="30" Height="52" HorizontalAlignment="Left" Margin="18,74,0,0" Name="textBlock2" Text="简介:" VerticalAlignment="Top" />
<TextBox Height="79" HorizontalAlignment="Left" Margin="93,65,0,0" Name="desc" Text="" VerticalAlignment="Top" Width="357" /> <Button
Content="保存" x:Name="saveButton"
Click="saveButton_Click" Margin="219,132,6,6" /> </Grid> <ListBox x:Name="toDoItemsListBox" ItemsSource="{Binding EmployeeTables}" Margin="12,241,12,0" Width="440">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch" Width="440">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions> <TextBlock
Text="{Binding EmployeeName}"
FontSize="{StaticResource PhoneFontSizeLarge}"
Grid.Column="1"
VerticalAlignment="Center"/>
<Button
Grid.Column="2"
x:Name="deleteButton"
BorderThickness="0"
Margin="0"
Click="deleteButton_Click"
Content="删除">
</Button>
<Button
Grid.Column="1"
x:Name="editButton"
BorderThickness="0"
Margin="209,0,81,0"
Click="editButton_Click"
Content="编辑" Grid.ColumnSpan="2">
</Button>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
后台代码:
public partial class MainPage : PhoneApplicationPage
{
//上下文对象
private EmployeeDataContext employeeDB;
//列表集合对象
private EmployeeCollection employeeCollection = new EmployeeCollection();
// Constructor
public MainPage()
{
InitializeComponent();
//连接数据库并初始化DataContext实例
employeeDB = new EmployeeDataContext(EmployeeDataContext.DBConnectionString);
//查询所有的记录
var employeeQry = from e in employeeDB.Employees select e; employeeCollection.EmployeeTables=new ObservableCollection<EmployeeTable>(employeeQry);
//设置当前页面的上下文对象为列表集合
this.DataContext = employeeCollection;
} /// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void deleteButton_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
if (button!=null)
{
//得到当前按钮所绑定的数据
EmployeeTable employee = button.DataContext as EmployeeTable;
//从集合中移除
employeeCollection.EmployeeTables.Remove(employee);
//从数据库中移除
employeeDB.Employees.DeleteOnSubmit(employee);
//更新到数据库
employeeDB.SubmitChanges();
}
} /// <summary>
/// 编辑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void editButton_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
if (button!=null)
{
//得到当前按钮绑定的数据
EmployeeTable employ = button.DataContext as EmployeeTable;
//将名称和简介设置为文本框的值
name.Text = employ.EmployeeName;
desc.Text = employ.EmployeeDesc;
//将当前编辑的对象放置在State中,用于保存时候使用
State["employee"] = employ;
//将当前对象从集合中移除
employeeCollection.UpdateEmployee(employ);
// employeeCollection.EmployeeTables.Remove(employ);
}
} /// <summary>
/// 保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void saveButton_Click(object sender, RoutedEventArgs e)
{
if (name.Text != "" && desc.Text != "")
{
//如果当前的State不为空,并且存在Employee,则为编辑
if (State.Count > && State["employee"] != null)
{
//得到State中的对象
EmployeeTable employ = (EmployeeTable)State["employee"];
//将文本框的值设置给对象
employ.EmployeeName = name.Text;
employ.EmployeeDesc = desc.Text;
//将数据保存到数据库
employeeDB.SubmitChanges();
// employeeCollection.EmployeeTables.Add(employ);
State["employee"] = null; }
//新增
else
{
//创建Employee对象
EmployeeTable employee = new EmployeeTable { EmployeeName = name.Text, EmployeeDesc = desc.Text };
//将Employee放入集合中
employeeCollection.EmployeeTables.Add(employee);
//将Employee添加到数据库
employeeDB.Employees.InsertOnSubmit(employee);
//将数据更新到数据库
employeeDB.SubmitChanges();
} name.Text = "";
desc.Text = "";
}
else
{
MessageBox.Show("姓名和简介不能为空");
}
}
}
五、迟到的创建数据库
细心的童鞋会发现,为什么木有创建数据库的过程,如下:
private void Application_Launching(object sender, LaunchingEventArgs e)
{
using (EmployeeDataContext db=new EmployeeDataContext (EmployeeDataContext.DBConnectionString))
{
if (db.DatabaseExists()==false)
{
db.CreateDatabase();
}
}
}
在App.xaml.cs中放入上述代码即可,也就是创建了一个数据库。
Tips:数据库文件同样是在独立存储空间中。
WP之Sql Server CE数据库的更多相关文章
- Windows Phone 8 SQL Server CE 数据库
员工信息表 EmployeeTable.cs using System.Data.Linq.Mapping; using System.ComponentModel; namespace SQLSer ...
- 让PDF.NET支持不同版本的SQL Server Compact数据库
最近项目中需要用到嵌入式数据库,我们选用的数据开发框架是PDF.NET(http://www.pwmis.com/SqlMap/),之前的博文已经总结了让PDF.NET支持最新的SQLite,今天我们 ...
- Linux下使用FreeTDS访问MS SQL Server 2005数据库(包含C测试源码)
Linux下使用FreeTDS访问MS SQL Server 2005数据库(包含C测试源码) http://blog.csdn.net/helonsy/article/details/7207497 ...
- 如何转换SQL Server 2008数据库到SQL Server 2005
背景介绍: 公司一套系统使用的是SQL SERVER 2008数据库,突然一天收到邮件,需要将这套系统部署到各个不同地方(海外)的工厂,需要在各个工厂部署该数据库,等我将准备工作做好,整理文档 ...
- SQL SERVER 2008数据库各版本功能对比
微软SQL SERVER 2008数据库有6个版本,分别是数据中心版.企业版.标准版.Web版.工作组版.简易版,有时候购买的时候或需要使用某项功能时,需要了解各个版本的区别,功能差异,很多时候,大部 ...
- SQL Server附加数据库时报1813错误的解决方案
SQL Server附加数据库时报1813错误的解决方案 无法打开新数据库 'ASR'.CREATE DATABASE 中止. 文件激活失败.物理文件名称'E:\SqlServer\MSSQL\D ...
- SQL Server附加数据库问题
SQL Server附加数据库时,遇到如下问题:“如果升级全文目录,请单加“添加目录”,然后找到它并选择它.基于全文升级选项,全文索引将为“已导入”.” 解决方法: 选择数据库文件所在目录,右键-&g ...
- SQL Server附加数据库出现错误5123的正确解决方法
因为自己有一本基于SQL Server 2005的数据库教程,里边使用的示例数据库是AdventureWorks for SQL Server 2005,而我的机子上装的是SQL Server 200 ...
- 通过SQL Server 2008数据库复制实现数据库同步备份
SQL Server 2008数据库复制是通过发布/订阅的机制进行多台服务器之间的数据同步,我们把它用于数据库的同步备份.这里的同步备份指的是备份服务器与主服务器进行 实时数据同步,正常情况下只使用主 ...
随机推荐
- SQL中的模糊查询
写个标题先.先来一篇大神的文章:http://www.cnblogs.com/GT_Andy/archive/2009/12/25/1921914.html 练习代码如下: 1.百分号:% 表示任 ...
- Basic Vlan Concepts
1. Vlan Benefit ·To reduce CPU overhead on each device by reducing the number of devices that recei ...
- 升级Mac osx 10.10 后安装php 的mcrypt extension
昨天折腾到凌晨四点多,今天又到这个点,终于成功了,心情比较复杂,先记录一下,改日再整理,此刻头昏脑胀,只想蒙头大睡. 发的问题,还没人回复,自己找到答案了,英语比较蹩脚,凑合看.. http://st ...
- Linq小技巧
遍历集合ForEach: listAll.Items.Clear(); List<Users> list = DP.UsersDAO.GetInfoList(); list.ForEach ...
- mutable和volatile关键字
1.mutable 在C++中,mutable是为了突破const的限制而设置的.被mutable修饰的变量,将永远处于可变的状态,即使在一个const函数中,甚至结构体变量或者类对象为const,其 ...
- C预处理,条件编译
预处理是指在编译器之前运行,常以“#”开头 包含3个方面的内容: 1)宏定义与宏替换 2)文件包含 3)条件编译 宏定义与宏替换: 宏名一般大写,替换发生在编译之前,且是机械替换,不做语法检查,也不分 ...
- hash --C++
题目来源:code[VS] 这是一个极其无聊的hash题.... 1230 元素查找 题目描述 Description 给出n个正整数,然后有m个询问,每个询问一个整数,询问该整数是否在n个正整数中出 ...
- JPA学习---第二节:JPA开发环境和思想介绍
一.下载相关 jar http://hibernate.org/orm/ 下载 hibernate ,解压 http://www.slf4j.org/download.html 下载 slf4j,解压 ...
- SQL Server DATEADD() 函数
SQL Server Date 函数 定义和用法 DATEADD() 函数在日期中添加或减去指定的时间间隔. 语法 DATEADD(datepart,number,date) date 参数是合法的日 ...
- Python Socket File Transfer
I have a RPi which I intented to use it to crawl data. The development environment in RPi is very ba ...