当前位置:数据库 > SQLServer >>

Windows phone数据库SQLCE

在wp 7.1中终于可以使用SQLCE了,要使用sqlce需要添加system.data.linq的命名空间的引用

要使用sqlce,首先要建立一个实体类,表示数据库中的一行的实例

using System.Data.Linq.Mapping;

using System.ComponentModel;

namespace WindowsPhoneSQLCE.App_Code

{   

 [Table]   

 public class Person : INotifyPropertyChanged, INotifyPropertyChanging  

  {      

  private int _id;

        //用户编号,该列自动生成,不可为空  

      [Column(IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false)]       

      public int Id       

     {         

      get { return _id; }           

      set

             {

                if (_id != value)

                {

                     OnPropertyChanging("Id");

                    _id = value;

                     OnPropertyChanged("Id");

                 }

                 _id = value;

             }

         }

        private string _name;

        //用户名称

         [Column]

        public string Name

         {

             get { return _name; }

             set

            {

                _name = value;

            }

        }

        private string _job;

        //用户工作

        [Column]

         public string Job

        {

             get { return _job; }

             set { _job = value; }

         }

        //属性更改完毕事件

         public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)

         {

             if (PropertyChanged != null)

             {

                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

             }

         }

        //属性更改事件

         public event PropertyChangingEventHandler PropertyChanging;

        public void OnPropertyChanging(string propertyName)

         { 

           if (PropertyChanging != null) 

           { 

               PropertyChanging(this, new PropertyChangingEventArgs(propertyName));

             }  

      }

    }

 }

 

建立一个数据上下文

using System.Data.Linq;

namespace WindowsPhoneSQLCE.App_Code

 {

     //Person数据上下文

     public class PersonDataContext : DataContext

     {

         private const string dbConstring = "Data Source=isostore:/db.sdf";

        public PersonDataContext()

             : base(dbConstring) 

       {

        }

        public Table<Person> person;

     }

}

 

 

使用时 的前台布局

<phone:PhoneApplicationPage

     x:Class="WindowsPhoneSQLCE.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">

    <!--LayoutRoot is the root grid where all page content is placed-->

    <Grid x:Name="LayoutRoot" Background="Transparent">

        <Grid.RowDefinitions> 

           <RowDefinition Height="Auto"/>

        &nbs

补充:移动开发 , Windows Phone ,
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,