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. Safari中的input、textarea无法输入的问题

    原因是这两种表单元素上应用了user-select:none的css属性.一般没人刻意这么做,可能是这样的情况: * { user-select: none; } 在css中排除掉这两种元素就好了: ...

  2. H5 动画:轨迹移动 | H5游戏 推金币

    https://aotu.io/notes/2017/11/06/path-animation/ https://aotu.io/notes/2017/11/06/coindozer/

  3. 浅谈HTTP请求与响应

    HTTP协议用于客户端和服务器之间的通信,请求访问的一段是客户端,提供资源响应的一段是服务器端. HTTP通信是采用请求应答的方式来进行的,客户端发出请求,服务器响应.如果没有客户端的请求,服务器端是 ...

  4. springbcloud5----高可用

    package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframew ...

  5. linux下多线程之pthread_detach(pthread_self())

    写个碰到的问题,记录下自己的技术之路点滴pthread_detach(pthread_self())linux线程执行和windows不同,pthread有两种状态joinable状态和unjoina ...

  6. PHP中的错误信息

    PHP中的错误信息   php.ini中配置错误消息 在PHP4中,没有异常 Exception这个概念,只有 错误Error.我们可以通过修改php.ini 文件来配置用户端输出的错误信息. 在ph ...

  7. JS类、对象、方法、prototype、_proto_

    案例代码: function People(name) { //对象属性 this.name = name; //对象方法 this.Introduce = function() { alert(&q ...

  8. Cisco 交换Vlan配置

    添加Vlan命令 #添加vlan100 config)#vlan 100 #重命名vlan100 config-vlan)#name vlan100 #返回上一层 config-vlan)#exit ...

  9. 软件测试技术作业3---PrintPrimes()

    一.代码部分: private static void printPrimes (int n) { int curPrime; // Value currently considered for pr ...

  10. excel省市区三级分类级联

    前言:同事正好需要一个这样的地址类型给用户使用下载模板,改好地址再导入,这样就不会出现地址不匹配问题.所以就自己也整理了一套,以备不时之需. 效果展示: 图一:省级 图二:市级 图三:区级 图四:各乡 ...