//public void SetConnectionString(string sConnectionString) //{ // msAccessDal.SetConnectionString(sConnectionString); //} public bool OpenMongoDb(OpenDatabaseParameters openDatabaseParameters, out string result) { return(mongoDbDal.OpenDb(openDatabaseParameters, out result)); }
// Purpose: Open database // Input: databaseParameters - see ContextRepositoryDataTypes.OpenDatabaseParameters in ContextRepository.Commom\Data Types.cs // result - [out] string // Output: true / false public bool OpenDb(OpenDatabaseParameters databaseParameters, out string result) { string report = string.Empty; string user_password = string.Empty; result = string.Empty; try { if (!string.IsNullOrEmpty(databaseParameters.DatabaseUsername)) { user_password = databaseParameters.DatabaseUsername + ":" + databaseParameters.DatabasePassword + "@"; } MongoIp = databaseParameters.DatabaseIpAddress; MongoPort = databaseParameters.DatabaseIpPort; _client = new MongoClient(string.Format("mongodb://" + user_password + "{0}:{1}", MongoIp, MongoPort)); DatabaseName = databaseParameters.DatabaseName; _database = _client.GetDatabase(databaseParameters.DatabaseName); // Remove the deserialize exception - without it when try to get an object from the mongo, // it will complain that we have no field named _id in the class var pack = new ConventionPack { new IgnoreExtraElementsConvention(true) }; ConventionRegistry.Register("IgnoreExtraElementsConvention", pack, t => true); if (IsConnected(out result)) { result = "Connect - Connected to MongoDB IP[" + MongoIp + "] Port[" + MongoPort + "] Database:[" + databaseParameters.DatabaseName + "]"; report += result; } else { result = "Failed Connecting to MongoDB IP[" + MongoIp + "] Port[" + MongoPort + "] Database:[" + databaseParameters.DatabaseName + "]"; string yesNo = (string.IsNullOrEmpty(user_password)) ? "No " : string.Empty; result += Environment.NewLine + yesNo + "User/Password Used To Authenticate"; return(false); } _collection = StringUtils.GetAllSubStrings(databaseParameters.DatabaseTables, ":"); if (_collection != null) { // get all needed tables/collections foreach (string currentCollection in _collection) { // if table/collection exists do nothing if (CollectionExist(currentCollection, out result)) { report += Environment.NewLine + "Collection[" + currentCollection + "] exists"; continue; } else { if (!string.IsNullOrEmpty(result)) { report += Environment.NewLine + result; } } // if table/collection does not exist create it CreateCollection(currentCollection, out result); report += Environment.NewLine + result; } } else { report += Environment.NewLine + "No collections in list"; result = report; return(false); } result = report; return(true); } catch (Exception e) { result = report + Environment.NewLine + e.Message; return(false); } }