Execute mongoimport command via c#

Asked

Viewed 143 times

1

I need to execute mongoimport command from mongoDB via code but not for sure:

           `pro.FileName = "cmd.exe";
            //pro.UseShellExecute = false;
            pro.Arguments = @"/k cd C:\Program Files\MongoDB\Server\3.4\bin\";
            Process proStart = new Process();
            proStart.StartInfo = pro;
            proStart.Start();

I need to execute the mongoimport that is inside the bin, but I cannot.

Someone can help out?

1 answer

0


You are just opening cmd and navigating to the bin folder of Mongo

An example taken from Soen

    public bool importCsv(string filepath,  string collectionName)
    {

    string result ="";
    try
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = @"C:\Program Files\MongoDB\Server\3.4\bin\mongoimport.exe";
        startInfo.Arguments = @" -d test -c " + collectionName + " --type csv --file " + filepath + " --headerline";
        Process proc = new Process();
        proc.StartInfo = startInfo;
        proc.Start();
        result += "ddd";
    }
    catch(Exception e)
    {
        Console.WriteLine(e.ToString());
    }

    if (!result.Equals(""))
    {
        return true;
    }
    return false;
}

Note that filename is itself mongoimport

 startInfo.FileName = @"C:\Program Files\MongoDB\Server\3.4\bin\mongoimport.exe";

Source | Soen

  • Thank you, however I tried this way and also does not work, but does not prevent error so it is difficult to know what it is, but I called right the executable of mongoimport and passed as argument the command and even so does not work :/

  • Sorry, the code is wrong I’ll make the correction

  • Now yes, I took the Soen code and added it to your case and forgot to put the executable. Try this way and check if it solves

  • You have to pass the name of the executable mongoimport.exe

  • I did so even passing the mongoimport.exe in filename and it still doesn’t work!

  • The worst is that I can’t reproduce any error that might be occurring

  • If you call straight by the cmd will?

  • Yes, by the cmd: I go to Pasa bin and in it I call the mongoimport -d test -c ... and it works normal

  • Test this code on c# Process.Start(@"C:\Program Files\MongoDB\Server\3.4\bin\mongoimport.exe", @" -d test -c " + collectionName + " --type csv --file " + filepath + " --headerline");

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.