mongoDB串連資訊及產生對應的collection產生代碼

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   ar   sp   div   on   

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MongoDB.Driver; namespace CDPWIB.DAL { public class MongoConnectionFactory { //private static readonly string _connectionString = "mongodb://username:[email protected]:27000"; private static readonly string _connectionString = System.Configuration.ConfigurationSettings.AppSettings["entmongoconectionstring"]; //{ConnectionMode=Automatic;ConnectTimeout=00:00:30;Credentials={{[email protected]}};GuidRepresentation=CSharpLegacy;IPv6=False;MaxConnectionIdleTime=00:10:00;MaxConnectionLifeTime=00:30:00;MaxConnectionPoolSize=100;MinConnectionPoolSize=0;ReadPreference=Primary;ReplicaSetName=;SecondaryAcceptableLatency=00:00:00.0150000;Servers=10.10.10.252:27000;SocketTimeout=00:00:00;Ssl=False;SslVerifyCertificate=True;WaitQueueSize=500;WaitQueueTimeout=00:02:00WriteConcern=w=1;} //public static MongoClientSettings clintser = new MongoClientSettings //{ // //Credentials = new List<MongoCredential> // //{ // // MongoCredential.CreateMongoCRCredential("USERNAME", "DBNAME", "DB") // //}, // MaxConnectionLifeTime = new TimeSpan(0, 5, 0), // MaxConnectionIdleTime = new TimeSpan(0, 2, 0), // MinConnectionPoolSize = 0, // MaxConnectionPoolSize = 1000, // SocketTimeout = new TimeSpan(0, 2, 0), // WaitQueueTimeout = new TimeSpan(0, 0, 30), // //Server = new MongoServerAddress("10.10.10.252", 27000), // Server = new MongoServerAddress("127.0.0.1", 27000), // //WaitQueueTimeout = new TimeSpan(0, 2, 0), // // // ConnectTimeout = new TimeSpan(0, 0, 30), //}; public static MongoClientSettings clintser = new MongoClientSettings { Credentials = new List<MongoCredential> { //MongoCredential.CreateMongoCRCredential("usename", "db", "pwd") }, MaxConnectionLifeTime = new TimeSpan(0, 5, 0), MaxConnectionIdleTime = new TimeSpan(0, 2, 0), MinConnectionPoolSize = 0, MaxConnectionPoolSize = 1000, SocketTimeout = new TimeSpan(0, 2, 0), WaitQueueTimeout = new TimeSpan(0, 0, 30), //Server = new MongoServerAddress("10.10.10.252", 27000), Server = new MongoServerAddress("127.0.0.1", 27017), //WaitQueueTimeout = new TimeSpan(0, 2, 0), // ConnectTimeout = new TimeSpan(0, 0, 30), }; private static MongoClient mongoClient = new MongoClient(clintser); private static MongoServer mongoServer = mongoClient.GetServer(); public static MongoClient GetMongoClient() { return mongoClient; } public static MongoServer GetMongoServer() { //var seting = mongoServer.Settings; //seting.ConnectTimeout = new TimeSpan(0, 2, 0); //seting.MinConnectionPoolSize = 100; //seting.MaxConnectionPoolSize = 1000; //seting.SocketTimeout = new TimeSpan(0, 2, 0); //seting.WaitQueueTimeout = new TimeSpan(0, 2, 0); return mongoServer; } public static MongoDatabase GetMongoDataBase(string dbname) { return GetMongoServer().GetDatabase(dbname); } public static MongoCollection<T> GetMongoCollction<T>(MongoDatabase database, string collectionname) { return database.GetCollection<T>(collectionname); } public static MongoCollection<T> GetMongoCollction<T>(string databasename, string collectionname) { MongoDatabase database = GetMongoDataBase(databasename); return database.GetCollection<T>(collectionname); } public static MongoCollection<T> GetMongoCollction<T>(string databasename) { MongoDatabase database = GetMongoDataBase(databasename); return database.GetCollection<T>(typeof(T).Name); } public static MongoCollection GetMongoCollction(string databasename, string collectionname) { MongoDatabase database = GetMongoDataBase(databasename); return database.GetCollection(collectionname); } public static MongoCollection<T> GetMongoCollction<T>(MongoDatabase database, Type type) { return database.GetCollection<T>(type.Name); } public static MongoCollection<T> GetMongoCollction<T>(string databasename, Type type) { MongoDatabase database = mongoServer.GetDatabase(databasename); return database.GetCollection<T>(type.Name); } } } 

 

mongoDB串連資訊及產生對應的collection產生代碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.