NBearLite是一个基于NBear的强类型查询内核的.NET 2.0开源(BSD协议)的通用数据访问组件。由
NBear.org及
Teddy维护。NBearLite继承了和
进一步简化扩展了NBear中类似LINQ的强类型数据库查询语法(支持CRUD简单查询及GroupBy、InnerJoin,Paging等常用复杂查询),简化了命名空间、代码架构,并进一步简化了用户使用,对已有代码也没有任何侵入。NBearLite不是一个ORM工具,它不涉及
实体类,NBearLite的所有查询返回Scalar/DataSet/IDataReader。
使用演示
无论是在新建的程序还是已有的程序中使用NBearLite,步骤都差不多,非常简单。
1、使用NBearLite提供的NBearLite.QueryColumnsGenerator.exe工具从已有数据库(可以是SqlServer、Oracle、MsAccess、MySql、Sqlite或
PostgreSql),为表和视图生成强类型查询辅助类,并将生成的代码文件引用到项目工程。
2、在项目中引用NBearLite.dll(该DLL内置了SqlServer、Oracle和MsAccess的DbProvider,如果需要使用MySql、Sqlite或
PostgreSql数据库,则还需要引用NBearLite.AdditionalDbProviders.dll)。
3、在项目的Web.config或App.config中的ConnectionStrings配置块中添加connectionstring。添加的语法可以和ADO.NET配置的标准语法一样,也可以简化设置,将
providerName
设为空(默认为sqlserver), sqlserver, sqlserver2005, msaccess, oracle, mysql,sqlite或postgresql。4、接着就可以直接在项目中实例化NBearLite.Database对象,并使用Database对象访问数据库了。具体的使用代码可以参见NBearLite.Test工程。例如:在NBearLite.Test工程中,SampleQueryColumnsDefinition.cs文件中保存了由NBearLite.QueryColumnsGenerator.exe工具生成的Northwind数据库的所有表和视图的强类型查询辅助类。在DatabaseTest.cs文件中,包含了一些查询演示:
1
[TestMethod]
2
public
void
TestInsert()
3
{ 4 Assert.AreEqual(1, db.Insert(Northwind.Categories).AddColumn(Northwind.Categories.CategoryName, "test1").Execute()); 5 Assert.IsTrue(db.Insert(Northwind.Categories).AddColumn(Northwind.Categories.CategoryName, "test1").ExecuteReturnAutoIncrementID(Northwind.Categories.CategoryID) > 1); 6 }
7
8
[TestMethod]
9
public
void
TestUpdate()
10
{11 Assert.AreEqual(1, db.Insert(Northwind.Categories).AddColumn(Northwind.Categories.CategoryName, "test1").Execute());12 Assert.IsTrue(db.Update(Northwind.Categories).AddColumn(Northwind.Categories.CategoryName, "test1").Where(Northwind.Categories.CategoryName == "test1").Execute() > 0);13 }
14
15
[TestMethod]
16
public
void
TestDelete()
17
{18 Assert.AreEqual(1, db.Insert(Northwind.Categories).AddColumn(Northwind.Categories.CategoryName, "test111").Execute());19 Assert.IsTrue(db.Delete(Northwind.Categories).Where(Northwind.Categories.CategoryName == "test111").Execute() > 0);20 }
21
22
[TestMethod]
23
public
void
TestSelect()
24
{25 DataSet ds = db.Select(Northwind.Categories).ToDataSet();26 Assert.IsTrue(ds.Tables[0].Rows.Count > 0);27 ds = db.Select(Northwind.Categories, Northwind.Categories.CategoryName).Where(Northwind.Categories.CategoryID > 0).ToDataSet();28 Assert.IsTrue(ds.Tables[0].Rows.Count > 0);29 ds = db.Select(Northwind.Categories, Northwind.Categories.CategoryName).GroupBy(Northwind.Categories.CategoryName).OrderBy(Northwind.Categories.CategoryName.Desc).SetSelectRange(2, 2, Northwind.Categories.CategoryName).ToDataSet();30 ds = db.Select(Northwind.Categories, Northwind.Categories.__Alias("CategoriesAlias").CategoryName).Join(Northwind.Categories, "CategoriesAlias", Northwind.Categories.CategoryID == Northwind.Categories.__Alias("CategoriesAlias").CategoryID).31 SetSelectRange(2, 2, Northwind.Categories.CategoryID).Where(Northwind.Categories.CategoryName.Length > 0 && Northwind.Categories.__Alias("CategoriesAlias").Description != null).32 ToDataSet();33 }
前三个Test都比较容易理解。TestSelect()中,分别演示了简单查询,分页GroupBy查询和一个自关联Join分页查询。除了以上演示的代码之外,还可以使用Database.CustomSql()/Database.StoredProcedure()方法查询自定义sql或存储过程。另外,NBearLite.QueryColumnsGenerator.exe工具本身也是一个使用NBearLite查询数据库的实际的实例程序。