Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install and use MongoDb in C #

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to install and use MongoDb in C #". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to install and use MongoDb in C #".

MongoDb visualization tool

I use Robomongo, which feels good to use and can be downloaded by myself.

C # operation

Create a new WinForm program, then right-click in the reference and select manage NuGet package.

Enter MongoDb in browsing to search

Install the first MongoDb.Driver, and then you will have the corresponding DLL in the reference.

Note: it is found that MongoServer cannot be used in the course of use, so it is necessary to install mongocsharpdriver.

Add a TextBox and a Button to the form interface

Then we create a new Class named CPersonal, which corresponds to the Personal of our MyDemo database in Mongodb.

To establish a database connection, you must know the address, port and other information of the server. All of this information is represented by a connection string. The connection string format for MongoDB is as follows:

Mongodb:// [username:password@] host1 [: port1] [, host2 [: port2], … [, hostN [: portN] [/ [database] [? options]]

Let's take a look at the meaning of each field in the connection string:

Mongodb://: this is the prefix of the MongoDB connection string

Username:password (Optional): optional, indicating login username and password, used to complete user security authentication

HostN: must specify at least one host, indicating the MongoDB instance to which you are connected

PortN (Optional): optional, connect to 27017 by default

Database (Optional): if you specify a username:password@, connection and verify login to the specified database. If not specified, the admin database is opened by default.

Options (Optional): optional, if you don't use / database, you need to precede it with /. All connection options are key-value pairs name=value, separated by & or; (semicolon)

We write the events of button1 to query all the information in Personal.

Var list = collection.AsQueryable ()

Foreach (CPersonal item in list)

{

TextBox1.AppendText ("name:" + item.name + "age:" + item.age)

TextBox1.AppendText ("\ r\ n")

}

Get all the Personal data through collection.AsQueryable, then output it with foreach, and click the button effect after running it.

If you look for people older than 30, you can use var list = collection.AsQueryable () above, followed by the lamda expression of where.

Var list = collection.AsQueryable () .where (t = > t.age > 30)

The effect is as follows

The method of changing to non-Lamda expression

Var list = from t in collection.AsQueryable ()

Where t.age > 30

Select t

At this point, I believe you have a deeper understanding of "how to install and use MongoDb in C #". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report