• mongo数据库连接工具类(C#)


    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;
            }
        }
    }
  • 相关阅读:
    企业资源管理概述
    有效的使用WSE(学习+实践)
    和优秀的员工一起工作,是一种幸福
    [恢]hdu 1312
    [恢]hdu 1010
    [恢]hdu 1302
    [恢]hdu 1056
    [恢]hdu 1030
    [恢]hdu 1730
    [恢]hdu 1032
  • 原文地址:https://www.cnblogs.com/threadj/p/10536273.html
Copyright © 2020-2023  润新知