久久综合丝袜日本网手机版,日韩欧美中文字幕在线三区,亚洲精品国产品国语在线,极品在线观看视频婷婷

      <small id="aebxz"><menu id="aebxz"></menu></small>
    1. NET連接MongoDB數(shù)據(jù)庫實例方法

      • 相關(guān)推薦

      NET連接MongoDB數(shù)據(jù)庫實例方法

        使用代碼

        讓我們從Mongo數(shù)據(jù)庫的一些細(xì)節(jié)和基本命令開始,并最終介紹如何創(chuàng)建一個可連接至Mongo數(shù)據(jù)庫的.NET Windows應(yīng)用。

        Mongo數(shù)據(jù)庫

        MongoDB 是一個跨平臺、文檔導(dǎo)向的數(shù)據(jù)庫系統(tǒng),它被歸類為“NoSQL”數(shù)據(jù)庫。MongoDB避開了傳統(tǒng)的基于表的關(guān)系數(shù)據(jù)庫結(jié)構(gòu),而是使用了帶動態(tài)模式的類JSON文檔。MongoDB將這種格式稱為BSON(二進(jìn)制JSON)。這種動態(tài)模式使得特定類型應(yīng)用中的數(shù)據(jù)整合更簡單、更快速。MongoDB是自由且開源的軟件。

        Mongo數(shù)據(jù)庫的特性

        Ad hoc 查詢

        1 標(biāo)引

        2 復(fù)制

        3負(fù)載均衡

        4 文件存貯

        5 聚合

        6 服務(wù)器端 JavaScript 執(zhí)行

        7 定容集合

        用戶可從 此處 下載Mongo數(shù)據(jù)庫,然后將其中內(nèi)容解壓至任一文件夾。 文件下載完成后,用戶需要配置MongoDB的數(shù)據(jù)文件夾。做法是在“C:Data”文件夾下創(chuàng)建一個名為“DB”的文件夾。

        數(shù)據(jù)文件夾創(chuàng)建好以后,可以通過用命令行提示符在“bin”文件夾下運行“mongod.exe”來啟動Mongo數(shù)據(jù)庫。

        現(xiàn)在數(shù)據(jù)庫已經(jīng)啟動,并正在運行。

        創(chuàng)建一個.NET應(yīng)用

        創(chuàng)建一個.NET web/Windows應(yīng)用。在這個示例中,我們將用一個簡單的員工表。

        開始之前,我們需要確保系統(tǒng)中裝有MongoDB的.NET驅(qū)動。你可以按下面的步驟來為一個指定的項目安裝驅(qū)動。

        打開Visual Studio的包管理器:

        打開包管理器控制臺后,用戶可以執(zhí)行下面的命令:

        復(fù)制代碼 代碼如下:

        Install-Package mongocsharpdriver

        在項目中添加對下列命名空間的引用:

        復(fù)制代碼 代碼如下:

        using MongoDB.Bson;

        using MongoDB.Driver;

        //此外,你將頻繁的用到下面這些 using 語句中的一條或多條:

        using MongoDB.Driver.Builders;

        using MongoDB.Driver.GridFS;

        using MongoDB.Driver.Linq;

        聲明數(shù)據(jù)庫服務(wù)器和數(shù)據(jù)庫的變量:

        復(fù)制代碼 代碼如下:

        MongoServer _server;

        MongoDatabase _database;

        用下面的命令連接至數(shù)據(jù)庫。在這里,數(shù)據(jù)庫服務(wù)器是在本地主機(jī)上運行的,端口為:27017,數(shù)據(jù)庫名為“ anoop”。

        復(fù)制代碼 代碼如下:

        private void Form1_Load(object sender, EventArgs e)

        {

        string connection = "mongodb://localhost:27017";

        _server = MongoServer.Create(connection);

        _database = _server.GetDatabase("anoop", SafeMode.True);

        }

        在這里,我們創(chuàng)建了三個使用不同屬性集合的類。我們可以設(shè)置這些類的屬性,并將數(shù)據(jù)保存至同一個數(shù)據(jù)庫、同一個表。這是無模式數(shù)據(jù)庫的真正優(yōu)勢:插入數(shù)據(jù)時不檢查模式。保存不同的記錄時可以用不同的域的集合,而其它的域 將 默認(rèn)被視為NULL。

        復(fù)制代碼 代碼如下:

        public class Users1

        {

        public ObjectId Id { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }

        }

        public class Users2

        {

        public ObjectId Id { get; set; }

        public string Name { get; set; }

        public string Location { get; set; }

        }

        public class Users3

        {

        public ObjectId Id { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }

        public string Location { get; set; }

        }

        private void rbEntity1_CheckedChanged(object sender, EventArgs e)

        {

        txtName.Enabled = true;

        txtAge.Enabled = true;

        txtLocation.Enabled = true;

        }

        private void rbEntity2_CheckedChanged(object sender, EventArgs e)

        {

        txtName.Enabled = true;

        txtAge.Enabled = false;

        txtLocation.Enabled = true;

        }

        private void rbEntity3_CheckedChanged(object sender, EventArgs e)

        {

        txtName.Enabled = true;

        txtAge.Enabled = true;

        txtLocation.Enabled = false;

        }

        private void btnSave_Click(object sender, EventArgs e)

        {

        if (rbEntity1.Checked)

        {

        var _users = _database.GetCollection("users");

        var user = new Users3 { };

        user.Age = Convert.ToInt32(txtAge.Text);

        user.Name = txtName.Text;

        user.Location = txtLocation.Text;

        _users.Insert(user);

        var id = user.Id;

        }

        else if (rbEntity2.Checked)

        {

        var _users = _database.GetCollection("users");

        var user = new Users2 { };

        user.Name = txtName.Text;

        user.Location = txtLocation.Text;

        _users.Insert(user);

        var id = user.Id;

        }

        else if (rbEntity3.Checked)

        {

        var _users = _database.GetCollection("users");

        var user = new Users1 { };

        user.Age = Convert.ToInt32(txtAge.Text);

        user.Name = txtName.Text;

        _users.Insert(user);

        var id = user.Id;

        }

        MessageBox.Show("User with name " + txtName.Text + " created");

        }

        復(fù)制代碼 代碼如下:

        ///下面的代碼幫助你從Mongo數(shù)據(jù)庫中查找一條現(xiàn)有記錄。

        _collection = _database.GetCollection("users");

        IMongoQuery query = Query.EQ("Name", "Anoop");

        Users1 _user = _collection.FindAs(query).FirstOrDefault();

        MessageBox.Show(_user.Age.ToString());

        復(fù)制代碼 代碼如下:

        ///下面的代碼幫助你更新Mongo數(shù)據(jù)庫中的一條現(xiàn)有記錄。

        _collection = _database.GetCollection("users");

        IMongoQuery query = Query.EQ("Name", "Anoop");

        Users1 _user = _collection.FindAs(query).FirstOrDefault();

        MessageBox.Show("Age before :" + _user.Age.ToString());

        //更新年齡的值

        _user.Age = 30;

        //保存更改

        _collection.Save(_user);

        MessageBox.Show("Age after :" + _user.Age.ToString());

      【NET連接MongoDB數(shù)據(jù)庫實例方法】相關(guān)文章:

      6個安全設(shè)置mongodb數(shù)據(jù)庫的命令07-01

      使用Java程序連接各種數(shù)據(jù)庫的方法07-01

      電路的連接方法07-03

      用php連接oracle數(shù)據(jù)庫的代碼06-28

      計算機(jī)三級數(shù)據(jù)庫知識:數(shù)據(jù)庫在連接中常見錯誤解決方法06-28

      收縮SQL數(shù)據(jù)庫的方法06-28

      windows下mongodb安裝教程06-28

      戴爾平板電腦連接藍(lán)牙耳機(jī)的方法07-12

      小米2連接電腦的方法07-10

      MySQL數(shù)據(jù)庫備份守則和方法04-22