设为首页 加入收藏

TOP

csharp: MongoDB(一)
2015-11-21 01:58:25 来源: 作者: 【 】 浏览:0
Tags:csharp: MongoDB
安装配置:
?
Install MongoDB on Windows(安装配置官方参考) http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/
?
1.Run MongoDB
?
C:\Program Files\MongoDB\Server\3.0\bin\mongod.exe --dbpath d:\data\db
?
#配置 数据库
mongod.exe --dbpath d:\data\db
#配置日志文件
mongod.exe --logpath D:\data\logs\mongodb.log --install
?
#测试用户登录
?
mongo -u geovindu -p
?
2.C# 连接字符串
?
?
以上四项都可以
?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using MongoDB.Bson;
using MongoDB.Driver;

namespace MongoDB2.Model
{
    /// 
    /// Wrapper class to communicate with 'MyCompany' database.
    /// 
    class MyCompany
    {

        /// 
        ///
        /// 
        public MyCompany()
        {

        }

        /// 
        /// Connection string to the Mongo database server
        /// 
        public static string ConnectionString
        {
            get
            {
                return ConfigurationManager.AppSettings["connectionString"];
            }
        }

        /// 
        /// Creates sample data for two collections(or tables) i.e, Departments, Employees.
        /// 
        public static void CreateData()
        {
            CreateDepartments();
            CreateEmployees();
        }
         
        #region Departments

        /// 
        /// Retrieve departments from MyCompany database.
        /// 
        /// 
        public static List GetDepartments()
        {
            List lst = new List();

            MongoServer server = MongoServer.Create(ConnectionString);
            MongoCredentials credentials = new MongoCredentials("geovindu", "geovindu");
            MongoDatabase myCompany = server.GetDatabase("geovinDB");//,credentials//MyCompany
            MongoCollection departments = myCompany.GetCollection("Departments");
            foreach (Department department in departments.FindAll())
            {
                lst.Add(department);
            }

            return lst;
        }

        /// 
        /// Inserts sample departments data in MyCompany database
        /// 
        private static void CreateDepartments()
        {
            string headOfDepartmentId;

            //insert department 'Development'
            headOfDepartmentId = "4f180083ef31ba0da8000010";
            CreateDepartment("Development", headOfDepartmentId);

            //insert department 'Accounts'
            headOfDepartmentId = "4f180083ef31ba0da8000011";
            CreateDepartment("Accounts", headOfDepartmentId);

            //insert department 'Human Resource'
            headOfDepartmentId = "4f180083ef31ba0da8000012";
            CreateDepartment("Human Resource", headOfDepartmentId);
        }

        /// 
        /// Insert the department
        /// 
        /// 
        /// 
        private static void CreateDepartment(string departmentName, string headOfDepartmentId)
        {
            MongoServer server = MongoServer.Create(ConnectionString);
            MongoCredentials credentials = new MongoCredentials("geovindu", "geovindu");
            MongoDatabase myCompany = server.GetDatabase("geovinDB");//, credentials //MyCompany

            MongoCollection departments = myCompany.GetCollection("Departments");
            BsonDocument deptartment = new BsonDocument {
                        { "DepartmentName", departmentName },
                        { "HeadOfDepartmentId", headOfDepartmentId }
                        };

            departments.Insert(deptartment);
        }

        /// 
        /// Delete all data in departments collection in MyCompany database
        /// 
        public static void DeleteDepartments()
        {
            MongoServer server = MongoServer.Create(ConnectionString);

            MongoCredentials credentials = new MongoCredentials("geovindu", "geovindu");
            MongoDatabase myCompany = server.GetDatabase("geovin
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇机房收费系统之组合查询――窗体.. 下一篇PostgreSQL-安装9.2

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: