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# Random类随机函数实例详解 2023-07-04
- c# 如何实现一个简单的json解析器 2023-03-09
- Unity实现简单场景分层移动 2023-04-28
- c# – 如何在Windows Phone 8.1上使用Zxing创建QR码图像 2023-09-18
- 浅谈Visual Studio 2019 Vue项目的目录结构 2023-02-09
- C#中File静态类对文件的读取写入 2023-06-05
- Unity实现批量Build打包详解 2023-05-11
- C# datagrid非常规方法实现添加合并列 2023-01-06
- .NET Core开发之配置详解 2022-12-30
- C#中C/S端实现WebService服务 2023-06-22
