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#与java TCP通道加密通信实例 2023-05-05
- vs dotnet core 附加进程调试时有多个dotnet进程,没有title 2023-09-26
- C# 创建EXCEL图表并保存为图片的实例 2022-12-01
- c# – Img标签没有在windows azure云服务中显示图像 2023-09-18
- C# winform自定义翻页控件详解 2022-11-14
- C#实现简单的五子棋游戏 2023-03-28
- C# 基础入门--常量 2022-11-04
- Unity OnGUI实时显示游戏FPS 2023-02-02
- C#中IntPtr类型的具体使用 2023-05-21
- C#连接MySQL数据库 2023-11-11
