I know that my title seems with others from StackOverflow but my intention is other.
I created a C# function which creates BCP files for every table from my database using BCP command. The below code is a sample, part of function.
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 );
}
I want to avoid executing multiple windows and I want to execute each command line in same window.
My code is good or is an alternative ?
Thanks
I’m not sure if there’s a way to open one command window and then keep posting your commands to it, that would be nice if someone had a clever way of doing that.
You could use Streamwriter to write a batch file with all the commands you want in it, and then run the batch file. That would at least only open one process.
The batch file has the “%1” in the command line so when you start the process you pass the password for the database to the batch file
This should make sure that if anyone ever found the batch file, it just has %1 where the password would be.
Not sure if this is better than what you have already… but it’s only opening one command window 🙂
Martyn