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 > 0 && 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:数据库文件同样是在独立存储空间中。

 

转载于:https://www.cnblogs.com/ListenFly/p/3333546.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
sql server ce 的工具 非常有用 SQL Server 2005 移动版(SQL Server Mobile)或SQL Server 2000 Windows CE 2.0版(SQL Server CE 2.0)的企业和个人用户如果计划与SQL Server 2000或SQL Server 2005数据库保持同步,需要在您运行Microsoft Internet Information Services(IIS)的服务器上安装互联工具。 本页内容表述的是互联工具。请注意文件名中的“xx”是代表安装语言的标识符。最初仅有英文互联工具信息(“en”即标识符“XX”)。为满足向后兼容的需要,我们提供SQL Server CE 2.0复制软件。 请查阅安装互联工具的设置说明。 1. Microsoft SQL Server 2005移动版服务器工具(sqlce30setupxx.msi)在IIS 箱中安装SQL Server Mobile复制组件。 这个组件用于把移动设备中的SQL Server Mobile连接到SQL Server 2005、SQL Server 2000 SP3a、及 SQL Server 2000 SP4数据库。 2. Microsoft SQL Server 2000 Service Pack 4复制组件(sql2kxxsp4.msi)在IIS机器中安装SQL Server 2000 SP3a复制组件。这个组件用于把移动设备中的SQL Mobile数据库连接到SQL Server 2000 SP4数据库。 3. Microsoft SQL Server 2000 Service Pack 3a复制组件(sql2kxxsp3a.msi)在IIS机器中安装 SQL Server CE 2.0 及 SQL Server 2000 SP4 复制组件。 该组件用于把移动设备中的SQL Server CE 2.0数据库连接到SQL Server 2000 SP3a数据库。 4. 用于SQL Server 2000 SP4 (sqlce20sql2ksp4.exe)的SQL Server CE 2.0 复制组件在IIS机器中安装 SQL Server CE 2.0 及 SQL Server 2000 SP4 复制组件。该组件用于把移动设备中的SQL Server CE 2.0数据库连接到SQL Server 2000 SP4。 5. 用于SQL Server 2000 SP3a (sqlce20sql2ksp3a.exe)的SQL Server CE 2.0复制组件在IIS机器中安装SQL Server CE 2.0 及 SQL Server 2000 SP3a 复制组件。该组件用于把移动设备中的SQL Server CE 2.0数据库连接到SQL Server 2000 SP3a 数据库

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值