设为首页 加入收藏

TOP

csharp: MongoDB(二)
2015-11-21 01:58:25 来源: 作者: 【 】 浏览:1
Tags:csharp: MongoDB
DB");//, credentials//MyCompany MongoCollection departments = myCompany.GetCollection("Departments"); departments.Drop(); } #endregion #region Employees /// /// Retrieve employees from MyCompany database. /// /// public static List GetEmployees() { List lst = new List(); MongoServer server = MongoServer.Create(ConnectionString); MongoCredentials credentials = new MongoCredentials("geovindu", "geovindu"); MongoDatabase myCompany = server.GetDatabase("geovinDB");//, credentials//无验证密码登录 MongoCollection employees = myCompany.GetCollection("Employees"); foreach (Employee employee in employees.FindAll()) { lst.Add(employee); } return lst; } /// /// Inserts sample employees data in MyCompany database /// private static void CreateEmployees() { // add 5 sample Employees for (int i = 1; i <= 5; i++) { string departmentId = "4f180083ef31ba0da8000010"; CreateEmployee("FirstName" + i, "LastName" + i, "Address" + i, "City" + i, departmentId); } } /// /// Insert the employee /// /// /// private static void CreateEmployee(string firstName, string lastName, string address, string city, string departmentId) { MongoServer server = MongoServer.Create(ConnectionString); MongoCredentials credentials = new MongoCredentials("geovindu", "geovindu"); MongoDatabase myCompany = server.GetDatabase("geovinDB");//, credentials//MyCompany MongoCollection employees = myCompany.GetCollection("Employees"); BsonDocument employee = new BsonDocument { { "FirstName", firstName }, { "LastName", lastName }, { "Address", address }, { "City", city }, { "DepartmentId", departmentId } }; employees.Insert(employee); } /// /// Delete all data in employees collection in MyCompany database /// public static void DeleteEmployees() { MongoServer server = MongoServer.Create(ConnectionString); MongoCredentials credentials = new MongoCredentials("geovindu", "geovindu"); MongoDatabase myCompany = server.GetDatabase("geovinDB");//, credentials//MyCompany MongoCollection employees = myCompany.GetCollection("Employees"); employees.Drop(); } #endregion } #region Department /// /// Department represents a single item(record) stored in Departments collection. /// class Department { public ObjectId _id { get; set; } public string DepartmentName { get; set; } public ObjectId HeadOfDepartmentId { get; set; } } #endregion #region Employee /// /// Department represents a single item(record) stored in Employees collection. /// class Employee { public ObjectId _id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } public string City { get; set; } public ObjectId DepartmentId { get; set; } } #endregion }

?


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇机房收费系统之组合查询――窗体.. 下一篇PostgreSQL-安装9.2

评论

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