Bước 1:
Làm thế nào để Kết nối dữ liệu MongoDB bằng C#?
Để kết nối tới MongoDB ta dùng lệnh sau:
|
1
2
3
4
5
6
|
//tham chiếu thư viện này:using MongoDB.Driver;//Dùng MongoClient để kết nối tới ServerMongoClient client = new MongoClient("mongodb://localhost:27017");//Dùng lệnh GetDatabase để kết nối Cơ sở dữ liệuIMongoDatabase database = client.GetDatabase("QuanLySanPham"); |
Các Em lưu ý lệnh:
mongodb://localhost:27017
Là cú pháp kết nối lên Server. Chúng ta chỉ đổi localhost thành tên Server(địa chỉ IP) và Port. Còn mọi thứ phải giữ nguyên. Đây là bản Standalone, nếu bạn đang dùng Replica Set thì thay đổi khác chút xíu (Đây là phần nâng cao, khoan hãy quan tâm).
Lệnh:
client.GetDatabase(“QuanLySanPham“);
Dùng để kết nối tới Cơ sở dữ liệu tên là QuanLySanPham, do đó bạn muốn kết nối tới Cơ sở dữ liệu khác thì đổi tên chỗ này là xong.
Bước 2:
Truy vấn dữ liệu, trong bài này ta sẽ truy vấn bảng Product nha.
IMongoCollection collection = database.GetCollection("Product");//Muốn truy vấn toàn bộ dữ liệu trong bảng dùng lệnh dưới đây:List documents = collection.Find(new BsonDocument()).ToList();Để lấy dữ liệu từng đối tượng (JSON, vì sao nó là JSon thì các Em xem lại các bài trước, hoặc ở đây) trong documents ra ta làm như sau:
//Các em có thể hiểu document là 1 đối tượng đang duyệt (là Json) trong tập các Json được lưu trong biến documentsforeach (BsonDocument document in documents){//Cột Ma (là thuộc tính Ma của đối tượng Product đang duyệt) có kiểu chuỗistring ma = document["Ma"].AsString;//Cột Ten (là thuộc tính Ten của đối tượng Product đang duyệt) có kiểu chuỗistring ten = document["Ten"].AsString;//Cột DonGia (là thuộc tính DonGia của đối tượng Product đang duyệt) có kiểu Doubledouble gia = document["DonGia"].AsDouble;}Coding chi tiết:
-Phần XAML (MainWindow.xaml):
using System;
using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using MongoDB.Bson;using MongoDB.Driver;namespace CSharpMongoDBExample{////// Interaction logic for MainWindow.xaml/// public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}private void btnGetProduct_Click(object sender, RoutedEventArgs e){MongoClient client = new MongoClient("mongodb://localhost:27017");IMongoDatabase database = client.GetDatabase("QuanLySanPham");IMongoCollection collection = database.GetCollection("Product");List documents = collection.Find(new BsonDocument()).ToList();lstProduct.Items.Clear();foreach (BsonDocument document in documents){string ma = document["Ma"].AsString;string ten = document["Ten"].AsString;double gia = document["DonGia"].AsDouble;lstProduct.Items.Add(ma + "\t" + ten + "\t" + gia);}}}}-Phần Coding(MainWindow.xaml,cs):Chạy lên ta sẽ kết quả như mong muốn!
» Tin mới nhất:
» Các tin khác: