Example #1
0
        /// <summary>
        /// 连接数据库
        /// </summary>
        /// <returns></returns>
        private MongoServer Connect(MongoDbConfig config)
        {
            if (_mongoSever == null)
            {
                try
                {
                    var mongoSettings = new MongoServerSettings();
                    mongoSettings.Server = new MongoServerAddress(config.Server, config.Port);

                    if (config.HasCredential)
                    {
                        mongoSettings.DefaultCredentials = new MongoCredentials(config.UserName, config.Password, true);
                    }

                    _mongoSever = new MongoServer(mongoSettings);
                    _mongoSever.Connect();
                }
                catch (Exception ex)
                {
                    throw (new Exception("_conn:" + config.ServerConn + "\r\n" + ex));
                }
            }

            return(_mongoSever);
        }
Example #2
0
        private bool Init(MongoDbConfig config)
        {
            _config     = config;
            _mongoSever = null;

            try
            {
                _mongoSever = Connect(config);
            }
            catch
            {
            }

            return(_mongoSever != null);
        }
Example #3
0
 public MongoDbConnection(MongoDbConfig config)
 {
     Init(config);
 }