利用Script Task,咱們能夠作一些自己SSIS沒能知足咱們的,或者實現起來效果不夠理想的。好比說咱們想作一件這樣的事情,去檢查某個文件是否爲空。若是咱們經過Row Count組件來實現,性能上不理想,由於我又並不須要要知道究竟文件包含多少行數據。咱們只須要簡單地知道文件是否包含數據。咱們能夠經過寫C#代碼,去調用BinaryStream的方法來讀取該文件的前幾行就能夠肯定是否文件包含數據。總的來講,Script Task能作事情分幾類:1) 讀取和改變包的變量;2)讀取包的屬性;3)用C#或者VS代碼實現業務邏輯,作有一些驗證檢查工做;4)控制workflow的執行;sql
上面說到的那個檢查數據文件是否爲空的例子,咱們能夠把這樣的C#代碼放到一個統一的項目中,而後編譯生成DLL庫(Assembly),再加載到Script Task裏面(或者說被引用),這樣能夠減小代碼的重複。app
Script Task中的DTS對象實際上是對Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel的實例化。它包含了一下的成員:oop
1) Connections:能夠引用Package的Connection Managers
2) Events:能夠引用Package的Events性能
3) ExecutionValue:ui
4) TaskResult:能夠用來手動指定成功(success)或者失敗(failure)debug
5) Transactionorm
6) VariableDispenser:能夠用來獲取包的Variablesxml
7) Variables:能夠用來存儲Varibales
對象
8) Log:寫logip
引用SSIS包的變量: Dts.Variables[「User::SomeStringVariable」].Value = 「MyValue」;
改變包的變量Dts.Variables[「User::HappyPathEnum」].Value = 1;
改變TaskResult屬性來告訴SSIS的包當前組件任務的執行是成功仍是失敗了,從而影響後面整個workflow的執行
Dts.TaskResult = (int)ScriptResults.Success;
VariableDispenser提供了鎖定變量的方法能夠防止在解除鎖定該變量被改變
Dts.VariableDispenser.LockForRead("SomeVariable")
Dts.VariableDispenser.GetVariables(vars)能夠把當前包的variables賦給某個variables集合
嘗試從Variables集合中讀取沒有賦制值的變量將會拋出異常
Script Task對變量是大小寫敏感的,必須把要再script代碼中用到變量設置到ReadOnlyVariables或ReadWriteVariables
下面是一個Script Task鏈接DataSource的例子:
public void Main()
{
string myPackageId = Dts.Variables[「System::PackageID」].Value.ToString();
string myValue = string.Empty;
string cmdString = 「SELECT VALUE FROM SSIS_SETTING 「 +
「WHERE PACKAGE_ID= @PACKAGEID And SETTING= @SETTINGID」;
try
{
SqlConnection mySqlConn =
(SqlConnection)Dts.Connections[0].AcquireConnection(null);
mySqlConn = new SqlConnection(mySqlConn.ConnectionString);
mySqlConn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = cmdString;
SqlParameter parm = new SqlParameter(「@PACKAGEID」, SqlDbType.UniqueIdentifier);
parm.Value = new Guid(myPackageId);
cmd.Parameters.Add(parm); parm = new SqlParameter(「@SETTINGID」,
SqlDbType.NVarChar);
parm.Value = 「LOGFILEPATH」;
cmd.Parameters.Add(parm);
cmd.Connection = mySqlConn;
cmd.CommandText = cmdString;
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
myValue = reader[「value」].ToString();
}
Dts.Variables[「User::LOGFILEPATH」].Value = myValue;
reader.Close();
mySqlConn.Close();
mySqlConn.Dispose();
}
catch
{
Dts.TaskResult = (int)ScriptResults.Failure;
throw;
}
System.Windows.Forms.MessageBox.Show(myValue);
Dts.TaskResult = (int)ScriptResults.Success;
}
下面是Script Task從FTP Server拿數據的例子:
ConnectionManager conn = default(ConnectionManager);
FtpClientConnection ftp = default(FtpClientConnection);
string[] folderNames = null;
string[] fileNames = null;
ArrayList fileArray = new ArrayList();
conn = Dts.Connections(「FTPServer」);
ftp = new FtpClientConnection(conn.AcquireConnection(null));
ftp.Connect();
ftp.GetListing(folderNames, fileNames);
foreach (string s in fileNames) {
fileArray.Add(s);
}
Dts.Variables(「FileList」).Value = fileArray;
ftp.Close();
Dts.TaskResult = ScriptResults.Success;
下面是Script Task寫入數據到XML文件的例子:
public void Main()
{
SqlConnection sqlConn;
string cmdString = 「SELECT * FROM SSIS_SETTING 「;
try
{
sqlConn = (SqlConnection)(Dts.Connections[「AdventureWorks」])
.AcquireConnection(Dts.Transaction
);
sqlConn = new SqlConnection(sqlConn.ConnectionString);
sqlConn.Open();
SqlCommand cmd = new SqlCommand(cmdString, sqlConn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ds.WriteXml(new System.IO.StreamWriter
(「C:\\ProSSIS\\Files\\myPackageSettings.xml」));
sqlConn.Close();
}
catch
{
Dts.TaskResult = (int)ScriptResults.Failure;
throw;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
下面是Script Task序列化數據再寫入到XML文件的例子:
public void Main()
{
SqlConnection sqlConn;
string cmdString = 「SELECT * FROM SSIS_SETTING 「;
try
{
sqlConn = (SqlConnection)(Dts.Connections[「AdventureWorks」])
.AcquireConnection(Dts.Transaction
);
sqlConn = new SqlConnection(sqlConn.ConnectionString);
sqlConn.Open();
SqlCommand cmd = new SqlCommand(cmdString, sqlConn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ds.WriteXml(new System.IO.StreamWriter
(「C:\\ProSSIS\\Files\\myPackageSettings.xml」));
sqlConn.Close();
}
catch
{
Dts.TaskResult = (int)ScriptResults.Failure;
throw;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
用XMLSerializer類來序列化XML數據
[Serializable()]
public class SSISSetting
{
public string PackageId { get; set; }
public string Setting { get; set; }
public string Value { get; set; }
}
public void Main()
{
SqlConnection sqlConn;
string cmdString = 「SELECT * FROM SSIS_SETTING 「;
try
{
sqlConn = (SqlConnection)(Dts.Connections[「AdventureWorks」])
.AcquireConnection(Dts.Transaction);
sqlConn = new SqlConnection(sqlConn.ConnectionString);
sqlConn.Open();
SqlCommand cmd = new SqlCommand(cmdString, sqlConn);
SqlDataReader dR = cmd.ExecuteReader();
List<SSISSetting> arrayListSettings = new List<SSISSetting>();
while (dR.Read())
{
SSISSetting oSet = new SSISSetting();
oSet.PackageId = dR[「PACKAGE_ID」].ToString();
oSet.Setting = dR[「SETTING」].ToString();
oSet.Value = dR[「VALUE」].ToString();
arrayListSettings.Add(oSet);
}
StreamWriter outfile = new StreamWriter
(「C:\\ProSSIS\\Files\\myObjectXmlSettings.xml」);
XmlSerializer ser = new XmlSerializer(typeof(List<SSISSetting>));
ser.Serialize(outfile, arrayListSettings);
outfile.Close();
outfile.Dispose();
sqlConn.Close();
}
catch
{
Dts.TaskResult = (int)ScriptResults.Failure;
throw;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
咱們也能夠手工觸發Event Handler
public void Main(){string taskName = Dts.Variables[「System::TaskName」].Value.ToString();bool retVal = false;Dts.Events.FireInformation(0, taskName, String.Format(「Starting Loop Operation at {0} 「,DateTime.Now.ToString(「MM/dd/yyyy hh:mm:ss」)), 「」, 0,ref retVal);for(int i=0; i <= 10; i++){Dts.Events.FireProgress(String.Format(」Loop in iteration {0}」, i),i * 10, 0, 10, taskName, ref retVal);}Dts.Events.FireInformation(0, taskName, String.Format(」Completion Loop Operationat {0} 」, DateTime.Now.ToString(」mm/dd/yyyy hh:mm:ss」)), 」」, 0, ref retVal);Dts.Events.FireWarning(1, taskName, 」This is a warning we want to pay attentionto...」, 」」, 0);Dts.Events.FireWarning(2, taskName, 」This is a warning for debugging only...」,」」, 0);Dts.Events.FireError(0, taskName, 」If we had an error it would be here」, 」」, 0);}