SQL Metal在生成Windows Phone数据库

简介:
如果在winsows phone中使用数据库,可以使用SQLMetal,它不但能把SQL Server中的数据生成实体应用在WP中,还可以很方便的使用Linq来对数据库各实体进行操作。可以说是手机开发与桌面及web开发实现无缝的开发体验。
1、  创建 SQL 数据库
 

 
2、  SQL 库导成实体 CSharp 源代码
安装完WP开发环境后,会在安装路径下安装一个工具sqletal,这个工具可以帮助我们把SQL Server数据库转换成WP中应用的CSharp源代码。
在cmd命令下键入:
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin>sqlmetal /server:. /database:accou
nt_db /namespace:test /code:f:/test.cs /language:csharp
server:指sql server服务器名
database:数据库名
namespace:生成的命名空间
code:生成源代码的路径和文件名
language:生成的语言
 
生成的源码如下:
 

 
 
  1. #pragma warning disable 1591  
  2. namespace test  
  3. {  
  4.          using System.Data.Linq;  
  5.          using System.Data.Linq.Mapping;  
  6.          using System.Data;  
  7.          using System.Collections.Generic;  
  8.          using System.Reflection;  
  9.          using System.Linq;  
  10.          using System.Linq.Expressions;  
  11.          using System.ComponentModel;  
  12.          using System;  
  13.            
  14.            
  15.          [System.Data.Linq.Mapping.DatabaseAttribute(Name="account_db")]  
  16.          public partial class Account_db : System.Data.Linq.DataContext  
  17.          {  
  18.                      
  19.                    private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();  
  20.                      
  21.     #region Extensibility Method Definitions  
  22.     partial void OnCreated();  
  23.     partial void InsertAccounts(Accounts instance);  
  24.     partial void UpdateAccounts(Accounts instance);  
  25.     partial void DeleteAccounts(Accounts instance);  
  26.     partial void InsertItems(Items instance);  
  27.     partial void UpdateItems(Items instance);  
  28.     partial void DeleteItems(Items instance);  
  29.     #endregion  
  30.                      
  31.                    public Account_db(string connection) :   
  32.                                      base(connection, mappingSource)  
  33.                    {  
  34.                             OnCreated();  
  35.                    }  
  36.                      
  37.                    public Account_db(System.Data.IDbConnection connection) :   
  38.                                      base(connection, mappingSource)  
  39.                    {  
  40.                             OnCreated();  
  41.                    }  
  42.                      
  43.                    public Account_db(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :   
  44.                                      base(connection, mappingSource)  
  45.                    {  
  46.                             OnCreated();  
  47.                    }  
  48.                      
  49.                    public Account_db(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :   
  50.                                      base(connection, mappingSource)  
  51.                    {  
  52.                             OnCreated();  
  53.                    }  
  54.                      
  55.                    public System.Data.Linq.Table<Accounts> Accounts  
  56.                    {  
  57.                             get 
  58.                             {  
  59.                                      return this.GetTable<Accounts>();  
  60.                             }  
  61.                    }  
  62.                      
  63.                    public System.Data.Linq.Table<Items> Items  
  64.                    {  
  65.                             get 
  66.                             {  
  67.                                      return this.GetTable<Items>();  
  68.                             }  
  69.                    }  
  70.          }  
  71.            
  72.          [Table(Name="dbo.Accounts")]  
  73.          public partial class Accounts : INotifyPropertyChanging, INotifyPropertyChanged  
  74.          {  
  75.                      
  76.                    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);  
  77.                      
  78.                    private int _ID;  
  79.                      
  80.                    private System.Nullable<decimal> _Amount;  
  81.                      
  82.                    private System.Nullable<System.DateTime> _Dtime;  
  83.                      
  84.                    private System.Nullable<int> _ItemID;  
  85.                      
  86.                    private EntityRef<Items> _Items;  
  87.                      
  88.     #region Extensibility Method Definitions  
  89.     partial void OnLoaded();  
  90.     partial void OnValidate(System.Data.Linq.ChangeAction action);  
  91.     partial void OnCreated();  
  92.     partial void OnIDChanging(int value);  
  93.     partial void OnIDChanged();  
  94.     partial void OnAmountChanging(System.Nullable<decimal> value);  
  95.     partial void OnAmountChanged();  
  96.     partial void OnDtimeChanging(System.Nullable<System.DateTime> value);  
  97.     partial void OnDtimeChanged();  
  98.     partial void OnItemIDChanging(System.Nullable<int> value);  
  99.     partial void OnItemIDChanged();  
  100.     #endregion  
  101.                      
  102.                    public Accounts()  
  103.                    {  
  104.                             this._Items = default(EntityRef<Items>);  
  105.                             OnCreated();  
  106.                    }  
  107.                      
  108.                    [Column(Storage="_ID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]  
  109.                    public int ID  
  110.                    {  
  111.                             get 
  112.                             {  
  113.                                      return this._ID;  
  114.                             }  
  115.                             set 
  116.                             {  
  117.                                      if ((this._ID != value))  
  118.                                      {  
  119.                                                this.OnIDChanging(value);  
  120.                                                this.SendPropertyChanging();  
  121.                                                this._ID = value;  
  122.                                                this.SendPropertyChanged("ID");  
  123.                                                this.OnIDChanged();  
  124.                                      }  
  125.                             }  
  126.                    }  
  127.                      
  128.                    [Column(Storage="_Amount", DbType="Money")]  
  129.                    public System.Nullable<decimal> Amount  
  130.                    {  
  131.                             get 
  132.                             {  
  133.                                      return this._Amount;  
  134.                             }  
  135.                             set 
  136.                             {  
  137.                                      if ((this._Amount != value))  
  138.                                      {  
  139.                                                this.OnAmountChanging(value);  
  140.                                                this.SendPropertyChanging();  
  141.                                                this._Amount = value;  
  142.                                                this.SendPropertyChanged("Amount");  
  143.                                                this.OnAmountChanged();  
  144.                                      }  
  145.                             }  
  146.                    }  
  147.                      
  148.                    [Column(Storage="_Dtime", DbType="DateTime")]  
  149.                    public System.Nullable<System.DateTime> Dtime  
  150.                    {  
  151.                             get 
  152.                             {  
  153.                                      return this._Dtime;  
  154.                             }  
  155.                             set 
  156.                             {  
  157.                                      if ((this._Dtime != value))  
  158.                                      {  
  159.                                                this.OnDtimeChanging(value);  
  160.                                                this.SendPropertyChanging();  
  161.                                                this._Dtime = value;  
  162.                                                this.SendPropertyChanged("Dtime");  
  163.                                                this.OnDtimeChanged();  
  164.                                      }  
  165.                             }  
  166.                    }  
  167.                      
  168.                    [Column(Storage="_ItemID", DbType="Int")]  
  169.                    public System.Nullable<int> ItemID  
  170.                    {  
  171.                             get 
  172.                             {  
  173.                                      return this._ItemID;  
  174.                             }  
  175.                             set 
  176.                             {  
  177.                                      if ((this._ItemID != value))  
  178.                                      {  
  179.                                                if (this._Items.HasLoadedOrAssignedValue)  
  180.                                                {  
  181.                                                         throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();  
  182.                                                }  
  183.                                                this.OnItemIDChanging(value);  
  184.                                                this.SendPropertyChanging();  
  185.                                                this._ItemID = value;  
  186.                                                this.SendPropertyChanged("ItemID");  
  187.                                                this.OnItemIDChanged();  
  188.                                      }  
  189.                             }  
  190.                    }  
  191.                      
  192.                    [Association(Name="FK_Accounts_Items", Storage="_Items", ThisKey="ItemID", OtherKey="ID", IsForeignKey=true)]  
  193.                    public Items Items  
  194.                    {  
  195.                             get 
  196.                             {  
  197.                                      return this._Items.Entity;  
  198.                             }  
  199.                             set 
  200.                             {  
  201.                                      Items previousValue = this._Items.Entity;  
  202.                                      if (((previousValue != value)   
  203.                                                                  || (this._Items.HasLoadedOrAssignedValue == false)))  
  204.                                      {  
  205.                                                this.SendPropertyChanging();  
  206.                                                if ((previousValue != null))  
  207.                                                {  
  208.                                                         this._Items.Entity = null;  
  209.                                                         previousValue.Accounts.Remove(this);  
  210.                                                }  
  211.                                                this._Items.Entity = value;  
  212.                                                if ((value != null))  
  213.                                                {  
  214.                                                         value.Accounts.Add(this);  
  215.                                                         this._ItemID = value.ID;  
  216.                                                }  
  217.                                                else 
  218.                                                {  
  219.                                                         this._ItemID = default(Nullable<int>);  
  220.                                                }  
  221.                                                this.SendPropertyChanged("Items");  
  222.                                      }  
  223.                             }  
  224.                    }  
  225.                      
  226.                    public event PropertyChangingEventHandler PropertyChanging;  
  227.                      
  228.                    public event PropertyChangedEventHandler PropertyChanged;  
  229.                      
  230.                    protected virtual void SendPropertyChanging()  
  231.                    {  
  232.                             if ((this.PropertyChanging != null))  
  233.                             {  
  234.                                      this.PropertyChanging(this, emptyChangingEventArgs);  
  235.                             }  
  236.                    }  
  237.                      
  238.                    protected virtual void SendPropertyChanged(String propertyName)  
  239.                    {  
  240.                             if ((this.PropertyChanged != null))  
  241.                             {  
  242.                                      this.PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));  
  243.                             }  
  244.                    }  
  245.          }  
  246.            
  247.          [Table(Name="dbo.Items")]  
  248.          public partial class Items : INotifyPropertyChanging, INotifyPropertyChanged  
  249.          {  
  250.                      
  251.                    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);  
  252.                      
  253.                    private int _ID;  
  254.                      
  255.                    private string _ItemName;  
  256.                      
  257.                    private EntitySet<Accounts> _Accounts;  
  258.                      
  259.     #region Extensibility Method Definitions  
  260.     partial void OnLoaded();  
  261.     partial void OnValidate(System.Data.Linq.ChangeAction action);  
  262.     partial void OnCreated();  
  263.     partial void OnIDChanging(int value);  
  264.     partial void OnIDChanged();  
  265.     partial void OnItemNameChanging(string value);  
  266.     partial void OnItemNameChanged();  
  267.     #endregion  
  268.                      
  269.                    public Items()  
  270.                    {  
  271.                             this._Accounts = new EntitySet<Accounts>(new Action<Accounts>(this.attach_Accounts), new Action<Accounts>(this.detach_Accounts));  
  272.                             OnCreated();  
  273.                    }  
  274.                      
  275.                    [Column(Storage="_ID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]  
  276.                    public int ID  
  277.                    {  
  278.                             get 
  279.                             {  
  280.                                      return this._ID;  
  281.                             }  
  282.                             set 
  283.                             {  
  284.                                      if ((this._ID != value))  
  285.                                      {  
  286.                                                this.OnIDChanging(value);  
  287.                                                this.SendPropertyChanging();  
  288.                                                this._ID = value;  
  289.                                                this.SendPropertyChanged("ID");  
  290.                                                this.OnIDChanged();  
  291.                                      }  
  292.                             }  
  293.                    }  
  294.                      
  295.                    [Column(Storage="_ItemName", DbType="NVarChar(50)")]  
  296.                    public string ItemName  
  297.                    {  
  298.                             get 
  299.                             {  
  300.                                      return this._ItemName;  
  301.                             }  
  302.                             set 
  303.                             {  
  304.                                      if ((this._ItemName != value))  
  305.                                      {  
  306.                                                this.OnItemNameChanging(value);  
  307.                                                this.SendPropertyChanging();  
  308.                                                this._ItemName = value;  
  309.                                                this.SendPropertyChanged("ItemName");  
  310.                                                this.OnItemNameChanged();  
  311.                                      }  
  312.                             }  
  313.                    }  
  314.                      
  315.                    [Association(Name="FK_Accounts_Items", Storage="_Accounts", ThisKey="ID", OtherKey="ItemID", DeleteRule="NO ACTION")]  
  316.                    public EntitySet<Accounts> Accounts  
  317.                    {  
  318.                             get 
  319.                             {  
  320.                                      return this._Accounts;  
  321.                             }  
  322.                             set 
  323.                             {  
  324.                                      this._Accounts.Assign(value);  
  325.                             }  
  326.                    }  
  327.                      
  328.                    public event PropertyChangingEventHandler PropertyChanging;  
  329.                      
  330.                    public event PropertyChangedEventHandler PropertyChanged;  
  331.                      
  332.                    protected virtual void SendPropertyChanging()  
  333.                    {  
  334.                             if ((this.PropertyChanging != null))  
  335.                             {  
  336.                                      this.PropertyChanging(this, emptyChangingEventArgs);  
  337.                             }  
  338.                    }  
  339.                      
  340.                    protected virtual void SendPropertyChanged(String propertyName)  
  341.                    {  
  342.                             if ((this.PropertyChanged != null))  
  343.                             {  
  344.                                      this.PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));  
  345.                             }  
  346.                    }  
  347.                      
  348.                    private void attach_Accounts(Accounts entity)  
  349.                    {  
  350.                             this.SendPropertyChanging();  
  351.                             entity.Items = this;  
  352.                    }                   
  353.                    private void detach_Accounts(Accounts entity)  
  354.                    {  
  355.                             this.SendPropertyChanging();  
  356.                             entity.Items = null;  
  357.                    }  
  358.          }  
  359. }  
  360. #pragma warning restore 1591 
 
3、  SQL 实体代码添加到项目中
A.        添加项目引用System.Data.Linq
B.        注释掉用System.Data.IDbConnection的构造函数,因为在WP中没有IDbConnection接口
C.        保持数据库实体对象中,表集合的命名是复数形式(注意是Account_db类中),即Accounts和Items
D.        实体类Table特性类的Name属性不要带dbo,如下:
[ Table(Name="dbo.Accounts")]改成[ Table(Name="Accounts")]
E.   实体类中的属性如果是string类型,最好用Column特性的DbType命名参数用NVarchar,如下:[ Column(Storage="_ItemName", DbType="NVarChar(50)")]
 
4、  Windows Phone 下生成数据库
可以在App类的Application_Launching事件方法中写入如下代码来创建WP中的数据库对象:
 

 
 
  1. using (Test.Account_db db = new Account_db("Data Source='isostore:/test.sdf';Password=123"))  
  2.             {                  
  3.                 if (db.DatabaseExists() == false)  
  4.                 {  
  5.                     db.CreateDatabase();  
  6.                 }  
  7.             } 
 
5、  操作数据
操作数据的方法与 web和桌面开发是相同的,使用Linq To SQL方法即可。下面是Items实体对象的添加。
 

  
  
  1. Account_db accountdb = new Account_db("Data Source='isostore:/test.sdf';Password='saiko2011'");  
  2.             Items item = new Items();  
  3.             item.ItemName = "收入";  
  4.             accountdb.Items.InsertOnSubmit(item);  
  5.             accountdb.SubmitChanges();  
  6.             lb.ItemsSource = accountdb.Items; 

 














本文转自桂素伟51CTO博客,原文链接:http://blog.51cto.com/axzxs/762688 ,如需转载请自行联系原作者

相关文章
|
4月前
|
SQL 机器学习/深度学习 人工智能
从“写SQL”到“聊数据”:NL2SQL如何用自然语言解锁数据库?
本文系统性地阐述了自然语言转SQL(NL2SQL) 技术如何让非技术背景的业务分析师实现数据自助查询,从而提升数据驱动决策的效率与准确性。
从“写SQL”到“聊数据”:NL2SQL如何用自然语言解锁数据库?
|
3月前
|
SQL 人工智能 Linux
SQL Server 2025 RC1 发布 - 从本地到云端的 AI 就绪企业数据库
SQL Server 2025 RC1 发布 - 从本地到云端的 AI 就绪企业数据库
361 5
SQL Server 2025 RC1 发布 - 从本地到云端的 AI 就绪企业数据库
|
2月前
|
SQL 存储 监控
SQL日志优化策略:提升数据库日志记录效率
通过以上方法结合起来运行调整方案, 可以显著地提升SQL环境下面向各种搜索引擎服务平台所需要满足标准条件下之数据库登记作业流程综合表现; 同时还能确保系统稳健运行并满越用户体验预期目标.
183 6
|
3月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
|
3月前
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
3月前
|
SQL Oracle 关系型数据库
Oracle数据库创建表空间和索引的SQL语法示例
以上SQL语法提供了一种标准方式去组织Oracle数据库内部结构,并且通过合理使用可以显著改善查询速度及整体性能。需要注意,在实际应用过程当中应该根据具体业务需求、系统资源状况以及预期目标去合理规划并调整参数设置以达到最佳效果。
287 8
|
4月前
|
SQL 人工智能 Java
用 LangChain4j+Ollama 打造 Text-to-SQL AI Agent,数据库想问就问
本文介绍了如何利用AI技术简化SQL查询操作,让不懂技术的用户也能轻松从数据库中获取信息。通过本地部署PostgreSQL数据库和Ollama模型,结合Java代码,实现将自然语言问题自动转换为SQL查询,并将结果以易懂的方式呈现。整个流程简单直观,适合初学者动手实践,同时也展示了AI在数据查询中的潜力与局限。
434 8
|
4月前
|
SQL 人工智能 Linux
SQL Server 2025 RC0 发布 - 从本地到云端的 AI 就绪企业数据库
SQL Server 2025 RC0 发布 - 从本地到云端的 AI 就绪企业数据库
264 5
|
6月前
|
SQL 关系型数据库 MySQL
Go语言数据库编程:使用 `database/sql` 与 MySQL/PostgreSQL
Go语言通过`database/sql`标准库提供统一数据库操作接口,支持MySQL、PostgreSQL等多种数据库。本文介绍了驱动安装、连接数据库、基本增删改查操作、预处理语句、事务处理及错误管理等内容,涵盖实际开发中常用的技巧与注意事项,适合快速掌握Go语言数据库编程基础。
470 62
|
3月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。

热门文章

最新文章