[英]How do I pass DTEXEC utility command line arguments to process.start using C# syntax?
我必須通過ASP.NET代碼將參數傳遞給DTEXEC實用程序。 我正在使用process.start()
方法來觸發SSIS包執行。
dtexec /file C:\ssis\pkg1.dtsx
/conn "MyConnectionManager";"\"Data Source=localhost\TestSQL2008R2;Initial Catalog=ConnDB;Integrated Security=SSPI;\""
/file C:\ssis\pkg1.dtsx
/conn "MyConnectionManager;Data Source=localhost\TestSQL2008R2;Initial Catalog=ConnDB;Integrated Security=SSPI;"
上述命令行參數是在通過DTEXEC實用程序手動執行程序包時生成的。 但是,我需要使用C#通過此命令行到達ASP.NET中的process.start()
方法。
如何在C#字符串語句中表示上述命令行? 換句話說,如何使用@
字符轉義特殊字符並將有效的語句傳遞給process.start()
?
逐字字符串文字可能最容易用於對此類字符串進行硬編碼。
您只需要將每個內部"
加倍即可:
@"dtexec /file C:\ssis\pkg1.dtsx /conn ""MyConnectionManager"";""\""Data Source=localhost\TestSQL2008R2;Initial Catalog=ConnDB;Integrated Security=SSPI;\""""";
和:
@"/file C:\ssis\pkg1.dtsx /conn ""MyConnectionManager;Data Source=localhost\TestSQL2008R2;Initial Catalog=ConnDB;Integrated Security=SSPI;""";
隨着“正規軍”的字符串,則需要每個逃生"
和\\
在原來有\\
在他們面前。
冒着失去主題的風險,與調用dtexec可執行文件相比,您擁有一種更好的機制。 由於您位於.net中,因此請使用現有的對象模型 。 這比通過DTEXEC提供的有限的抽象更為強大。
粗略的代碼看起來像
string packagePath = @"C:\ssis\pkg1.dtsx";
string connectionString = @"Data Source=localhost\TestSQL2008R2;Initial Catalog=ConnDB;Integrated Security=SSPI;"
Application app = new Application();
Package pkg = null;
pkg = app.LoadPackage(packagePath, null);
pkg.Connections["MyConnectionManager"].ConnectionString = connectionString;
pkg.Execute();
我沒有測試這些,但是下面的應該做。
var string1 =
"dtexec /file C:\\ssis\\pkg1.dtsx /conn \"MyConnectionManager\";\"\\\"Data Source=localhost\\TestSQL2008R2;Initial Catalog=ConnDB;Integrated Security=SSPI;\\\"\"";
var string2 = "dtexec /file C:\\ssis\\pkg1.dtsx /conn \"MyConnectionManager;Data Source=localhost\\TestSQL2008R2;Initial Catalog=ConnDB;Integrated Security=SSPI;\"";
如果你去老式的路線就像我上面顯示,你只需要逃避\\
(因為這是你的逃生字符)和"
與\\
在字符串中的任何地方。如果你使用版本batim喜歡對方的回答表明,它將更易於閱讀和維護。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.