Esempio n. 1
0
        public mongodbproxy(String ip, short port)
        {
            var setting = new MongoDB.Driver.MongoServerSettings();

            setting.Server = new MongoDB.Driver.MongoServerAddress(ip, port);
            _mongoserver   = new MongoDB.Driver.MongoServer(setting);
        }
Esempio n. 2
0
        /// <summary>
        /// 根据ObjectID 查询
        /// </summary>
        public BsonDocument LogSelectOne(string objId)
        {
            BsonDocument docFind = new BsonDocument();

            MongoDB.Driver.MongoServer server = MongoDB.Driver.MongoServer.Create(ConfigurationManager.AppSettings["mongoDBConfig"]);
            server.Connect();
            //获取指定数据库
            MongoDB.Driver.MongoDatabase db = server.GetDatabase(ConfigurationManager.AppSettings["mongoDBName"].ToString());
            //获取表
            MongoDB.Driver.MongoCollection <BsonDocument> col = db.GetCollection <BsonDocument>(ConfigurationManager.AppSettings["mongoDBCollection"].ToString());

            try
            {
                var query = new MongoDB.Driver.QueryDocument("_id", new ObjectId(objId));
                docFind = col.FindOne(query);
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
            }
            finally
            {
                server.Disconnect();
            }
            return(docFind);
        }
Esempio n. 3
0
        /// <summary>
        /// 根据ObjectID 查询
        /// </summary>
        public BsonDocument LogSelectOne(string objId)
        {
            BsonDocument docFind = new BsonDocument();

            MongoDB.Driver.MongoServer server = MongoDB.Driver.MongoServer.Create(ConfigurationManager.AppSettings["mongoDBConfig"]);
            server.Connect();
            //获取指定数据库
            MongoDB.Driver.MongoDatabase db = server.GetDatabase(ConfigurationManager.AppSettings["mongoDBName"].ToString());
            string appid = string.Empty;

            if (!string.IsNullOrEmpty(txtAppid.Text.Trim()))
            {
                appid = txtAppid.Text.Trim().Split(' ')[0];
            }
            //获取表
            MongoDB.Driver.MongoCollection <BsonDocument> col = db.GetCollection <BsonDocument>(appid + "logs");

            try
            {
                var query = new MongoDB.Driver.QueryDocument("_id", new ObjectId(objId));
                docFind = col.FindOne(query);
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
            }
            finally
            {
                server.Disconnect();
            }
            return(docFind);
        }
Esempio n. 4
0
        public mongodbproxy(String ip, short port, String db, String collection)
        {
            var setting = new MongoDB.Driver.MongoServerSettings();

            setting.Server = new MongoDB.Driver.MongoServerAddress(ip, port);
            _mongoserver   = new MongoDB.Driver.MongoServer(setting);
            var _db = _mongoserver.GetDatabase(db);

            _collection = _db.GetCollection <MongoDB.Bson.BsonDocument> (collection);
        }
Esempio n. 5
0
        protected void Unnamed1_Click(object sender, System.EventArgs e)
        {
            string fileName = @"D:\stefan.steiger\Downloads\keywords.xlsx";

            MongoDB.Driver.MongoClient   client   = new MongoDB.Driver.MongoClient("mongodb://localhost/27017/mydb");
            MongoDB.Driver.MongoServer   server   = client.GetServer();
            MongoDB.Driver.MongoDatabase database = server.GetDatabase("testdb");
            MongoGridFs gridFs = new MongoGridFs(database);

            MongoDB.Bson.ObjectId id = MongoGridFsUsage.UploadFile(gridFs, fileName);
            // byte[] baFile = DownloadFile(gridFs, id);
            // DownloadFile(gridFs, id, @"d:\test.rar");
        }
Esempio n. 6
0
 public static bool IsOnline()
 {
     MongoDB.Driver.MongoServer server = MongoDB.Driver.MongoServer.Create(GetConnectionString());
     try
     {
         server.Ping();
         server.Disconnect();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Esempio n. 7
0
        // http://odetocode.com/blogs/scott/archive/2013/04/16/using-gridfs-in-mongodb-from-c.aspx
        public static void Test()
        {
            string fileName = "clip_image071.jpg";

            MongoDB.Driver.MongoClient   client   = new MongoDB.Driver.MongoClient();
            MongoDB.Driver.MongoServer   server   = client.GetServer();
            MongoDB.Driver.MongoDatabase database = server.GetDatabase("testdb");
            MongoGridFs gridFs = new MongoGridFs(database);


            MongoDB.Bson.ObjectId id = UploadFile(gridFs, fileName);
            byte[] baFile            = DownloadFile(gridFs, id);
            DownloadFile(gridFs, id, @"d:\test.rar");
        } // End Sub Test
 public void Mongo000DefaultConnect()
 {
     try
     {
         Console.WriteLine("Creating client...");
         MongoDB.Driver.MongoClient _client = new MongoDB.Driver.MongoClient();
         Console.WriteLine("Getting Server...");
         MongoDB.Driver.MongoServer _server = _client.GetServer();
         Console.WriteLine("Connecting...");
         _server.Connect();
         Console.WriteLine("Connection Successful.");
     }
     catch (Exception exc)
     {
         new frmException(exc, "You are not running MongoDB on localhost:27017." + Environment.NewLine + "So it is failing.").ShowDialog();
         Console.WriteLine("Connection UnSuccessful.");
     }
 }
 public void Mongo020ConnectClientSettings()
 {
     try
     {
         Console.WriteLine("Creating client...");
         MongoDB.Driver.MongoClient _client = new MongoDB.Driver.MongoClient(new MongoDB.Driver.MongoClientSettings {
             Server = new MongoDB.Driver.MongoServerAddress(Program.MongoData.Host, Program.MongoData.Port)
         });
         Console.WriteLine("Getting Server...");
         MongoDB.Driver.MongoServer _server = _client.GetServer();
         Console.WriteLine("Connecting...");
         _server.Connect();
         Console.WriteLine("Connection Successful.");
     }
     catch (Exception exc)
     {
         new frmException(exc, "You are not running MongoDB on " + Program.MongoData.Host + ":" + Program.MongoData.Port + "." + Environment.NewLine + "So it is failing.").ShowDialog();
         Console.WriteLine("Connection UnSuccessful.");
     }
 }
 public void Mongo020DB()
 {
     try
     {
         String connString = "mongodb://" + Program.MongoData.Host + ":" + Program.MongoData.Port;
         Console.WriteLine("Creating client...");
         Console.WriteLine(connString);
         MongoDB.Driver.MongoClient _client = new MongoDB.Driver.MongoClient(connString);
         Console.WriteLine("Getting Server...");
         MongoDB.Driver.MongoServer _server = _client.GetServer();
         Console.WriteLine("Getting database " + Program.MongoData.Database + "...");
         MongoDB.Driver.MongoDatabase _database = _server.GetDatabase(Program.MongoData.Database);
         Console.WriteLine(_database.Name);
     }
     catch (Exception exc)
     {
         new frmException(exc, "You are not running MongoDB on " + Program.MongoData.Host + ":" + Program.MongoData.Port + "." + Environment.NewLine + "Or you do not have rights to list databases.").ShowDialog();
         Console.WriteLine("Connection UnSuccessful.");
     }
 }
 public void Mongo000ShowDbs()
 {
     try
     {
         String connString = "mongodb://" + Program.MongoData.Host + ":" + Program.MongoData.Port;
         Console.WriteLine("Creating client...");
         Console.WriteLine(connString);
         MongoDB.Driver.MongoClient _client = new MongoDB.Driver.MongoClient(connString);
         Console.WriteLine("Getting Server...");
         MongoDB.Driver.MongoServer _server = _client.GetServer();
         Console.WriteLine("Connecting...");
         String[] databases = _server.GetDatabaseNames().ToArray();
         foreach (String db in databases)
         {
             Console.WriteLine("\t" + db);
         }
         Console.WriteLine(databases.Length + " databases listed.");
     }
     catch (Exception exc)
     {
         new frmException(exc, "You are not running MongoDB on " + Program.MongoData.Host + ":" + Program.MongoData.Port + "." + Environment.NewLine + "Or you do not have rights to list databases.").ShowDialog();
         Console.WriteLine("Connection UnSuccessful.");
     }
 }
Esempio n. 12
0
 public void InitializeDatabaseConnection(String connectionString, String dbname)
 {
     client   = new MongoDB.Driver.MongoClient(connectionString);
     server   = client.GetServer();
     Database = server.GetDatabase(dbname);
 }