/Files/humble/MOON.ORM使用方法.doc
加入群:
1.背景
针对Qin.Data的架构设计反应出的一些问题进行了全新的架构设计,弥补了多数据源使用不便、同道反应不过ORM、自身架构的瑕疵等问题.
2.介绍
Moon .ORM是一个通用数据库处理框架(可以包含MSSQL POSTGRESQL,SQLITE EXCEL MYSQL DB2 ORACLE...只要你愿意实现接口就可以).很便捷地进行常用数据库操作(增删改查).其性能是几近纯ADO.NET.对于实体的查询采用emit实 现,如果您还不满意可用此框架的代码生成器直接生成纯ADO.NET SQL形式.其主要特色就是性能和便捷的操作.
3.特色
1.高性能(该框架采用纯的ADO.NET进行框架,避免Linq以及反射带来的性能损失);
2.易用性强(配置简单,智能感知,代码生成器的辅助,会sql就可(可以自我性能优化)) ;
3.多数据库支持(如果需要可自我扩增,热烈欢迎同道加入团队开发中(联系qq:564064202))
4.强大查询语法糖功能
5.多数据源支持
6..net framework 2.0原生支持
4.配置简单
<appSettings>
<add key="dbType" value="MSSQL" />
<!--数据库的类型 还可以写MYSQL,SQLITE,ACCESS等....—>
<add key="linkString" value="Server=mainserver;database=HD01SystemDB;Uid=sa;Pwd=123" />
</appSettings>
代码功能演示
using System;
using System.Collections.Generic;
using Moon.Orm;
using MoonDB;
namespace r
{
class Program
{
public static void Main( string[] args)
{
// 数据添加
PersonSet person= new PersonSet();
person.Age= 133;
person.AgePeriod= 1;
person.IsBeiJing= true;
person.Sex= true;
person.UserName= " 秦仕川 ";
DBFactory.Add(person);
Console.WriteLine( " 新的数据唯一识别标志: "+person.GetOnlyMark());
// 另类数据添加
person.Set(PersonSetTable.UserName, " 另类 ");
person.Set(PersonSetTable.Age, 12);
person.Set(PersonSetTable.AgePeriod, 11);
person.Set(PersonSetTable.IsBeiJing, false);
person.Set(PersonSetTable.Sex, true);
DBFactory.Add(person);
Console.WriteLine( " 新的数据11唯一识别标志: "+person.GetOnlyMark());
// 数据删除
long ret= DBFactory.DeleteWhen(PersonSetTable.IsBeiJing.Equal( 1).And(PersonSetTable.Age.BiggerThan( 12)));
Console.WriteLine( " 被删除的条数: "+ret);
// 改数据
person.UserName= " 另类修改后 ";
person.SetOnlyMark(PersonSetTable.UserName.Equal( " 另类 "));
DBFactory.Update(person);
// 查询
PersonSet p=DBFactory.GetEntity<PersonSet>(
PersonSetTable.UserName.Equal( " 另类修改后 "));
Console.WriteLine(p.Age);
// 查询一个字段
int age=DBFactory.GetOneField< int>(PersonSetTable.Age, PersonSetTable.ID.Equal( 5));
Console.WriteLine(age);
Console.Write( " Press any key to continue . . . ");
Console.ReadKey( true);
}
}
}
实体代码生成器
数据库升级问题(我们常常面临数据库表的变动问题)
Moon.ORM中不必担心这些东西,因为实体全由代码生成器生成,更新一次数据库,你重新生成一次DLL(代码生成器带有编译功能)
----------------------------------例子2--------------------
* 由SharpDevelop创建。
* 用户: qinshichuan
* 日期: 2011-12-18
* 时间: 16:47
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
using System.Collections.Generic;
using System.Data;
using Moon.Orm;
using MoonDB;
namespace r
{
class Program
{
public static void Main( string[] args)
{
// ------------------------
Console.WriteLine( " 1 排序操作-------- ");
List<UserSet> list=DBFactory.GetEntities<UserSet>(
UserSetTable.ID.BiggerThan( 0).OrderBy(UserSetTable.UserName, true));
foreach (UserSet element in list) {
Console.WriteLine(element.UserName+ " "+element.ID);
}
// -----------------------------------
Console.WriteLine( " 2 添加操作------ ");
UserSet user= new UserSet();
user.date=DateTime.Now;
user.datetime=DateTime.Now;
user.money=123M;
user.UserName= " 秦时明月 ";
object newID=DBFactory.Add(user);
Console.WriteLine( " 新增数据的主键为: "+newID);
// -----------------------------
Console.WriteLine( " 3 修改操作------ ");
UserSet updateUser= new UserSet();
updateUser.UserName= " 秦时明月-updateUser ";
// 设置更新目标
updateUser.SetOnlyMark(user.GetOnlyMark());
DBFactory.Update(updateUser);
Console.WriteLine( " 被修改的对象: "+updateUser.GetOnlyMark());
Console.WriteLine( " 修改后的对象值为: ");
string name=DBFactory.GetOneField< string>(UserSetTable.UserName ,updateUser.GetOnlyMark());
Console.WriteLine(name);
Console.Read();
}
}
}
=====================使用步骤=============
1创建数据库,注意表必须要有主键,建议为自增int64
2设置代码生成器的配置文件
<configuration>
<connectionStrings>
</connectionStrings>
<appSettings>
<add key= " dbType " value= " PostgreSql " />//数据库类型
<add key= " linkString " value= " Server=localhost;Database=MoonDB;User ID=postgres;Password=mingyue; " />//连接字符串
</appSettings>
</configuration>
生成代码.
3创建项目,引入Moon.ORM