C# 连接MongoDB,含用户验证
/// <summary> /// 构造函数 /// </summary> /// <param name="connectionStrings"></param> public MongodbHelper(string connectionStrings) { var mongoUrl = new MongoUrlBuilder(connectionStrings); string databaseName = mongoUrl.DatabaseName; MongoClientSettings mongoSettings = new MongoClientSettings(); TimeSpan t = new TimeSpan(3000); mongoSettings.ConnectTimeout = t;//设置连接超时时长 mongoSettings.MaxConnectionPoolSize = 2000;//设置连接池最大连接数 MongoCredential credentials = MongoCredential.CreateCredential(databaseName, mongoUrl.Username, mongoUrl.Password);//添加用户名、密码 mongoSettings.Credential = credentials; mongoSettings.Server = mongoUrl.Server;//服务器地址 mongoSettings.ReadPreference = new ReadPreference(ReadPreferenceMode.Primary); MongoClient client = new MongoClient(mongoSettings); }
转载于:https://www.cnblogs.com/wanggang2016/p/10550659.html