Calling Command
December 18, 2012 Leave a comment
I know I am supposed to be using (and loving) power shell, but I ran across a problem this weekend and the good-old command window worked fine. I was building a one-click application that moved data from one location to another and then manipulating the data. As part of the workflow, I created a DTS/SSIS package. To execute this package, I used the following code to shell out to the command prompt and fire up the package:
String sourceConnectionString = CreateSourceConnectionString(); String targetConnectionString = CreateTargetConnectionString(); Process process = new Process(); process.StartInfo.FileName = "cmd"; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendFormat(@"/k"); stringBuilder.AppendFormat(@"dtexec"); stringBuilder.AppendFormat(@" /F"); stringBuilder.AppendFormat(@" TransferAllTables.dtsx"); stringBuilder.AppendFormat(" /Conn {0};\"{1}\"", "SourceConnectionOLEDB", sourceConnectionString); stringBuilder.AppendFormat(" /Conn {0};\"{1}\"", "DestinationConnectionOLEDB", targetConnectionString); process.StartInfo.Arguments = stringBuilder.ToString(); process.Start();
I also want to thank my friend Ian (who doesn’t have a blog yet so I can’t point you to him) for mentioning the StringBuilder.AppendFormat() function. Append() + String.Format() in 1 place. Nice! Thanks Ian!