利用Script Task,我们可以做一些本身SSIS没能满足我们的,或者实现起来效果不够理想的。比如说我们想做一件这样的事情,去检查某个文件是否为空。如果我们通过Row Count组件来实现,性能上不理想,因为我又并不需要要知道究竟文件包含多少行数据。我们只需要简单地知道文件是否包含数据。我们可以通过写C#代码,去调用BinaryStream的方法来读取该文件的前几行就可以确定是否文件包含数据。总的来说,Script Task能做事情分几类:1) 读取和改变包的变量;2)读取包的属性;3)用C#或者VS代码实现业务逻辑,做有一些验证检查工作;4)控制workflow的执行;
上面说到的那个检查数据文件是否为空的例子,我们可以把这样的C#代码放到一个统一的项目中,然后编译生成DLL库(Assembly),再加载到Script Task里面(或者说被引用),这样可以减少代码的重复。
Script Task中的DTS对象其实是对Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel的实例化。它包含了一下的成员:
1) Connections:可以引用Package的Connection Managers
2) Events:可以引用Package的Events
3) ExecutionValue:
4) TaskResult:可以用来手动指定成功(success)或者失败(failure)
5) Transaction
6) VariableDispenser:可以用来获取包的Variables
7) Variables:可以用来存储Varibales
8) Log:写log
引用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
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
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 Operation
at {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 attention
to...”, ””, 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);
}