zoukankan      html  css  js  c++  java
  • Window Phone中使用本地数据库SQLCE的简单入门

    window phone 7.1的SDK已经内置了SQLCE引擎,我们就完全可以使用SQLCE关系数据库为我们的应用程序的管理存储数据。

    在window phone操作SQLCE主要使用linq to sql的技术知识,如果你对linq to sql比较熟悉,那么用sqlce是很简单,

    我对linq to sql不太熟悉,一边学一边操作了

    当然在window phone上的linq to sql 有很多限制,功能有很多弱化,原生态的SQL语句是不被支持,等等其他

    另外window phone项目必须添加对LInq的动态链接库引用(system.data.linq)

    我的主要学习参考资料在下面,目前主要简单学习操作数据库和数据的添加,删除,修改,查找。当然还有很多比较复杂的数据操作我还没深入学习,

    这个文章当作入门引子吧

    一个第三方的系列文章,推荐先看这个

    http://windowsphonegeek.com/tips/Windows-Phone-Mango-Local-Database(SQL-CE)-Introduction

    这是微软官方的文档,某些语法或者方法,属性详细查找应该在这里查找

    http://msdn.microsoft.com/en-us/library/ff626522%28v=vs.92%29.aspx

    一。数据表和数据库的定义

    定义一个Country数据类,也就是数据库中的表,linq会保持映射关系

    上面的标签属性很简单,看一下就明白了,

    [Table(Name="Country")]
    public class Country
    {
    [Column(IsPrimaryKey=true,IsDbGenerated=true)]//指定是主键,而且是自增的
    public int CountryID
    {
    get;
    set;
    }
    [Column(CanBeNull = true, DbType = "NVarChar(100) not null")]//定义了数据类型
    public string CountryName
    {
    get;
    set;
    }
    }

    定义数据上下文,也可以认为就是数据库关系

    必须继承DataContext,下面的定义表面这个数据库中有一个表Country。

    connectionString这是链接字符串
     public class WPContext : DataContext
    {
    public WPContext(string connectionString)
    : base(connectionString)
    {
    }

    public Table<Country> Countries
    {
    get
    {
    return this.GetTable<Country>();
    }
    }
    }


    二。在程序中创建数据上下文,也就是数据库

    传统上创建或者链接数据库都离不开连接字符串,window phone也一样,语法稍微有点变化

    isostore是关键字表面是在独立存储区域内,sdf就是数据库文件

    string connectionString = "Data Source='isostore:/file.sdf'";

    这个语法指定了文化特性

    private const string ConnectionString = @"Data Source = CountryDB.sdf'; Culture Identifier = fr-FR; Case Sensitive = true;";
     
    这个语法指定了加密的密码
    private const string ConnectionString = @"Data Source='isostore:/CountryDB.sdf';Password='MyPassword';";
     
    下面的代码就创建了file数据库。
    定义自己的WPContext 的时候一定要内部有表数据,
     
     string connectionString = "Data Source='isostore:/file.sdf'";
    using (WPContext context = new WPContext(connectionString))
    {
    //context.DeleteDatabase();
    if (!context.DatabaseExists())
    {
    context.CreateDatabase();
    }
    }

    创建数据上下文以后就可以对Country表进行数据操作。

    所有的操作必须在数据上下文环境中进行,也就是using语法的闭合内

    数据添加

                    Country model = new Country();
    model.CountryName = "China2";
    context.Countries.InsertOnSubmit(model);

    context.SubmitChanges();



    数据查询

     IQueryable<Country> query = from c in context.Countries select c;
    IList<Country> temp = query.ToList();

    数据查找,更新和删除

                    IQueryable<Country> query2 = from c in context.Countries where c.CountryID==1 select c;
    Country c2 = query2.FirstOrDefault();
    c2.CountryName = "update By";
    context.SubmitChanges();
             //删除
                    context.Countries.DeleteOnSubmit(c2);
                    context.SubmitChanges();


    以上代码在模拟器中可以顺利运行。


    以上学习和代码都是比较简单入门,更复杂的应用还有待继续研究!

  • 相关阅读:
    CSP游戏 4
    CSP 交通规划
    CSP 地铁修建
    CSP 通信网络
    CSP URL映射
    CSP 权限查询
    CSP Markdown
    CSP JSON 查询
    SQL里的子查询
    SQL里的操作符
  • 原文地址:https://www.cnblogs.com/zjypp/p/2373614.html
Copyright © 2011-2022 走看看