.NET C# 下調用MongoDB連結的方法


  • 註冊用戶

    本文章討論如何在C#環境中 調用MongoDB的資料庫

    範例代碼

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    //ref 1
    using MongoDB.Bson;
    using MongoDB.Driver;
    
    namespace WebSocketServer
    {
        class MongoCommand
        { 
             //ref2
            const string strConn = "mongodb://127.0.0.1:27017";
            MongoServer server = MongoDB.Driver.MongoServer.Create(strConn);
            public class LogData
            {
                public ObjectId _id;
                public string TransactionID{ get; set; }
                public string Content{ get; set; }
                public string SendTime{ get; set; }
                public string Role{ get; set; }
                public string SendName { get; set; }
                public string MerchantSysno { get; set; }//对应的商家编号
                public string MessageType { get; set; }//消息类型:内容,登出
            }
            public void MongoSaveChatRecord(string sendName,string role,string transactionID,string merchantSysno,string content,string sendTime)
            {
                try
                {
                    LogData ld = new LogData();
                    ld.TransactionID = transactionID;
                    ld.SendTime = DateTime.Now.ToString("yyyy-MM-dd").ToString()+sendTime;
                    ld.Content = content;
                    ld.Role = role;
                    ld.SendName = sendName;
                    ld.MessageType = "ChatContent";
                    ld.MerchantSysno = merchantSysno;
                    MongoDatabase db = server.GetDatabase(DateTime.Now.ToString("yyyy-MM"));
                    MongoCollection clo = db.GetCollection("ChatRecordBy"+merchantSysno);//每个商家一个集合
                    clo.Insert<LogData>(ld);
                }
                catch
                {
    
                }
            }
    
            public void MongoSaveNotification(string content, string transactionID, string merchantSysno)
            {
                try
                {
                    LogData ld = new LogData();
                    ld.TransactionID = transactionID;
                    ld.SendTime = DateTime.Now.ToString();
                    ld.Content = content;
                    ld.Role = "系统";
                    ld.SendName = "系统";
                    ld.MessageType = "Notification";
                    ld.MerchantSysno = merchantSysno;
                    MongoDatabase db = server.GetDatabase(DateTime.Now.ToString("yyyy-MM"));
                    MongoCollection clo = db.GetCollection("ChatRecordBy" + merchantSysno);//每个商家一个集合
                    clo.Insert<LogData>(ld);
                }
                catch
                {
    
                }
    
            }
            public string MongoQuery(string transactionID, string merchantSysno)
            {
                string ChatRecord = "ChatRecord:";
                try
                {
                    //ref3
                    MongoDatabase db = server.GetDatabase(DateTime.Now.ToString("yyyy-MM"));
                    MongoCollection clo = db.GetCollection("ChatRecordBy" + merchantSysno);
                    var query = new QueryDocument { { "TransactionID", transactionID } };
                    MongoCursor<BsonDocument> ld = clo.FindAs<BsonDocument>(query);
                    foreach (BsonDocument a in ld)
                    {
                        if (a["Role"] == "Agent")
                            ChatRecord = ChatRecord + "<li class='contentRight'><div class='ReviceTime'>" + a["SendTime"] + "</div><div>"+a["Content"]+"</div>";
                        if(a["Role"] == "Customer")
                            ChatRecord = ChatRecord + "<li class='contentLeft'><div class='ReviceTime'>" + a["SendTime"] + "</div><div>" + a["Content"] + "</div>";
                        if(a["Role"] == "系统")
                            ChatRecord = ChatRecord + "<li class='contentSys'><div>" + a["Content"] + "</div>";
                    }
                    return ChatRecord;
                }
                catch
                {
                    return ChatRecord;
                }
            }
    
            public string getTransaction(string agent, string customer)//生成一个不重复的随机数,暂时用这个
            {
                LogData ld = new LogData();
                ld.TransactionID=ld._id.ToString();
                ld.Content = "Agent:" + agent + "Join Customer:" + customer;
                MongoDatabase db = server.GetDatabase(DateTime.Now.ToString("yyyy-MM"));
                MongoCollection clo = db.GetCollection("Login");
                clo.Insert<LogData>(ld);
                return ld._id.ToString();
            }
        }
    }
    
    

    使用說明

    1.使用類庫
    using MongoDB.Bson;
    using MongoDB.Driver;

    2.建立連結方式
    const string strConn = "mongodb://127.0.0.1:27017";
    MongoServer server = MongoDB.Driver.MongoServer.Create(strConn);

    3.調用類庫方法進行操作

    MongoDatabase db = server.GetDatabase(DateTime.Now.ToString("yyyy-MM"));
    MongoCollection clo = db.GetCollection("ChatRecordBy" + merchantSysno);
    實現clo類後,就可對MongoDB資料庫進行操相關的操作
    clo.Insert(ld);

    出處來源:
    http://m.blog.csdn.net/article/details?id=7440916

    Provide by Fred / Hongfei


  • 註冊用戶

    非常老的帖子也要回复一下,不错,分享是优良的美德 ,不过目前还没有学MongoDB,迟早的事,


  • 註冊用戶

    收藏~感謝分享~


  • Lv 1

    這篇應該是舊了... 新版應該是用 MongoClient 建立連線


登录后回复
 

与 萌阔论坛 的连接断开,我们正在尝试重连,请耐心等待