Framework版本:.Net Framework 4using System;using System.Collections.Generic;using System.Linq;using System.Web;using MongoDB.Driver;namespace ReligionServer.util{public class ConnectionUtil{private s...
Framework版本:.Net Framework 4
using System; using System.Collections.Generic; using System.Linq; using System.Web; using MongoDB.Driver; namespace ReligionServer.util { public class ConnectionUtil { private static MongoClient client = null; private static MongoServer server = null; private static MongoDatabase database = null; private static MongoCollection collection = null; private static String DATA_SERVER_URL = "mongodb://ip地址:端口/数据库名";//数据库地址 //mongodb://192.168.1.1:27017/test private static String DATA_BASE = "test";//数据库名 //根据传入的集合名获取到当前集合 public static MongoCollection GetCollection<T>(String collectionName) { if (null == server) { server = getMongoServer(); } if (null == database) { database = server.GetDatabase(DATA_BASE); } collection = database.GetCollection<T>(collectionName); return collection; } private static MongoServer getMongoServer() { if (client == null) { client = new MongoClient(DATA_SERVER_URL); } if (server == null) { server = client.GetServer(); } server.Connect(); return server; } } }
沃梦达教程
本文标题为:mongo数据库连接工具类(C#)
基础教程推荐
猜你喜欢
- c# – Windows Phone 8通知和后台任务 2023-09-20
- C#中try...catch的使用与常见面试题分享 2022-11-02
- Unity通过脚本创建网格Mesh的方法 2023-05-30
- C# MemoryStream类案例详解 2023-04-27
- C#操作Byte数组和十六进制进行互转 2023-06-05
- UGUI实现ScrollView无限滚动效果 2023-01-16
- C#连接Oracle数据库字符串(引入DLL)的方式 2023-01-27
- C#滑动验证码拼图验证功能实现(SlideCaptcha) 2023-06-04
- C#中类的异常处理详解 2023-05-22
- WinForm IP地址输入框控件实现 2022-12-11