實體類:using MongoDB.Bson;namespace WindowsFormsApp{ class User { //public ObjectId _id; //BsonType.ObjectId 這個對應了 MongoDB.Bson.ObjectId ...
實體類:
using MongoDB.Bson; namespace WindowsFormsApp { class User { //public ObjectId _id; //BsonType.ObjectId 這個對應了 MongoDB.Bson.ObjectId public int UserId { get; set; } public string UserName { get; set; } public string Password { get; set; } } }
測試代碼:
using System; using System.Windows.Forms; using MongoDB.Driver; namespace WindowsFormsApp { public partial class frmMongoDBTest : Form { //資料庫連接字元串 string conn = "mongodb://127.0.0.1:27017"; //資料庫名稱 string database = "RsdfDb"; public frmMongoDBTest() { InitializeComponent(); } /// <summary> /// 插入數據 /// </summary> public void InsertData() { //創建資料庫鏈接 var Server = new MongoClient(conn).GetServer(); //獲得資料庫RsdfDb var db = Server.GetDatabase(database); User user = new User(); user.UserId = 0; user.UserName = "admin"; user.Password = "1"; //獲得Act_User集合,如果資料庫中沒有,先新建一個 var col = db.GetCollection("Act_User"); //執行插入操作 col.Insert<User>(user); user = new User(); user.UserId = 1; user.UserName = "chenqp"; user.Password = "1"; //獲得Act_User集合,如果資料庫中沒有,先新建一個 col = db.GetCollection("Act_User"); //執行插入操作 col.Insert<User>(user); user = new User(); user.UserId = 2; user.UserName = "yangh"; user.Password = "1"; //獲得Act_User集合,如果資料庫中沒有,先新建一個 col = db.GetCollection("Act_User"); //執行插入操作 col.Insert<User>(user); } /// <summary> /// 更新數據 /// </summary> public void UpdateData() { //創建資料庫鏈接 var Server = new MongoClient(conn).GetServer(); //獲得資料庫RsdfDb var db = Server.GetDatabase(database); //獲取Act_User集合 var col = db.GetCollection("Act_User"); //定義獲取“UserName”值為“admin”的查詢條件 var query = new QueryDocument { { "UserName", "admin" } }; //定義更新文檔 var update = new UpdateDocument { { "$set", new QueryDocument { { "Password", "admin" } } } }; //執行更新操作 col.Update(query, update); } /// <summary> /// 刪除數據 /// </summary> public void DeleteData() { //創建資料庫鏈接 var Server = new MongoClient(conn).GetServer(); //獲得資料庫RsdfDb var db = Server.GetDatabase(database); //獲取Act_User集合 var col = db.GetCollection("Act_User"); //定義獲取“UserName”值為“admin”的查詢條件 var query = new QueryDocument { { "Password", "admin" } }; //執行刪除操作 col.Remove(query); } /// <summary> /// 查詢數據 /// </summary> public void QueryData() { //創建資料庫鏈接 var Server = new MongoClient(conn).GetServer(); //獲得資料庫RsdfDb var db = Server.GetDatabase(database); //獲取Act_User集合 var col = db.GetCollection("Act_User"); //定義獲取“UserName”值為“admin”的查詢條件 var query = new QueryDocument { { "UserName", "chenqp" } }; //查詢全部集合里的數據 var result1 = col.FindAllAs<User>(); //查詢指定查詢條件的第一條數據,查詢條件可預設。 var result2 = col.FindOneAs<User>(); //查詢指定查詢條件的全部數據 var result3 = col.FindAs<User>(query); } private void btnInsert_Click(object sender, EventArgs e) { InsertData(); lblResult.Text = "Insert Data completed"; } private void btnUpdate_Click(object sender, EventArgs e) { UpdateData(); lblResult.Text = "Update Data completed"; } private void btnDelete_Click(object sender, EventArgs e) { DeleteData(); lblResult.Text = "Delete Data completed"; } private void btnQuery_Click(object sender, EventArgs e) { QueryData(); } } }