1.创建实体 注意加Table和Column特性
/// <summary>
/// 课程类
/// </summary>
[Table] //表示类将成为一个table
public class Course : INotifyPropertyChanged, INotifyPropertyChanging
{
[Column(IsVersion = true)] //table的列
private Binary _version;
private int _id;
[Column(IsPrimaryKey=true,IsDbGenerated=true)] //table的列,主键,自动生成
public int Id
{
get { return _id; }
set
{
if (_id != value)
{
RaiseProtertyChanging("Id");
_id = value;
RaisePropertyChanged("Id");
}
}
}
private string _name;
[Column]
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
RaiseProtertyChanging("Name");
_name = value;
RaisePropertyChanged("Name");
}
}
}
private string _location;
[Column]
public string Location
{
get { return _location; }
set
{
if (_location != value)
{
RaiseProtertyChanging("Location");
_location = value;
RaisePropertyChanged("Location");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangingEventHandler PropertyChanging;
private void RaiseProtertyChanging(string propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
}
2.创建类继承DataContext,而且封装this.GetTable<Course>()方法,否则创建数据库时报错
public class MyDataContext : DataContext
{
//连接字符窜
public const string ConnectionString = "Data Source=isostore:/MyDb.sdf";
//构造函数
public MyDataContext()
: base(ConnectionString)
{
if (!this.DatabaseExists())
{
//创建数据库
this.CreateDatabase();
}
}
//必须存在,否则创建数据库报错:DataContext不存在表
public Table<Course> CourseTable
{
get { return this.GetTable<Course>(); }
}
}
3.页面前台代码
<!--ContentPanel - 在此处放置其他内容-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Button Content="添加" Click="Button_Click" />
<Button Content="编辑" Click="Button_Click_1" />
<Button Content="删除" Click="Button_Click_2" />
</StackPanel>
<ListBox Grid.Row="1" Name="CourseList">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Id,Mode=TwoWay}"/>
<TextBlock Grid.Column="1" Text="{Binding Name,Mode=TwoWay}"/>
<TextBlock Grid.Column="2" Text="{Binding Location,Mode=TwoWay}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
4.页面后台代码
添加:调用GetTable<T>().InsertOnSubmit(T)方法,最后调用SubmitChanges()向数据库提交数据。
删除: GetTable<T>().DeleteOnSubmit(T)方法,最后调用SubmitChanges()向数据库提交数据。
编辑:调用SubmitChanges()
页面绑定的数据源必须是ObservableCollection类型
public partial class MainPage : PhoneApplicationPage
{
private ObservableCollection<Course> Courses;
private DataContext _data;
// 构造函数
public MainPage()
{
InitializeComponent();
//创建数据库
//CreateDatabase();
_data = new MyDataContext();
//初始化数据
InitData();
}
private void CreateDatabase()
{
_data = new MyDataContext();
if (!_data.DatabaseExists())
{
_data.CreateDatabase();
}
}
private void InitData()
{
Courses = new ObservableCollection<Course>
{
new Course{Name="电子商务",Location="教学楼101"},
new Course{Name="心理学",Location="教学楼101"},
new Course{Name="高等数学",Location="教学楼101"},
new Course{Name="网络营销",Location="教学楼101"},
};
foreach (var c in Courses)
{
_data.GetTable<Course>().InsertOnSubmit(c);//插入数据库
}
_data.SubmitChanges();
this.CourseList.ItemsSource = Courses;
}
//添加事件
private void Button_Click(object sender, RoutedEventArgs e)
{
Course c = new Course()
{
Name = "客户关系管理",
Location = "教学楼401"
};
Courses.Add(c);
_data.GetTable<Course>().InsertOnSubmit(c);
_data.SubmitChanges();
}
//编辑事件
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Course c = Courses.First(s => s.Name == "网络营销");
c.Location = "编辑教学楼";
_data.SubmitChanges();
}
//删除事件
private void Button_Click_2(object sender, RoutedEventArgs e)
{
Course c = Courses.First(s => s.Name == "电子商务");
Courses.Remove(c);
_data.GetTable<Course>().DeleteOnSubmit(c);
_data.SubmitChanges();
}
}