MongoDB结合Flexgrid的简单数据呈现 本示例以常用的:用户,帖子,评论为基础模型,实现了一个简单的MongoDB结合Flexgrid数据呈现的Demo。由于时间所限,功能上仅提供对MongoDB数据的常用查询操作(分页,排序,查询等)。高手请对我这种菜鸟多些包容。 一,
MongoDB结合Flexgrid的简单数据呈现
本示例以常用的:用户,帖子,评论为基础模型,实现了一个简单的MongoDB结合Flexgrid数据呈现的Demo。由于时间所限,功能上仅提供对MongoDB数据的常用查询操作(分页,排序,查询等)。高手请对我这种菜鸟多些包容。
一,准备工作:
MongoDB官方下载:
当前最新版本是2.2.0版本。话说,MongoDB的版本更新是相当的快。
本示例使用的MongoDB C#版驱动下载:
https://github.com/samus/mongodb-csharp
该驱动附源码和示例程序,有兴趣的朋友可以研究一下,对自己的编码能力会有很大的提高。用VS打开Source文件夹,编译后得到程序集,在自己的项目中引用即可。
这是官方邮件提供的 C#版驱动:
https://github.com/mongodb/mongo-csharp-driver/downloads
版本很多,而且基本上都是十多兆的东西,然后,没有然后。
如果你不习惯以命令行的方式对MongoDB进行操作和管理。推荐以下客户端管理工具:
1,MongoVUE。这应该是目前应用最广的了。
下载地址:
2,虚拟主机,博客园高手自己开发的MongoDB管理工具:
试用了一下,也是相当不错的!
虽然上述管理工具能够以图形化的方式与MongoDB交互,但强烈建议你运行mongo.exe客户端,以命令行的方式对MongoDB进行操作和管理,这会让你对MongoDB有更加直观而深刻的体会。另外,Windows8依然内置了DOS。
这里有MongoDB的常用命令:
简单AI
搜狐推出的AI图片生成社区
307 查看详情
这是Flexgrid的官方网站:
页面顶部有一个硕大的红色Download按钮。
TestDriven.net,必不可少的开发和测试工具。本示例需要用它向MongoDB中初始化测试数据。下载地址:
二,项目结构:
麻雀虽小,五脏俱全。整个项目结构是一个最原始的三层。直接上图:

三,技术细节:
1,MongoDB数据库操作。
ADO.NET虽然很强大,但我们通常需要自己动手写一个SqlHelper。MongoDB也一样,我们仍有必要在驱动的基础上对常用的数据操作进行封装。下面贴出本鸟写的MongoDB数据库操作类。大多数方法都与数据查询相关。贴出主要代码:
using System;using System.Collections.Generic;using System.Linq;using System.Configuration;using MongoDB;using MongoDB.Linq;using Mcmurphy.Commons.Enumerations;namespace Mcmurphy.DAL{public class MongoDBHelper{#region 基本信息private static readonly string ConnectionString;private static readonly string DatabaseName;///
private static Mongo mongo;///
static MongoDBHelper(){ConnectionString = ConfigurationManager.AppSettings[“connString”];DatabaseName = ConfigurationManager.AppSettings[“currentDB”];}///
private static Mongo CurrentMongo{get{return new Mongo(ConnectionString);}}///
private static IMongoCollection GetCollection() where T:class{try{mongo = CurrentMongo;mongo.Connect();IMongoDatabase db = GetCurrentDataBase();IMongoCollection collection = db.GetCollection();return collection;}catch (Exception ex){throw ex;}}///
/// 当前Mongo数据库对象private static IMongoDatabase GetCurrentDataBase(){return mongo.GetDatabase(DatabaseName);}#endregion#region 数据查询///
private static IndexOrder GetIndexOrder(DataOrder order){IndexOrder @orderby = order == DataOrder.Ascending ?IndexOrder.Ascending : IndexOrder.Descending;return orderby;}///
/// 类型参数/// 条件Lamda表达式/// 当前页索引/// 分页大小/// 结果集public static IEnumerable GetBySearch(System.Linq.Expressions.Expression> selector, int pageIndex, int pageSize) where T : class{try{var currentCollection = GetCollection();return currentCollection.Find(selector).Skip((pageIndex – 1) * pageSize).Limit(pageSize).Documents.ToList();}catch (Exception ex){throw ex;}finally{mongo.Disconnect();mongo.Dispose();}}///
/// 类型参数/// 条件Lamda表达式/// 当前页索引/// 分页大小/// 排序规则/// 排序字段/// 结果集public static IEnumerable GetBySearch(System.Linq.Expressions.Expression> selector, int pageIndex, int pageSize, DataOrder order, string orderField) where T : class{try{IndexOrder orderby = GetIndexOrder(order);var currentCollection = GetCollection();return currentCollection.Find(selector).Sort(orderField, orderby).Skip((pageIndex – 1) * pageSize).Limit(pageSize).Documents.ToList();}catch (Exception ex){throw ex;}finally{mongo.Disconnect();mongo.Dispose();}}///
/// 类型参数/// 查询条件Lamda表达式/// 查询对象public static T GetOneBySearch(System.Linq.Expressions.Expression> selector) where T : class{try{var currentCollection = GetCollection();return currentCollection.FindOne(selector);}catch (Exception ex){throw ex;}finally{mongo.Disconnect();mongo.Dispose();}}///
/// 类型参数/// 查询条件/// 记录数public static long GetTotalCount(System.Linq.Expressions.Expression> selector) where T : class{try{var currentCollection = GetCollection();return currentCollection.Count(selector);}catch (Exception ex){throw ex;}finally{mongo.Disconnect();mongo.Dispose();}}///
/// 类型参数/// 记录数public static long GetTotalCount() where T : class{try{var currentCollection = GetCollection();return currentCollection.Count();}catch (Exception ex){throw ex;}finally{mongo.Disconnect();mongo.Dispose();}}#endregion#region 数据插入///
/// 类型参数/// 要插入的数据对象public static void Insert(T t) where T : class{try{var currentCollection = GetCollection();currentCollection.Insert(t);}catch (Exception ex){throw ex;}finally{mongo.Disconnect();mongo.Dispose();}}#endregion#region 数据更新///
/// 类型参数/// 待更新的对象/// 更新的条件Lamda表达式public static void Update(T t, System.Linq.Expressions.Expression> selector) where T : class{try{var currentCollection = GetCollection();currentCollection.Update(t,selector);}catch (Exception ex){throw ex;}finally{mongo.Disconnect();mongo.Dispose();}}///
/// 类型参数/// 待更新的对象public static void Update(T t) where T : class{try{var currentCollection = GetCollection();//inserts of updates depends on whether id existscurrentCollection.Save(t);}catch (Exception ex){throw ex;}finally{mongo.Disconnect();mongo.Dispose();}}#endregion#region 数据删除///
/// 类型参数/// 查询的条件Lamda表达式public static void Delete(System.Linq.Expressions.Expression> selector) where T : class{try{var currentCollection = GetCollection();currentCollection.Remove(selector);}catch (Exception ex){throw ex;}finally{mongo.Disconnect();mongo.Dispose();}}#endregion}}
初次调用的时候,会读取在站点项目下Web.Config文件中配置的数据库服务器地址以及数据库名称。
2,Flexgrid加载的数据格式。
在官网上徘徊了很久也没有找到Flexgrid要求的数据格式说明。还好有Firebug,但遗憾的是官方示例采用的是XML格式。如下:
1 234 … …
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/471632.html
微信扫一扫
支付宝扫一扫