当前位置: 首页>>代码示例>>C#>>正文


C# MongoClient.GetDatabaseNames方法代码示例

本文整理汇总了C#中MongoDB.Driver.MongoClient.GetDatabaseNames方法的典型用法代码示例。如果您正苦于以下问题:C# MongoClient.GetDatabaseNames方法的具体用法?C# MongoClient.GetDatabaseNames怎么用?C# MongoClient.GetDatabaseNames使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在MongoDB.Driver.MongoClient的用法示例。


在下文中一共展示了MongoClient.GetDatabaseNames方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        public static void Main(string[] args)
        {
            // http://stackoverflow.com/questions/6499268/mongodb-connection-refused
            // I ran into the same issue because I upgraded my mongo using brew.
            // To fix this issue open the config file /etc/mongod.conf
            // and comment out the "bind_ip" property.

            // http://docs.mongodb.org/ecosystem/tutorial/use-csharp-driver/

            // mongodb://server2/?ssl=true
            // mongodb://[username:[email protected]]hostname[:port][/[database][?options]]
            // string connectionString = "mongodb://localhost:27020/mydb";
            string connectionString = "mongodb://localhost:27017/mydb";
            // string connectionString = "mongodb://localhost:27017";
            MongoServer server = new MongoClient(connectionString).GetServer();
            System.Collections.Generic.List<string> ls = (System.Collections.Generic.List<string>)
                server.GetDatabaseNames ();

            System.Console.WriteLine (ls.Count);
            System.Console.WriteLine ("Hello World!");
        }
开发者ID:moacap,项目名称:MongoUpload,代码行数:21,代码来源:Program.cs

示例2: GetInstanceNode

 /// <summary>
 /// 获取实例节点
 /// </summary>
 /// <param name="mongoConnKey"></param>
 /// <param name="config">由于是结构体,必须ref</param>
 /// <param name="mongoConn"></param>
 /// <param name="mMasterServerInstace"></param>
 /// <param name="mServer"></param>
 /// <param name="UserList"></param>
 /// <returns></returns>
 private static TreeNode GetInstanceNode(String mongoConnKey,
                                         ref ConfigHelper.MongoConnectionConfig config,
                                         MongoServer mongoConn,
                                         MongoServerInstance mMasterServerInstace,
                                         MongoServer mServer,
                                         EachDatabaseUser UserList)
 {
     Boolean isReplsetMasterServer = false;
     //无论如何,都改为主要服务器读优先
     if (mMasterServerInstace == null)
     {
         isReplsetMasterServer = true;
     }
     TreeNode SvrInstanceNode = new TreeNode();
     String ConnSvrKey;
     if (isReplsetMasterServer)
     {
         ConnSvrKey = mongoConnKey + "/" + mongoConnKey;
     }
     else
     {
         ConnSvrKey = mongoConnKey + "/" + mMasterServerInstace.Address.ToString().Replace(":", "@");
     }
     SvrInstanceNode.SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.WebServer;
     SvrInstanceNode.ImageIndex = (int)GetSystemIcon.MainTreeImageType.WebServer;
     if (isReplsetMasterServer)
     {
         SvrInstanceNode.Text = "Connection";
     }
     else
     {
         SvrInstanceNode.Text = "Server[" + mMasterServerInstace.Address.ToString() + "]";
     }
     if ((!String.IsNullOrEmpty(config.UserName)) & (!String.IsNullOrEmpty(config.Password)))
     {
         //是否是认证模式,应该取决于服务器!
         config.AuthMode = true;
     }
     //获取ReadOnly
     config.IsReadOnly = false;
     List<String> databaseNameList = new List<String>();
     if (!String.IsNullOrEmpty(config.DataBaseName))
     {
         //单数据库模式
         TreeNode mongoSingleDBNode;
         if (isReplsetMasterServer)
         {
             mongoSingleDBNode = FillDataBaseInfoToTreeNode(config.DataBaseName, mServer, mongoConnKey + "/" + mongoConnKey);
         }
         else
         {
             mongoSingleDBNode = FillDataBaseInfoToTreeNode(config.DataBaseName, mServer, mongoConnKey + "/" + mMasterServerInstace.Address.ToString());
         }
         mongoSingleDBNode.Tag = SINGLE_DATABASE_TAG + ":" + ConnSvrKey + "/" + config.DataBaseName;
         mongoSingleDBNode.SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.Database;
         mongoSingleDBNode.ImageIndex = (int)GetSystemIcon.MainTreeImageType.Database;
         SvrInstanceNode.Nodes.Add(mongoSingleDBNode);
         SvrInstanceNode.Tag = SINGLE_DB_SERVER_TAG + ":" + ConnSvrKey;
         ///获取User信息
         if (config.AuthMode)
         {
             try
             {
                 ///尝试添加用户信息
                 UserList.AddUser(mongoConn.GetDatabase(config.DataBaseName), config.UserName);
             }
             catch (Exception)
             {
                 //可能出现没有权限的问题,这里就认为无法取得权限
             }
         }
     }
     else
     {
         MongoServer InstantSrv;
         if (isReplsetMasterServer)
         {
             InstantSrv = mServer;
             databaseNameList = mServer.GetDatabaseNames().ToList<String>();
         }
         else
         {
             MongoClientSettings setting = CreateMongoClientSettingsByConfig(ref config);
             setting.ConnectionMode = ConnectionMode.Direct;
             //When Replset Case,Application need to read admin DB information
             //if Primary,there will be exception
             setting.ReadPreference = ReadPreference.PrimaryPreferred;
             setting.Server = mMasterServerInstace.Address;
             InstantSrv = new MongoClient(setting).GetServer();
             databaseNameList = InstantSrv.GetDatabaseNames().ToList<String>();
//.........这里部分代码省略.........
开发者ID:ZhenghaiYe,项目名称:MagicMongoDBTool,代码行数:101,代码来源:MongoDBHelper_Connection_Present.cs

示例3: MongoDb_Supports_Basic_Crud_Operations

        public void MongoDb_Supports_Basic_Crud_Operations()
        {
            const string connectionString = "mongodb://localhost";

            if (!MongoDbRepositoryManager.ServerIsRunning(connectionString))
            {
                AssertIgnores.MongoServerIsNotRunning();
            }

            var server = new MongoClient(connectionString).GetServer();
            var databaseNames = server.GetDatabaseNames();
            foreach (var db in databaseNames)
            {
                server.DropDatabase(db);
            }

            var database = server.GetDatabase("Order");
            var orders = database.GetCollection<Order>("Order");

            Console.WriteLine("* CREATE *");

            var create = new Order { Name = "Big sale" };
            database.GetCollection<Order>("Order").Insert(create);

            foreach (var order in database.GetCollection<Order>("Order").AsQueryable())
            {
                Console.WriteLine(order.Name + ", " + order.OrderId);
            }

            Console.WriteLine("* READ *");

            var read = orders.AsQueryable().FirstOrDefault(e => e.OrderId == create.OrderId);
            read.Name.ShouldEqual(create.Name);

            Console.WriteLine("* UPDATE *");

            read.Name = "Really big sale";
            database.GetCollection<Order>("Order").Save(read);

            foreach (var order in database.GetCollection<Order>("Order").AsQueryable())
            {
                Console.WriteLine(order.Name + ", " + order.OrderId);
            }

            var update = database.GetCollection<Order>("Order").AsQueryable().FirstOrDefault(e => e.OrderId == read.OrderId);
            update.OrderId.ShouldEqual(read.OrderId);
            update.Name.ShouldEqual(read.Name);

            Console.WriteLine("* DELETE *");

            var delete = database.GetCollection<Order>("Order").AsQueryable().FirstOrDefault(e => e.OrderId == update.OrderId);

            database.GetCollection<Order>("Order").Remove(Query.EQ("OrderId", delete.OrderId));

            foreach (var order in database.GetCollection<Order>("Order").AsQueryable())
            {
                Console.WriteLine(order.Name + ", " + order.OrderId);
            }

            database.GetCollection<Order>("Order").RemoveAll();

            Console.WriteLine("* DELETE ALL *");

            foreach (var order in database.GetCollection<Order>("Order").AsQueryable())
            {
                Console.WriteLine(order.Name + ", " + order.OrderId);
            }

            server.DropDatabase("Order");
        }
开发者ID:mgmccarthy,项目名称:SharpRepository,代码行数:70,代码来源:MongoRepositorySpikes.cs


注:本文中的MongoDB.Driver.MongoClient.GetDatabaseNames方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。