国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

C# mongodb [下]

2019-11-17 02:25:37
字體:
供稿:網(wǎng)友

C# mongodb [下]

概述

  傳統(tǒng)的關系數(shù)據(jù)庫一般由數(shù)據(jù)庫(database)、表(table)、記錄(record)三個層次概念組成,MongoDB是由數(shù)據(jù)庫(database)、集合(collection)、文檔對象(document)三個層次組成。MongoDB對于關系型數(shù)據(jù)庫里的表,但是集合中沒有列、行和關系概念,這體現(xiàn)了模式自由的特點。

  在C#中想使用MongoDB我們還需要下載驅(qū)動,現(xiàn)在一般比較常見的驅(qū)動有官網(wǎng)驅(qū)動,samus驅(qū)動。samus驅(qū)動除了支持一般形式的操作之外,還支持linq方式操縱數(shù)據(jù)。本文所使用的為官網(wǎng)驅(qū)動。官網(wǎng)驅(qū)動地址:點擊下載

下載完后我們在下載包中可以得到二個DLL:

  MongoDB.Driver.dll  驅(qū)動程序

  MongoDB.Bson.dll   序列化、Json相關

實例代碼

新增:

//數(shù)據(jù)庫連接字符串const string connectionString = "mongodb://127.0.0.1:27017";//數(shù)據(jù)庫const string Database = "ck_test_db";MongoClient client = new MongoClient(connectionString);MongoServer server = client.GetServer();MongoDatabase db = server.GetDatabase(Database);MongoCollection collection = db.GetCollection("student");//新增student stud_Add = new student() { name="趙云", code="2001", sex="女", age="23" };collection.Insert<student>(stud_Add);//查詢QueryDocument query_sel = new QueryDocument { { "name", "趙云" } };var list = collection.FindAs<student>(query_sel);foreach (student item in list){    Console.WriteLine("name:{0},code:{1}", item.name, item.code);}

顯示結(jié)果:

修改:

//數(shù)據(jù)庫連接字符串const string connectionString = "mongodb://127.0.0.1:27017";//數(shù)據(jù)庫const string Database = "ck_test_db";MongoClient client = new MongoClient(connectionString);MongoServer server = client.GetServer();MongoDatabase db = server.GetDatabase(Database);MongoCollection collection = db.GetCollection("student");//更新QueryDocument query_upd = new QueryDocument { { "name", "趙云" } };IMongoUpdate update = Update.Set("code", "4444");  collection.Update(query_upd, update);

顯示結(jié)果:

刪除

//數(shù)據(jù)庫連接字符串const string connectionString = "mongodb://127.0.0.1:27017";//數(shù)據(jù)庫const string Database = "ck_test_db";MongoClient client = new MongoClient(connectionString);MongoServer server = client.GetServer();MongoDatabase db = server.GetDatabase(Database);MongoCollection collection = db.GetCollection("student");//刪除QueryDocument query_del = new QueryDocument { { "name", "趙云" } };collection.Remove(query_del);

全量代碼:

static void Main(string[] args){    //數(shù)據(jù)庫連接字符串    const string connectionString = "mongodb://127.0.0.1:27017";    //數(shù)據(jù)庫    const string Database = "ck_test_db";    MongoClient client = new MongoClient(connectionString);    MongoServer server = client.GetServer();    MongoDatabase db = server.GetDatabase(Database);    MongoCollection collection = db.GetCollection("student");    //新增    student stud_Add = new student() { name="趙云", code="2001", sex="女", age="23" };    collection.Insert<student>(stud_Add);    //更新    QueryDocument query_upd = new QueryDocument { { "name", "趙云" } };    IMongoUpdate update = Update.Set("code", "4444");      collection.Update(query_upd, update);    //刪除    QueryDocument query_del = new QueryDocument { { "name", "趙云" } };    collection.Remove(query_del);    //查詢    QueryDocument query_sel = new QueryDocument { { "name", "趙云" } };    var list = collection.FindAs<student>(query_sel);    foreach (student item in list)    {        Console.WriteLine("name:{0},code:{1}", item.name, item.code);    }}public class student{    public ObjectId _id; //BsonType.ObjectId 這個對應了 MongoDB.Bson.ObjectId     public string name { get; set; }    public string code { get; set; }    public string sex { get; set; }    public string age { get; set; }    /*_id 屬性必須要有,否則在更新數(shù)據(jù)時會報錯:“Element '_id' does not match any field or PRoperty of class”。*/}
View Code

基礎的增刪改查就是上面所述。

實例代碼:點擊下載


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 榆林市| 常熟市| 修水县| 宾川县| 威信县| 久治县| 阜南县| 黔西县| 黄大仙区| 自治县| 伊宁县| 惠安县| 衡水市| 吉林省| 浮梁县| 土默特左旗| 且末县| 彭山县| 阜新市| 襄樊市| 泸水县| 丰宁| 苏尼特左旗| 左贡县| 永寿县| 河北区| 延庆县| 珲春市| 滦南县| 仙游县| 白山市| 泰安市| 巴南区| 饶河县| 常山县| 湘阴县| 佛冈县| 博湖县| 商水县| 伊吾县| 石泉县|