zoukankan      html  css  js  c++  java
  • MongoHelper.cs

    using System;
    using MongoDB.Bson;
    using MongoDB;
    using System.Web;
    using MongoDB.Driver;
    
    namespace YSF.ImageUtility
    {
    //
    //http://blog.csdn.net/dannywj1371/article/details/7440916
    //https://github.com/mongodb/mongo-csharp-driver
    //http://docs.mongodb.org/ecosystem/drivers/csharp-community-projects/
    public class MongoDBHelper
    {
    private const string CurrentSessionKey = "mongodb.current_session";
    
    private static string dbName = "test";
    static string connectionString = "mongodb://localhost";
    static MongoClient client = null;
    static MongoServer server = null;
    //static MongoDatabase database = null;// WriteConcern defaulted to Acknowledged
    
    static MongoDBHelper()
    {
    client = new MongoClient(connectionString);
    }
    
    /// <summary>
    /// 打开一个数据库连接
    /// </summary>
    public static MongoDatabase Open()
    {
    if (HttpContext.Current != null)
    {
    server = HttpContext.Current.Session[CurrentSessionKey] as MongoServer;
    
    if (server == null)
    {
    server = client.GetServer();
    HttpContext.Current.Session[CurrentSessionKey] = server;
    }
    }
    else
    {
    server = client.GetServer();
    }
    server.Connect();
    
    return server.GetDatabase(dbName);
    }
    
    /// <summary>
    /// 不要忘记关闭连接
    /// </summary>
    public static void Close()
    {
    if (HttpContext.Current == null)
    {
    return;
    }
    
    if (server == null)
    {
    server = HttpContext.Current.Session[CurrentSessionKey] as MongoServer;
    }
    
    if (server != null)
    {
    server.Disconnect();
    }
    }
    }
    
    //public class MongoDBHelper
    //{
    // private const string CurrentSessionKey = "mongodb.current_session";
    
    // //string strCon = @"mongodb://admin:xPlBBU7H1cDr@127.6.127.130:27017/";
    // private static string strConnection = @"mongodb://localhost/";
    // private static string dbName = "bolo";
    // private static MongoConfigurationBuilder config = new MongoConfigurationBuilder();
    
    // static MongoDBHelper()
    // {
    // // COMMENT OUT FROM HERE
    // config.Mapping(mapping =>
    // {
    // mapping.Map<Account>();
    // mapping.Map<Live>();
    
    // });
    // config.ConnectionString(strConnection);
    // }
    
    // /// <summary>
    // /// 打开一个数据库连接
    // /// </summary>
    // public static IMongoDatabase Open()
    // {
    // Mongo mongo = null;
    // if (HttpContext.Current != null)
    // {
    // mongo = HttpContext.Current.Session[CurrentSessionKey] as Mongo;
    
    // if (mongo == null)
    // {
    // mongo = new Mongo(config.BuildConfiguration());
    // HttpContext.Current.Session[CurrentSessionKey] = mongo;
    // }
    // mongo.Connect();
    // }
    // else
    // {
    // mongo = new Mongo(config.BuildConfiguration());
    // mongo.Connect();
    // }
    
    // return mongo.GetDatabase(dbName);
    // }
    
    // /// <summary>
    // /// 不要忘记关闭连接
    // /// </summary>
    // public static void Close()
    // {
    // if (HttpContext.Current == null)
    // {
    // return;
    // }
    
    // var mongo = HttpContext.Current.Session[CurrentSessionKey] as Mongo;
    // if (mongo != null)
    // {
    // mongo.Disconnect();
    // }
    // }
    //}
    }
    
     
    

      

  • 相关阅读:
    input[type="checkbox"]与label对齐
    JS 中 JSON 对象与字符串之间的相互转换
    ashx 一般处理程序中使用 Session
    “性能监视器”监视系统性能的基本设置
    Android 之 获取地理位置及监听
    Android 之 Fagment 完全解析
    PLT redirection through shared object injection into a running process
    Linux动态链接之GOT与PLT
    站点参考设计
    bash shell for循环1到100
  • 原文地址:https://www.cnblogs.com/huxiaolin/p/4488615.html
Copyright © 2011-2022 走看看