C# 在C中使用相同ProcessStartInfo实例执行多个命令(使用BCP)#

C# 在C中使用相同ProcessStartInfo实例执行多个命令(使用BCP)#,c#,sql,sql-server,process,C#,Sql,Sql Server,Process,我知道我的头衔似乎和StackOverflow的其他人一样,但我的意图是其他的 我创建了一个C#函数,它使用BCP命令为数据库中的每个表创建BCP文件。下面的代码是一个示例,是函数的一部分 List<string> tables = GetTables(); // a function which takes all tables from my database string command = @"bcp ADatabase.dbo.{0} out {0}.bcp -n -Us

我知道我的头衔似乎和StackOverflow的其他人一样,但我的意图是其他的

我创建了一个C#函数,它使用BCP命令为数据库中的每个表创建BCP文件。下面的代码是一个示例,是函数的一部分

List<string> tables = GetTables(); // a function which takes all tables from my database

string command = @"bcp ADatabase.dbo.{0} out {0}.bcp -n -Usa -Ppassword";

ProcessStartInfo processInfo = null;
     try {
        foreach ( string table in tables ) {
           command = string.Format( command, table );
           processInfo = new ProcessStartInfo( "cmd", "/c " + command );
           processInfo.RedirectStandardOutput = true;
           processInfo.CreateNoWindow = false;
           Process process = new Process();
           process.StartInfo = processInfo;
           process.Start();
           string result = process.StandardOutput.ReadToEnd();
           Console.WriteLine( result );
        }
     } catch ( Exception e ) {
        Console.WriteLine( e.Message );
     }
List tables=GetTables();//从我的数据库中获取所有表的函数
string命令=@“bcp ADatabase.dbo.{0}out{0}.bcp-n-Usa-Ppassword”;
ProcessStartInfo processInfo=null;
试一试{
foreach(表中的字符串表){
command=string.Format(命令,表格);
processInfo=newprocessstartinfo(“cmd”、“/c”+命令);
processInfo.RedirectStandardOutput=true;
processInfo.CreateNoWindow=false;
流程=新流程();
process.StartInfo=processInfo;
process.Start();
字符串结果=process.StandardOutput.ReadToEnd();
控制台写入线(结果);
}
}捕获(例外e){
控制台写入线(如消息);
}
我希望避免执行多个窗口,并且希望在同一窗口中执行每个命令行

我的代码是好的还是替代的


谢谢

一个非常简单的选择是使用
&
操作符构建一个巨大的命令。这里有一个片段来说明这个想法:
cmd/c echo hello&&echo world
。这里的缺点是,如果您的任何任务都失败了,那么就有点难以确定是哪一个任务


您可以将命令写入本地*.bat文件,然后处理该文件。然后,如果一个失败了,您就可以更好地了解它失败的地方,并且您希望将cmd参数从
/c
切换到
/k
,这样您就可以保持窗口打开以查看发生了什么。

我不确定是否有办法打开一个命令窗口,然后继续向其发布命令,如果有人能想出一个聪明的方法,那就太好了

您可以使用Streamwriter编写一个包含所有所需命令的批处理文件,然后运行批处理文件。这至少只会打开一个进程

StreamWriter sw = new StreamWriter("MyBatchFile.bat");
            string command = @"bcp ADatabase.dbo.{0} out {0}.bcp -n -Usa -%1";
            ProcessStartInfo processInfo = null;
            try
            {
                foreach (string table in tables)
                {
                    command = string.Format(command, table);
                    sw.WriteLine(command);                                        
                }
                sw.Flush();
                sw.Close();
                processInfo = new ProcessStartInfo("cmd", "/c MyBatchFile.Bat Ppassword");
                processInfo.RedirectStandardOutput = true;
                processInfo.CreateNoWindow = false;
                Process process = new Process();
                process.StartInfo = processInfo;
                process.Start();
                string result = process.StandardOutput.ReadToEnd();
                Console.WriteLine(result);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
批处理文件的命令行中有“%1”,因此,当启动该进程时,将数据库的密码传递给批处理文件

ProcessStartInfo("cmd", "/c MyBatchFile.Bat Ppassword");
这应该确保如果有人找到了批处理文件,它的密码只有%1

不确定这是否比你已经拥有的更好。。。但它只打开了一个命令窗口:)

马汀