我是C#和mongoDB的新手。我编写了一个简单的代码来连接到MongoDB。但是当我运行该程序时,我收到了这个错误"
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
Additional information: Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Bson;
using MongoDB.Driver.GridFS;
namespace MongoDbTest
{
class Program
{
static void Main(string[] args)
{
var connectionString = "mongodb://localhost:27017";
MongoClient oClient = new MongoClient(connectionString);
MongoServer oServer = oClient.GetServer();
MongoDatabase db = oServer.GetDatabase("dblearnfiles");
MongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("user");
foreach (var item in collection.FindAll())
{
Console.WriteLine(item.ToString());
}
Console.WriteLine("LearnFiles");
Console.ReadKey();
}
}
}
我在这一行中遇到错误:
MongoClient oClient = new MongoClient(connectionString);
oClient为null !!
答案 0 :(得分:0)
我解决了那个。 我将此代码更改为:
var client = new MongoClient();
var db = client.GetDatabase("dblearnfiles");
var coll = db.GetCollection<BsonDocument>("user");
foreach(var item in coll.Find(new BsonDocument()).ToList())
{
Console.WriteLine(item);
}
Console.ReadKey();
并且工作了。