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();
// }
// }
//}
}

  

MongoHelper.cs的更多相关文章

  1. neo-thinsdk-cs 之 thinWallet 接入私链

    neo-thinsdk-cs 之 thinWallet 接入私链 2017年底刚开始接触区块链,目前在被 NEO 折磨. 一开始被官方文档和 NEO-GUI 搞得体无完肤(尤其是传说中的 F12),也 ...

  2. [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute

    剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...

  3. Atitit 软件架构方法的进化与演进cs bs soa roa  msa  attilax总结

    Atitit 软件架构方法的进化与演进cs bs soa roa  msa  attilax总结 1.1. 软件体系架构是沿着单机到 CS 架构,再到 BS 的三层架构甚至多层架构逐步发展过来的,关于 ...

  4. 从java文件和CS文件里查询方法使用次数工具

    前几天,领导让我找一下老系统(Java)里getRemoteUser方法都哪个文件用了,package是什么,方法被调用了多少次,当时因为着急,所以,直接人工找的,但是以后要是再出现,人工找就太讨厌了 ...

  5. 关于 WP 开发中.xaml 与.xaml.cs 的关系

    今天我们先来看一下在WP8.1开发中最长见到的几个文件之间的关系.比较论证,在看这个问题之前我们简单看看.NET平台其他两个不同的框架: Windows Forms 先看看Window Forms中的 ...

  6. .net 用户控件ascx.cs注册js脚本代码无效果

    在.net web项目中碰到一个比较奇怪的问题,网上没找到解决方案,先自己mark一下 问题描述: 添加一个用户控件ascx,在后端.cs添加js注册脚本,执行后没有弹出框 注册脚本为: this.P ...

  7. DateHelper.cs日期时间操作辅助类C#

    //==================================================================== //** Copyright © classbao.com ...

  8. 仅用aspx文件实现Ajax调用后台cs程序。(实例)

    仅用aspx文件实现Ajax调用后台cs无刷新程序.(实例) 两个文件:aaa.aspx 和aaa.aspx.cs 一.aaa.aspx <script type="text/java ...

  9. Etw EventSourceProvider_EventsProducer.cs OopConsoleTraceEventListenerMonitor_TraceControllerEventsConsumer.cs

    // EventSourceProvider_EventsProducer.cs /* /r:"D:\Microshaoft.Nuget.Packages\Microsoft.Diagnos ...

随机推荐

  1. tensorflow(二)----线程队列与io操作

    一.队列和线程 1.队列: 1).tf.FIFOQueue(capacity, dtypes, name='fifo_queue') 创建一个以先进先出的顺序对元素进行排队的队列 参数: capaci ...

  2. 定时任务之elastic-job概述

    定时任务有哪些? Timer定时器 ScheduledExecutorService Spring自带的@Scheduled Quartz定时任务 当当elastic job定时任务 Quartz实现 ...

  3. 关于js中的取值问题

    像这样是获取不到值的,弹出的消息是 underfined:<html><style type="text/css">input { border: 1px ...

  4. Druid学习之路 (五)Druid的数据摄取任务类型

    作者:Syn良子 出处:https://www.cnblogs.com/cssdongl/p/9885534.html 转载请注明出处 Druid的数据摄取任务类型 Druid支持很多种类型的数据摄取 ...

  5. Webpack+React+ES6 最新环境搭建和配置(2017年)

    刚刚学习React,发现React在ES6下的语法才是本体,结合ES6新的语言特性,使组件化开发显得更加直观.而且现在的Angular2也开始使用支持强类型的TypeScript,转译(transpi ...

  6. Netflix Hystrix — 应对复杂分布式系统中的延时和故障容错 转

    转自 https://segmentfault.com/a/1190000005988895 前言 分布式系统中经常会出现某个基础服务不可用造成整个系统不可用的情况, 这种现象被称为服务雪崩效应. 为 ...

  7. Ubuntu16.04安装印象笔记

    Nixnote 是一个 Evernote 开源客户端,原名 Nevernote.Evernote 是一个著名的笔记等个人资料整理和同步软件, 因为 Evernote 没有 Linux 下的官方版本,因 ...

  8. zookeeper和淘宝dubbo的关系

    Dubbo建议使用Zookeeper作为服务的注册中心. 1.   Zookeeper的作用:         zookeeper用来注册服务和进行负载均衡,哪一个服务由哪一个机器来提供必需让调用者知 ...

  9. addEventListener、onclick和jquery的bind()、click()

    addEventListener("click",function(event){},false); removeEventListener("click",f ...

  10. 20145302张薇《Java程序设计》第六周学习总结

    20145302 <Java程序设计>第六周学习总结 教材学习内容总结 第十章 串流设计的概念 无论来源和目的地实体形式是什么,只要取得InputStream和OutputStream实例 ...