Hello everyone, i’m new to MongoDB.
I’m writing a new application in my desktop, but i cannot undestand how to check if the client is connected to the server before to make any query.
If i stop the service in windows, in the shell the connection to “mongo” fail.
So in the driver how can check this ? Thanks a lot.
private void Init()
{
client = new MongoClient("mongodb://localhost:27017");
}
private bool CheckConnectionToBatabase()
{
//verify is the server is up and running and if the client is connected to it?
var t = client.GetDatabase("prodcuts");
//return true or false
}
private void InsertDocument(BsonDocument document)
{
if(CheckConnectionToBatabase())
collection.InsertOne(document);
}
private void MongoDb_Load(object sender, EventArgs e)
{
Init();
GetDatabase("shop");
GetCollection("products");
}