员工信息表 EmployeeTable.cs
using System.Data.Linq.Mapping;
using System.ComponentModel; namespace SQLServerDemo
{
[Table]
public class EmployeeTable : INotifyPropertyChanged, INotifyPropertyChanging
{
// 定义员工表独立增长ID,设置为主键
private int _employeeId; [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)
{
NotifyPropertyChanging("EmployeeID");
_employeeId = value;
NotifyPropertyChanged("EmployeeID");
}
}
} // 定义员工名字字段
private string _employeeName; [Column]
public string EmployeeName
{
get
{
return _employeeName;
}
set
{
if (_employeeName != value)
{
NotifyPropertyChanging("EmployeeName");
_employeeName = value;
NotifyPropertyChanged("EmployeeName");
}
}
} //定义员工简介字段
private string _employeeDesc; [Column]
public string EmployeeDesc
{
get
{
return _employeeDesc;
}
set
{
if (_employeeDesc != value)
{
NotifyPropertyChanging("EmployeeDesc");
_employeeDesc = value;
NotifyPropertyChanged("EmployeeDesc");
}
}
} #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; //用来通知页面表的字段数据产生了改变
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
} #endregion #region INotifyPropertyChanging Members public event PropertyChangingEventHandler PropertyChanging; // 用来通知数据上下文表的字段数据将要产生改变
private void NotifyPropertyChanging(string propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
} #endregion }
}
员工数据库EmployeeDataContext继承DataContext
using System.Data.Linq; namespace SQLServerDemo
{
//定义一个EmployeeDataContext类来继承DataContext,在EmployeeDataContext中定义数据库连接字符串,以及员工信息表
public class EmployeeDataContext : DataContext
{
// 数据库链接字符串
public static string DBConnectionString = "Data Source=isostore:/Employee.sdf"; // 传递数据库连接字符串到DataContext基类
public EmployeeDataContext(string connectionString)
: base(connectionString)
{ } // 定义一个员工信息表
public Table<EmployeeTable> Employees;
}
}
创建页面数据绑定集合
using System.ComponentModel;
using System.Collections.ObjectModel; namespace SQLServerDemo
{
//EmployeeCollection用于跟页面的数据绑定
public class EmployeeCollection : INotifyPropertyChanged
{
//定义ObservableCollection来绑定页面的数据
private ObservableCollection<EmployeeTable> _employeeTables;
public ObservableCollection<EmployeeTable> EmployeeTables
{
get
{
return _employeeTables;
}
set
{
if (_employeeTables != value)
{
_employeeTables = value;
NotifyPropertyChanged("EmployeeTables");
}
}
} #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; //用于通知属性的改变
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
在App.cs 的Launching添加数据库检查
// Code to execute when the application is launching (eg, from Start)
// This code will not execute when the application is reactivated
private void Application_Launching(object sender, LaunchingEventArgs e)
{
//如果数据库不存在则创建一个数据库
using (EmployeeDataContext db = new EmployeeDataContext(EmployeeDataContext.DBConnectionString))
{
if (db.DatabaseExists() == false)
{
//创建一个数据库
db.CreateDatabase();
}
}
}
MainPage.xaml.cs
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel; namespace SQLServerDemo
{
public partial class MainPage : PhoneApplicationPage
{
// 创建DataContext实例用于用于操作本地的数据库
private EmployeeDataContext employeeDB;
private EmployeeCollection employeeCol = new EmployeeCollection(); public MainPage()
{
InitializeComponent(); //连接数据库并初始化DataContext实例
employeeDB = new EmployeeDataContext(EmployeeDataContext.DBConnectionString); // 使用Linq查询语句查询EmployeeTable表的所有数据
var employeesInDB = from EmployeeTable employee in employeeDB.Employees
select employee; // 将查询的结果返回到页面数据绑定的集合里面
employeeCol.EmployeeTables = new ObservableCollection<EmployeeTable>(employeesInDB); //赋值给当前页面的DataContext用于数据绑定
this.DataContext = employeeCol;
} /// <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)
{
//获取当前按钮绑定的DataContext,即当前的删除的EmployeeTable实例
EmployeeTable employeeForDelete = button.DataContext as EmployeeTable;
//移除绑定集合里面要删除的EmployeeTable记录
employeeCol.EmployeeTables.Remove(employeeForDelete);
// 移除数据库里面要删除的EmployeeTable记录
employeeDB.Employees.DeleteOnSubmit(employeeForDelete);
//保存数据库的改变
employeeDB.SubmitChanges();
}
} /// <summary>
/// 保存操作,处理新增和编辑员工信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void addButton_Click(object sender, RoutedEventArgs e)
{
//控制员工名字和简介不能为空
if (name.Text != "" && desc.Text != "")
{
if (State.Count> && State["employee"] != null )//编辑状态
{
//获取编辑的EmployeeTable对象
EmployeeTable employee = (EmployeeTable)State["employee"];
employee.EmployeeName = name.Text;
employee.EmployeeDesc = desc.Text;
//保存数据库的改变
employeeDB.SubmitChanges();
//添加绑定集合的数据,因为在单击编辑的时候移除了
employeeCol.EmployeeTables.Add(employee);
State["employee"] = null;
}
else//新增状态
{
//创建一条表的数据
EmployeeTable newEmployee = new EmployeeTable { EmployeeName = name.Text, EmployeeDesc = desc.Text };
//添加绑定集合的数据
employeeCol.EmployeeTables.Add(newEmployee);
//插入数据库
employeeDB.Employees.InsertOnSubmit(newEmployee);
//保存数据库的改变
employeeDB.SubmitChanges();
} name.Text = "";
desc.Text = "";
}
else
{
MessageBox.Show("姓名和简介不能为空!");
}
} /// <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)
{
//获取当前按钮绑定的DataContext,即当前的编辑的EmployeeTable实例
EmployeeTable employeeForEdit = button.DataContext as EmployeeTable;
name.Text = employeeForEdit.EmployeeName;
desc.Text = employeeForEdit.EmployeeDesc;
//将需要编辑的表实例存储在State里面
State["employee"] = employeeForEdit;
employeeCol.EmployeeTables.Remove(employeeForEdit);
}
}
}
}