使用mongodb_name在C#进程中不起作用

时间:2016-04-07 13:36:50

标签: c# windows mongodb process

我经常使用命令

使用some_mongodb

从Windows上的命令提示符创建新数​​据库

当你想在C#进程中执行它时,这个命令似乎不起作用

我有以下代码尝试从C#

创建mongo数据库
ProcessStartInfo startInfo = new ProcessStartInfo
{
    UseShellExecute = false,
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    RedirectStandardInput = true,
    WorkingDirectory = _mongoBinDir,
    FileName = "mongo.exe",
    Arguments = "use " + databaseTxt.Text
};

_mongoInsertProcess = new Process
{
    StartInfo = startInfo
};

_mongoInsertProcess.Start();

string stderrStr = _mongoInsertProcess.StandardError.ReadToEnd();
string stdoutStr = _mongoInsertProcess.StandardOutput.ReadToEnd();

stdoutStr 变量获取值

  

“MongoDB shell版本:3.2.1连接到:使用   2016-04-07T15:28:52.875 + 0200 E - [main]文件[some_db]没有   存在无法加载:some_db“

请就此提出建议。

1 个答案:

答案 0 :(得分:1)

use some_db不是一个有效的论据。只需传递数据库的名称即:

ProcessStartInfo startInfo = new ProcessStartInfo
{
    UseShellExecute = false,
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    RedirectStandardInput = true,
    WorkingDirectory = _mongoBinDir,
    FileName = "mongo.exe",
    Arguments = databaseTxt.Text
};