Created
January 22, 2021 21:51
-
-
Save ricoisme/ba95018d60574d959ab898e11aa388e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string pkgLocation; | |
Package pkg; | |
Application app; | |
DTSExecResult pkgResults; | |
MyEventListener eventListener; | |
//Variables vars; | |
string appDirectory = Path.GetDirectoryName( | |
Assembly.GetExecutingAssembly().Location.ToString()); | |
//讀取封裝組態檔案 | |
XDocument pkgxdc = XDocument.Load(string.Format(@"{0}\Package.dtsConfig", appDirectory)); | |
var datas = from data in pkgxdc.Descendants("Configuration") | |
select data; | |
foreach (var data in datas) | |
{ | |
var attritems = from attritem in data.Attributes() | |
select attritem; | |
foreach (var at in attritems) | |
{ | |
if (at.Name.ToString().ToUpper() == "PATH" && at.Value.Contains("Password")) | |
{ | |
data.Element("ConfiguredValue").SetValue("登入密碼");//寫入密碼 | |
break; | |
} | |
} | |
} | |
pkgxdc.Save(string.Format(@"{0}\Package.dtsConfig", appDirectory)); | |
XDocument xdoc = XDocument.Load(string.Format(@"{0}\SSISData.xml", appDirectory)); | |
var items = from item in xdoc.Descendants("Item") | |
select item; | |
string pkgpath = string.Empty; | |
string pkgname = string.Empty; | |
foreach (var item in items) | |
{ | |
pkgpath = item.Element("FilePath").Value; | |
pkgname = item.Element("FileName").Value; | |
eventListener = new MyEventListener(); | |
pkgLocation = pkgpath + pkgname; | |
app = new Application(); | |
app.PackagePassword = "1234"; //設定Package密碼 | |
pkg = app.LoadPackage(pkgLocation, eventListener); | |
pkg.EnableConfigurations =true; | |
//匯入封裝組態檔案 | |
pkg.ImportConfigurationFile(string.Format(@"{0}\Package.dtsConfig", appDirectory)); | |
//vars = pkg.Variables; | |
//vars["strtype"].Value = value; //設定 SSIS 變數 | |
pkgResults = pkg.Execute(null, null, eventListener, null, null); | |
#region 清除連線字串密碼 | |
foreach (var data in datas) | |
{ | |
var attritems = from attritem in data.Attributes() | |
select attritem; | |
foreach (var at in attritems) | |
{ | |
if (at.Name.ToString().ToUpper() == "PATH" && at.Value.Contains("Password")) | |
{ | |
data.Element("ConfiguredValue").SetValue(""); | |
break; | |
} | |
} | |
} | |
pkgxdc.Save(string.Format(@"{0}\Package.dtsConfig", appDirectory)); | |
#endregion | |
Console.WriteLine(string.Format("執行結果:{0}", pkgResults.ToString())); | |
Console.ReadKey(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment