设为首页 收藏本站
查看: 1008|回复: 0

[经验分享] 发布NBearLite v1.0.0: 提供强类型查询语法的非ORM数据访问组件 [8/2 更新至v1.0.0.9 beta

[复制链接]

尚未签到

发表于 2016-11-22 06:09:15 | 显示全部楼层 |阅读模式
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 DSC0000.gif         [TestMethod]
2        public void TestInsert()
3 DSC0001.gif DSC0002.gif          DSC0003.gif {
4 DSC0004.gif             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 DSC0005.gif         }
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查询数据库的实际的实例程序。


下载

NBearLite_v1.0.0.9 Beta 及中文版NBearLite完全参考手册

修订

7/21 更新至v1.0.0.2 beta - 新增PostgreSql DbProvider,Insert/Update/Select/Delete方法的参数有部分修改,请注意参考以上已更新的的实例代码。
7/22 更新至v1.0.0.3 beta - 修复PostgreSql DbProvider中的Bug。
7/24 更新至v1.0.0.4 beta - 全面支持SqlServer,Oracle,MySql,PostgreSql数据库存储过程调用代码生成(C#/VB.NET)使用示例
7/27 更新至v1.0.0.5 beta - 支持Sub Query查询

Sub Query示例:


1            //SELECT * FROM [Products] WHERE ([Products].[CategoryID] IN ((SELECT TOP 10 [Categories].[CategoryID] FROM [Categories] ORDER BY [Categories].[CategoryID])))   
2            DataSet ds = db.Select(Northwind.Products)
3                .Where(Northwind.Products.CategoryID.In
4                (
5                    db.Select(Northwind.Categories, Northwind.Categories.CategoryID).SetSelectRange(10, 0, Northwind.Categories.CategoryID).ToSubQuery())
6                )
7                .ToDataSet();
8
9            //SELECT * FROM [Products] WHERE ([Products].[CategoryID] = (SELECT TOP 1 [Categories].[CategoryID] FROM [Categories] ORDER BY [Categories].[CategoryID]))
10            ds = db.Select(Northwind.Products)
11                            .Where(Northwind.Products.CategoryID ==
12                            (
13                                db.Select(Northwind.Categories, Northwind.Categories.CategoryID).SetSelectRange(1, 0, Northwind.Categories.CategoryID).ToSubQuery())
14                            )
15                            .ToDataSet();
16            Assert.IsTrue(ds.Tables[0].Rows.Count > 0);

7/28 更新至v1.0.0.6 beta - 支持Save DataTable/DataRow

Save DataTable/DataRow示例:


1SelectSqlSection selectSection = db.Select(Northwind.Categories)
2  .Where(Northwind.Categories.CategoryID == catID)
3  .OrderBy(Northwind.Categories.CategoryID.Desc)
4  .SetSelectRange(1, 0, Northwind.Categories.CategoryID);
5DataTable dt = selectSection.ToDataSet().Tables[0];
6
7dt.Rows[0]["CategoryName"] = "modified";
8
9//save a modified row
10db.Save(selectSection.ToDbCommand(), 10, dt);
11
12dt.Rows[0]["CategoryName"] = "modified2";
13
14DataRow newRow = dt.NewRow();
15newRow["CategoryName"] = "new";
16dt.Rows.Add(newRow);
17
18//save 1 modified row and insert a new row
19db.Save(selectSection.ToDbCommand(), tran, 0, dt.Rows[0], dt.Rows[1]);
7/29 更新至v1.0.0.7 beta - 发布中文版NBearLite完全参考手册
7/30 更新至v1.0.0.8 beta - 为Stored Procedure包装方法增加带Transaction重载
8/2 更新至v1.0.0.9 beta - 修复NBearLite参考手册某些操作系统打开错误的问题

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-303607-1-1.html 上篇帖子: teamlab与redmine试用对比报告 下篇帖子: mojoPortal介绍
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表