/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