zoukankan      html  css  js  c++  java
  • MongoDB学习笔记《三》

    这一次做一些基本的操作增删改查操作

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace MongoDbTestPrint
    {
        public class Student
        {
            private int _ID;
    
            public int ID
            {
                get { return _ID; }
                set { _ID = value; }
            }
    
            private string _Name;
    
            public string Name1
            {
                get { return _Name; }
                set { _Name = value; }
            }
    
            private string _Sex;
    
            public string Sex
            {
                get { return _Sex; }
                set { _Sex = value; }
            }
    
            private int _Age;
    
            public int Age1
            {
                get { return _Age; }
                set { _Age = value; }
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using MongoDB;
    
    namespace MongoDbTestPrint
    {
       public class StudentHandler
        {
           public string connectionName = "mongodb://localhost";
           public string collectionName = "mongoCollection";
           public string databaseName = "mongodbName";
    
           public Mongo mongoDB;
    
           public MongoCollection<Document> mongoCollection;
    
           public MongoDatabase mongoDataBase;
    
    
           public StudentHandler()
           {
               mongoDB = new Mongo(connectionName);
               mongoDataBase = mongoDB.GetDatabase(databaseName) as MongoDatabase;
               mongoCollection = mongoDataBase.GetCollection<Document>(collectionName) as MongoCollection<Document>;
               
           }
    
           public void InserStudent(Student student)
           {
               try
               {
                   mongoDB.Connect();
                   Document doc = new Document();
                   doc["ID"] = student.ID;
                   doc["Name"] = student.Name1;
                   doc["Sex"] = student.Sex;
                   doc["Age"] = student.Age1;
                   mongoCollection.Insert(doc);
               }
               finally
               {
                   mongoDB.Disconnect();
    
               }
           }
           public List<Document> SelectStudent(Student student)
           {
               List<Document> stu;
               try
               {
                   mongoDB.Connect();
                  stu= mongoCollection.Find(new Document { {"ID",student.ID}}).Documents.ToList();
               }
               finally
               {
                   mongoDB.Disconnect();
    
               }
               return stu;
           }
           public void UpdateStudent(Student student)
           {
               try
               {
                   mongoDB.Connect();
                    Document doc = new Document();
                   doc["ID"] = student.ID;
                   doc["Name"] = student.Name1;
                   doc["Sex"] = student.Sex;
                   doc["Age"] = student.Age1;
                   mongoCollection.Update(doc, new Document { { "ID", student.ID } });
               }
               finally
               {
                   mongoDB.Disconnect();
    
               }
           }
           public void DeleteStudent(Student student)
           {
               try
               {
                   mongoDB.Connect();
                   mongoCollection.Remove(new Document { { "ID", student.ID } });
               }
               finally
               {
                   mongoDB.Disconnect();
    
               }
           }
        }
    }
    static  void Main(string[] args)
            {
            try
                {
                    StudentHandler handler = new StudentHandler();
                    Student student = new Student();
                    student.ID = 1;
                    student.Name1 = "宝宝";
                    student.Sex = "11";
                    student.Sex = "";
    
                    handler.InserStudent(student);
                    List<Document> doc= handler.SelectStudent(student);
                    handler.UpdateStudent(student);
                    handler.DeleteStudent(student); 
                 }
                catch (Exception)
                {
                    throw;
                }    Console.ReadLine();
            }        
  • 相关阅读:
    对于ajax传递中文乱码问题,研究js encodeURI 与request.HtmlEncode的区别
    对于sa无法登陆,如何用windows身份验证来修改密码
    ASP.Net中自定义Http处理及应用之HttpHandler篇 1
    ReportViewer报表控件解析与使用(原)
    HttpUtility.UrlEncode,Server.UrlEncode 的区别
    xsl xml 以及 树的编写(原创)
    【转】存储过程的优缺点
    【转】关闭模态子窗口后刷新父窗口
    【转】ASP.NET 文件下载
    【转】去除HTML标签的方法
  • 原文地址:https://www.cnblogs.com/qishiguilai/p/2883730.html
Copyright © 2011-2022 走看看