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. PHP双向队列,双端队列代码

    <?php /**  * User: jifei  * Date: 2013-07-30  * Time: 23:12 */ /**  * PHP实现双向队列,双端队列  * 双端队列(dequ ...

  2. XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem K. Piecemaking

    题目:Problem K. PiecemakingInput file: standard inputOutput file: standard outputTime limit: 1 secondM ...

  3. ReentrantLock的底层实现机制 AQS

    ReentrantLock的底层实现机制是AQS(Abstract Queued Synchronizer 抽象队列同步器).AQS没有锁之类的概念,它有个state变量,是个int类型,为了好理解, ...

  4. RedisTemplate访问Redis数据结构

    https://www.jianshu.com/p/7bf5dc61ca06 Redis 数据结构简介 Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字 ...

  5. JAVA学习笔记之图解JAVA参数传递

    今天做项目,发现了一个问题,当String作为参数传递的时候,在函数内部改变值对外部的变量值无影响,如下代码: public static void main(String[] args) { Str ...

  6. Python面试题目之(针对dict或者set数据类型)边遍历 边修改 报错dictionary changed size during iteration

    # result 是一个字典, 把里面属性值是None的属性删除 for key in result: if not result[key]: del result[key] continue 但是报 ...

  7. Java:延迟功能的Robot在Lunix系统上会报错

    Java:延迟功能的Robot在Lunix系统上会报错 关于延迟功能的Robot: 今天开发过程中发现,本机开发好的项目,部署到Lunix服务器竟然报错!查了代码发现: Robot r = new R ...

  8. C++开学第一次作业(5.4)

    开学第一次作业(5.4) 代码传送门 题目 Create a program that asks for the radius of a circle and prints the area of t ...

  9. 通过map文件了解堆栈分配(STM32、MDK5)--避免堆栈溢出

    环境:STM32F103C8T6,MDK5 在最近的一个项目的开发中,每当调用到一个函数,程序就直接跑飞.debug跟进去看不出什么逻辑错误,但发现函数内局部变量声明之后,全局变量的值被清零,后来查看 ...

  10. 解读:hadoop压缩格式

    Hadoop中用得比较多的4种压缩格式:lzo,gzip,snappy,bzip2.它们的优缺点和应用场景如下: 1). gzip压缩 优点:压缩率比较高,而且压缩/解压速度也比较快:hadoop本身 ...