QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

查看: 2044|回复: 0

[C#] MongoDB C# Driver 快速入门

[复制链接]

等级头衔

积分成就    金币 : 2810
   泡泡 : 1516
   精华 : 6
   在线时间 : 1245 小时
   最后登录 : 2024-5-23

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老

联系方式
发表于 2021-7-30 08:22:55 来自手机 | 显示全部楼层 |阅读模式
      MongoDB的官方C#驱动可以通过这个链接得到。链接提供了.msi和.zip两种方式获取驱动dll文件。C#驱动的基本数据库连接,增删改查操作。在使用C#驱动的时候,要在工程中添加"MongoDB.Bson.dll"和"MongoDB.Driver.dll"的引用。同时要在代码中加入下面两个using语句。
8 M8 t% Y4 @# \' J) p
  1. using MongoDB.Bson.Serialization.Conventions;
  2. using MongoDB.Driver;
  3. using MongoDB.Driver.Linq;
  4. using System.Text;

! l5 O. _. h+ W注册常用约定' Z3 n) v/ q' T9 T8 L! I
  1.   public static void RigisterConventions()
  2.         {
  3.             var pack = new ConventionPack();
  4.             //元素名称序列化成驼峰形式
  5.             pack.Add(new CamelCaseElementNameConvention());
  6.             ConventionRegistry.Register("MyConventions", pack, x => true);
  7.         }
9 L; a0 j: v, C, @
初始化Mongo帮助类
- z6 L7 c6 N" D
  1.   /// <summary>
  2.         /// 初始化Mongo帮助类
  3.         /// </summary>
  4.         /// <param name="connectionString">连接字符串</param>
  5.         /// <param name="dbName">数据库名</param>
  6.         public MongoHelper(string connectionString, string dbName)
  7.         {
  8.             _client = new MongoClient(connectionString);
  9.             _database = _client.GetDatabase(dbName);
  10.         }

5 T, }. D: r  l) \0 S# @  d获取集合
- U( r. V* S; Y5 v
  1.    /// <summary>
  2.         /// 获取集合
  3.         /// </summary>
  4.         /// <typeparam name="T">集合类型</typeparam>
  5.         /// <param name="collectionName">集合名</param>
  6.         /// <returns></returns>
  7.         public IMongoCollection<T> GetCollection<T>(string collectionName)
  8.         {
  9.             return _database.GetCollection<T>(collectionName);
  10.         }
( Y/ M0 t4 \: v% w9 H) O
完整代码
/ P2 v6 T( [" {
  1. /// <summary>
  2.     /// MongoDB访问帮助类
  3.     /// </summary>
  4.     public class MongoHelper
  5.     {
  6.         private MongoClient _client;
  7.         private IMongoDatabase _database;
  8.         /// <summary>
  9.         ///  注册常用约定
  10.         /// </summary>
  11.         public static void RigisterConventions()
  12.         {
  13.             var pack = new ConventionPack();
  14.             //元素名称序列化成驼峰形式
  15.             pack.Add(new CamelCaseElementNameConvention());
  16.             ConventionRegistry.Register("MyConventions", pack, x => true);
  17.         }
  18.         /// <summary>
  19.         /// 初始化Mongo帮助类
  20.         /// </summary>
  21.         /// <param name="connectionString">连接字符串</param>
  22.         /// <param name="dbName">数据库名</param>
  23.         public MongoHelper(string connectionString, string dbName)
  24.         {
  25.             _client = new MongoClient(connectionString);
  26.             _database = _client.GetDatabase(dbName);
  27.         }
  28.         /// <summary>
  29.         /// 数据库
  30.         /// </summary>
  31.         public IMongoDatabase Db => _database;
  32.         /// <summary>
  33.         /// 获取集合
  34.         /// </summary>
  35.         /// <typeparam name="T">集合类型</typeparam>
  36.         /// <param name="collectionName">集合名</param>
  37.         /// <returns></returns>
  38.         public IMongoCollection<T> GetCollection<T>(string collectionName)
  39.         {
  40.             return _database.GetCollection<T>(collectionName);
  41.         }
  42.         /// <summary>
  43.         /// 获取Queryable对象
  44.         /// </summary>
  45.         /// <typeparam name="T">对象类型</typeparam>
  46.         /// <param name="collectionName">集合名</param>
  47.         /// <returns></returns>
  48.         public IMongoQueryable<T> GetQueryable<T>(string collectionName)
  49.         {
  50.             return GetCollection<T>(collectionName).AsQueryable();
  51.         }
  52.         /// <summary>
  53.         /// 模糊查询转换特殊字符:正则表达式有以下特殊字符。需要转义  * . ? + $ ^ [ ] ( ) { } | \ /
  54.         /// 如:{"phone":/U9G\/XoDNo8ozbwbxal\+Qzg==/}
  55.         /// </summary>
  56.         /// <param name="str"></param>
  57.         /// <returns></returns>
  58.         public static string ChangeSpecialCharacter(string str)
  59.         {
  60.             if (string.IsNullOrEmpty(str))
  61.                 return str;
  62.             StringBuilder retValue = new StringBuilder();
  63.             string str1 = "*.?+$^[](){}|\\/";
  64.             for (int i = 0; i < str.Length; i++)
  65.             {
  66.                 string ss = str[i].ToString();
  67.                 if (str1.Contains(ss))
  68.                 {
  69.                     ss = "\" + ss;
  70.                 }
  71.                 retValue.Append(ss);
  72.             }
  73.             return retValue.ToString();
  74.         }

' s; T6 A. E2 J( o* [( C
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号 )

GMT+8, 2024-5-27 00:04

Powered by paopaomj X3.4 © 2016-2024 sitemap

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