Thursday, May 3, 2012

C# CODE TO INSERT DATA TO REMOTE SERVER MONGODB FROM CSV FILE


using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Driver;
using MongoDB.Bson;
using System.IO;
using System.Text.RegularExpressions;


namespace csharpMongo
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceFile=@"F:\FILE.csv";
            StreamReader sr = new StreamReader(sourceFile);
            string line = sr.ReadLine();
            var columnNames = Regex.Split(line, ",");
            string connectionString = "mongodb://xxx.xxx.x.xxx"; //TARGET IP ADDRESS
            MongoServer mongo = MongoServer.Create(connectionString);
            mongo.Connect();
            var db = mongo.GetDatabase("database_name");  // TARGET DATABASE NAME
       
            using (mongo.RequestStart(db))
            {
                var collection = db.GetCollection<BsonDocument >("Collection_name");
                while ((line = sr.ReadLine()) != null)
                {
                    string[] cols=Regex .Split (line ,",");
                    BsonDocument book = new BsonDocument();
                    for (int i = 0; i < columnNames.Length; i++)
                    {
                        book.Add(columnNames[i], cols[i]);
                    }
                    collection.Insert(book);
                }
            }

         
            mongo.Disconnect();
        }
    }
}

C# CODE TO COPY FILE FROM WINDOWS TO UBUNTU



using System;
using System.Collections.Generic;
using Tamir.SharpSsh;
//using Tamir.Streams;


namespace CopyFileToUbuntu
{
    class Program
    {
        static void Main(string[] args)
        {

            string host = "ipaddress";
            string username = "username";
            string password = "password";
            string sourcePath = @"F:\sample.json";
            //string sourcePath = @"F:\";
            string destPath = "/home/myfolder/Archive/";

            Sftp sftpClient = new Sftp(host, username, password);

            sftpClient.Connect();
            sftpClient.Put(sourcePath, destPath);
         
            //sftpClient.Get( destPath,sourcePath );
            sftpClient.Close();
        }
    }
}