C # connection to MongoDB Database Application Practice

Source: Internet
Author: User

1. Download the driver

C # DRIVER:

Remote download: http://cloud.github.com/downloads/mongodb/mongo-csharp-driver/CSharpDriver-1.1.0.4184.zip

 

Download CSharpDriver-1.1.0.4184.zip locally

 

Decompress it to the D: \ MongoDB \ drivers \ directory, where there are 2 important DLL files

MongoDB. bson. dll-serialization, JSON-related
MongoDB. Driver. dll -- driver

2. Add reference

Create a C # project, add references, and introduce the above two DLL files to the project:

3,CodeAnalysis

The following uses an insert operation as an example to explain the code step by step:

Using system;

Using system. Collections. Generic;

Using system. LINQ;

Using system.
Text
;

//
Add namespace

Using MongoDB. bson;

Using MongoDB. driver;

Namespace consoleapplication3

{

Class Program

{

Static void main (string
[]
ARGs)

{


//
MongoDBServer
Connection string

String connectionstring
=
"MongoDB:
//
192.168
.
1.103
";

Mongoserver Server
=
Login server.
Create
(Connectionstring );


//
Connect to mongodb_c_demo Database

Analytic database DB
=
Server. getdatabase ("mongodb_c_demo ");


//
Get the set fruit

Mongocollection collection
=
DB. getcollection ("Fruit ");


//
Create object fruit_1

Bsondocument fruit_1
=
New bsondocument

{

{"Webste", "http://www.my400800.cn
"},

{"Name", "400 phone
"}

};


//
Create object fruit_2

Bsondocument fruit_2
=
New bsondocument

{

{"
Webste
"," Http://www.hrxc.net "},

{"
Name
"," Hua Ren Xin Cheng
"}

};


//
Put the object fruit_1 in the set fruit

Collection.
Insert
(Fruit_1 );


//
Put the object fruit_2 in the set fruit

Collection.
Insert
(Fruit_2 );


//
The above code inserts two pieces of data into the fruit table.


//
Insert

Into
Export db_c_demo.fruit (name, color)


//
Values
(
'
Webste
'
,
'
Name
'
),(
'
Http://www.hrxc.net
'
,
'
Hua Ren Xin Cheng
'
);

}

}

}

4. Verify whether to insert data through MongoDB shell:

>

Use
Mongodb_c_demo

Switched
To
DB mongodb_c_demo

>
DB. Fruit. Find ();

{"_ Id": objectid ("4da1c5fdfad96211a08f5752 "),"
Webste
":"
Http://www.my400800.cn
","
Name
":"
400 phone
"}

{"_ Id": objectid ("4da1c5fdfad96211a08f5753 "),"
Webste
":"
Http://www.hrxc.net
","
Name
":"
Hua Ren Xin Cheng
"}

>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.