Following from yesterday’s post, here’s how to achive SSIS configurations with a script instead of a SQL statement and a loop.
Database setup is exactly the same, and the two variables for environment and configuiration database connection string are set up the same, but the package needs just a script task. The only property for the script which needs to be set is MaximumErrorCount which should be set to zero.
This script connects to the configuration database, selects all the variables that are valid for the current package and environment and applies them. In the script code add another using for System.Data.SQLClient then add the following:
public void Main()
{
// Create a variable dispenser and a variables collection
VariableDispenser disp = this.Dts.VariableDispenser;
Variables vars = default(Variables);
String connString = null;
String environmentName = null;
String packageName = null;
try
{
disp.LockForRead("User::strConfigDatabaseConnString");
disp.LockForRead("User::strEnvironment");
disp.LockForRead("System::PackageName");
disp.GetVariables(ref vars);
connString = vars["User::strConfigDatabaseConnString"].Value.ToString();
environmentName = vars["User::strEnvironment"].Value.ToString();
packageName = vars["System::PackageName"].Value.ToString();
try
{
// Connect to the configuration database
SqlConnection conn = new SqlConnection(connString);
conn.Open();
// Retrieve configuration values
String sql = String.Concat("select VarName, VarValue, VarType from ConfigVars where EnvironmentName = '",
@environmentName, "' and (PackageName = 'Sys' or PackageName = '", @packageName, "')");
SqlCommand command = new SqlCommand(sql, conn);
SqlDataReader reader = command.ExecuteReader();
// Loop through the reader
while (reader.Read())
{
String varName = reader["VarName"].ToString();
String varValue = reader["VarValue"].ToString();
String varType = reader["VarType"].ToString();
// Set the value for the variable
SetVariable(varName, varValue, varType);
}
// Tidy up
reader.Close();
reader.Dispose();
command.Dispose();
conn.Close();
conn.Dispose();
}
catch
{
Dts.Events.FireWarning(0, "Set configuration variable values", "Unable to retrieve configuration values from database.", "", 0);
}
}
catch
{
Dts.Events.FireWarning(0, "Set configuration variable values", "Unable to get configuration database connection string.", "", 0);
}
Dts.TaskResult = (int)ScriptResults.Success;
}
public void SetVariable(string varName, string varValue, string varType)
{
VariableDispenser disp = this.Dts.VariableDispenser;
Variables vars = default(Variables);
try
{
// Lock the variable we are going to provide a value for
disp.LockOneForWrite(varName, ref vars);
// Convert the value to the right type for the variable and assign it
if (varType.ToLower() == "string")
{
vars[0].Value = varValue;
}
else if (varType.ToLower() == "int32")
{
vars[0].Value = Convert.ToInt32(varValue);
}
else if (varType.ToLower() == "datetime")
{
// Add more strings to this array depending on the format of dates you have used in the config database
String[] sFormats = { "yyyy-mm-dd" };
DateTime dt = DateTime.ParseExact(varValue, sFormats, null, System.Globalization.DateTimeStyles.None);
vars[0].Value = dt;
}
else if (varType.ToLower() == "boolean")
{
vars[0].Value = Convert.ToBoolean(varValue);
}
Boolean bReFire = true;
Dts.Events.FireProgress(String.Concat("Set ", varName, " value to '", varValue, "'."), 100, 0, 0, "Set configuration variable values", ref bReFire);
}
catch (DtsRuntimeException ex)
{
Dts.Events.FireWarning(0, "Set configuration variable values", ex.Message, "", 0);
}
}
Very simple and very flexible.
