在Windows Phone的第一个版本7.0版本里面是没有本地数据库支持的,要使用数据库只能够使用第三方的数据库组件。Windows Phone的本地数据库SQL Server CE是7.1版本即芒果更新的新特性,所以你要在应用程序中使用SQL Server CE数据库必须使用Windows Phone 7.1的API才行。
下面用一个实例演示如何使用SQL Server CE数据库。
(1)创建数据表以及数据库的数据上下文DateContent
先创建一个员工信息表,用于保存员工的名字和简介,员工表有一个自增的ID。
EmployeeTable.cs创建数据库的DataContent,定义一个EmployeeDataContext类来继承DataContext,在EmployeeDataContext中定义数据库连接字符串,以及员工信息表。
EmployeeDataContext.cs- using System.Data.Linq;
- namespace SQLServerDemo
- {
- public class EmployeeDataContext : DataContext
- {
- // 数据库链接字符串
- public static string DBConnectionString = "Data Source=isostore:/Employee.sdf";
- // 传递数据库连接字符串到DataContext基类
- public EmployeeDataContext(string connectionString)
- : base(connectionString)
- { }
- // 定义一个员工信息表
- public Table<EmployeeTable> Employees;
- }
- }
复制代码 (2) 创建页面数据绑定的集合
EmployeeCollection.cs- 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
- }
- }
复制代码 (3)创建数据库,绑定数据,实现员工信息表的增删改查操作。
在App.xaml.cs中的程序加载事件中进行创建数据库- private void Application_Launching(object sender, LaunchingEventArgs e)
- {
- //如果数据库不存在则创建一个数据库
- using (EmployeeDataContext db = new EmployeeDataContext(EmployeeDataContext.DBConnectionString))
- {
- if (db.DatabaseExists() == false)
- {
- //创建一个数据库
- db.CreateDatabase();
- }
- }
- }
复制代码 MainPage.xaml文件代码- <phone:PhoneApplicationPage
- x:Class="SQLServerDemo.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
- xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
- FontFamily="{StaticResource PhoneFontFamilyNormal}"
- FontSize="{StaticResource PhoneFontSizeNormal}"
- Foreground="{StaticResource PhoneForegroundBrush}"
- SupportedOrientations="Portrait" Orientation="Portrait"
- shell:SystemTray.IsVisible="True">
- <Grid x:Name="LayoutRoot" Background="Transparent">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="*"/>
- </Grid.RowDefinitions>
- <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
- <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
- <TextBlock x:Name="PageTitle" Text="SQL Server" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
- </StackPanel>
- <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="addButton"
- Click="addButton_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>
- </Grid>
- </phone:PhoneApplicationPage>
复制代码 MainPage.xaml.cs文件代码运行效果如下:
|