C#mongoDB驱动程序数据库连接

时间:2014-12-30 18:19:12

标签: c# asp.net mongodb mongodb-.net-driver

所以我刚开始编程并进入C#& noSQL数据库。 我目前正在开发一个小程序,它在MongoDB网络应用程序上显示ASP.NET数据库的数据。我坚持连接到我的本地数据库。

数据需要存储在lblMotherboard中: 这是我的代码。 builder.aspx:

<asp:Label ID="lblMotherboard" runat="server" Text=""></asp:Label>

builder.aspx.cs:

using MongoDB.Bson;
using MongoDB.Driver;

    public void showMobos_Click(object sender, EventArgs e)
    {
        var name = "";
        var connectionString = "mongodb://localhost:27017/";
        var mongoClient = new MongoClient(connectionString);
        MongoServer server = mongoClient.GetServer(); 
        MongoDatabase database = server.GetDatabase("mydb");

        MongoCollection<Post> mobos = database.GetCollection<Post>("moederborden");
        foreach (Post parts in mobos.FindAll())
        {
            name = name + " " + parts.Aanbieder + " " + parts.ProductNaam; 
        }
        lblMotherboard.Text = name; 
    }
单击按钮时

showMobos_Click开始。它不会打印任何数据。出了什么问题?

示例文件:

{
    "_id" : ObjectId("54a287ef0d0a7f888510d14e"),
    "Aanbieder" : "Coolblue",
    "Productlink" : "http://computerstore.nl/product/470130/category-208983/asrock-z97-extreme6.html",
    "Productnaam" : "Asus H97-Pro Gamer",
    "Prijs" : "129,-",
    "Socket" : "1150"
} 

Post.class:

public class Post
{
    public ObjectId _id { get; set; }
    public String Aanbieder { get; set; }
    public String Productlink { get; set; }
    public String Productnaam { get; set; }
    public decimal Prijs { get; set; } 
    public int Socket { get; set; } 
}

1 个答案:

答案 0 :(得分:2)

您必须将Prijs更改为数字

在mongo console中运行:

db.moederborden.find().forEach(function(doc)
{
    var price = doc.Prijs.replace(',', ''); // it may be vary for your other document
    price = price.replace('-', '');    
    doc.Prijs = Number(price);

    db.moederborden.update({_id : doc._id} , doc);
})