1. Create a SSIS project in Microsoft Visual Visual Studio.
2. Name the package "InsertItems.dtsx".
3.Create the following Parameters.
4.Create Data Flow Task
5.Edit the Data Flow detail.
The script code.
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Net.Mail;
using System.Net;
#endregion
namespace ST_fbf9ced29caf40f891b78b2e509a9bd5
{
/// <summary>
/// ScriptMain is the entry point class of the script. Do not change the name, attributes,
/// or parent of this class.
/// </summary>
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region Help: Using Integration Services variables and parameters in a script
/* To use a variable in this script, first ensure that the variable has been added to
* either the list contained in the ReadOnlyVariables property or the list contained in
* the ReadWriteVariables property of this script task, according to whether or not your
* code needs to write to the variable. To add the variable, save this script, close this instance of
* Visual Studio, and update the ReadOnlyVariables and
* ReadWriteVariables properties in the Script Transformation Editor window.
* To use a parameter in this script, follow the same steps. Parameters are always read-only.
*
* Example of reading from a variable:
* DateTime startTime = (DateTime) Dts.Variables["System::StartTime"].Value;
*
* Example of writing to a variable:
* Dts.Variables["User::myStringVariable"].Value = "new value";
*
* Example of reading from a package parameter:
* int batchId = (int) Dts.Variables["$Package::batchId"].Value;
*
* Example of reading from a project parameter:
* int batchId = (int) Dts.Variables["$Project::batchId"].Value;
*
* Example of reading from a sensitive project parameter:
* int batchId = (int) Dts.Variables["$Project::batchId"].GetSensitiveValue();
* */
#endregion
#region Help: Firing Integration Services events from a script
/* This script task can fire events for logging purposes.
*
* Example of firing an error event:
* Dts.Events.FireError(18, "Process Values", "Bad value", "", 0);
*
* Example of firing an information event:
* Dts.Events.FireInformation(3, "Process Values", "Processing has started", "", 0, ref fireAgain)
*
* Example of firing a warning event:
* Dts.Events.FireWarning(14, "Process Values", "No values received for input", "", 0);
* */
#endregion
#region Help: Using Integration Services connection managers in a script
/* Some types of connection managers can be used in this script task. See the topic
* "Working with Connection Managers Programatically" for details.
*
* Example of using an ADO.Net connection manager:
* object rawConnection = Dts.Connections["Sales DB"].AcquireConnection(Dts.Transaction);
* SqlConnection myADONETConnection = (SqlConnection)rawConnection;
* //Use the connection in some code here, then release the connection
* Dts.Connections["Sales DB"].ReleaseConnection(rawConnection);
*
* Example of using a File connection manager
* object rawConnection = Dts.Connections["Prices.zip"].AcquireConnection(Dts.Transaction);
* string filePath = (string)rawConnection;
* //Use the connection in some code here, then release the connection
* Dts.Connections["Prices.zip"].ReleaseConnection(rawConnection);
* */
#endregion
/// <summary>
/// This method is called when this script task executes in the control flow.
/// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
/// To open Help, press F1.
/// </summary>
public void Main()
{
// TODO: Add your code here
OleDbDataAdapter AddedNewItemsDA = new OleDbDataAdapter();
System.Data.DataTable AddedNewItemsDT = new System.Data.DataTable();
AddedNewItemsDA.Fill(AddedNewItemsDT, Dts.Variables["User::AddedItems"].Value);
string AddedItemsStr = "";
OleDbDataAdapter DuplicateItemsDA = new OleDbDataAdapter();
System.Data.DataTable DuplicateItemsDT = new System.Data.DataTable();
DuplicateItemsDA.Fill(DuplicateItemsDT, Dts.Variables["User::DuplicateItems"].Value);
string DuplicateItemsStr = "";
string ToAddress = Dts.Variables["$Package::ToAddress"].Value.ToString();
//MessageBox.Show(ToAddress);
int AddedItemCount = AddedNewItemsDT.Rows.Count;
int DuplicateItemCount = DuplicateItemsDT.Rows.Count;
if (AddedItemCount != 0)
{
AddedItemsStr = "Added the following " + AddedItemCount.ToString() + " new item(s). \r\n";
foreach (DataRow row in AddedNewItemsDT.Rows)
{
AddedItemsStr = AddedItemsStr + row.ItemArray[0].ToString() + "\r\n";
}
}
else
{
AddedItemsStr = "No item(s) are added! \r\n";
}
if (DuplicateItemCount !=0)
{
DuplicateItemsStr = "The following " + DuplicateItemCount.ToString() + " item(s) are duplicate. They are already in the system. \r\n";
foreach (DataRow row in DuplicateItemsDT.Rows)
{
DuplicateItemsStr = DuplicateItemsStr + row.ItemArray[0].ToString() + "\r\n";
}
}
string Mailbody = AddedItemsStr + "\r\n" + "\r\n" + DuplicateItemsStr;
//MessageBox.Show(Mailbody);
SendMail(ToAddress, Mailbody);
Dts.TaskResult = (int)ScriptResults.Success;
}
public void SendMail(string to, string mailbody)
{
MailAddress Mailto = new MailAddress(to);
MailAddress Mailfrom = new MailAddress("smtpxx@dxxfxx.com");
MailMessage mail = new MailMessage(Mailfrom, Mailto);
mail.Subject = "NAV Adding New Items Log.";
mail.Body = mailbody;
SmtpClient smtp = new SmtpClient();
smtp.Host = "Smtp.office365.com";
smtp.Port = 587;
smtp.Credentials = new NetworkCredential("smtpxx@dxxfxx.com", "yourpassword");
smtp.EnableSsl = true;
smtp.Send(mail);
}
#region ScriptResults declaration
/// <summary>
/// This enum provides a convenient shorthand within the scope of this class for setting the
/// result of the script.
///
/// This code was generated automatically.
/// </summary>
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}
7.Create a Master.dtsx package.
8.Master package control flow.
9.After run the package. It will send an email as log.