SSIS Package to Resize image

SSIS Package to Resize image.

This script helps you to resize images as you needed in percentage(Dynamically).

Step1 : drag n drop foreeach loop container.

Step2 : drag n drop script task in to foreach loop container.

Step3 : Right click on any part of your control flow and select variables to see list of variables.

Create two new string variable with name “ImageFileName” and “ImagePath” and assign your local image folder path to ImagePath Variable value.

Note: Here local path is “D:\Work\Images”.

[image5.png]

Step4: Right click on foreeachloop container and click on edit.Select Collection from left pane.

Select Foreeach File Enumerator in Enumerator and select your variable “@[User:ImagePath]” in directory and click ok.

SSIS Resize Image 2

Step5 : Go to variable mappings and select imagefilename variable with index 0 and click ok.

SSIS Resize Image 3

Step6: Right click on script task and click on edit.

Select Script from left pane.

Select variable imagefilename for readonlyvariables.

SSIS Resize Image 4

Step7: Click on Edit Script and paste below code.

#region Help:  Introduction to the script task

/* The Script Task allows you to perform virtually any operation that can be accomplished in

  * a .Net application within the context of an Integration Services control flow.

  *

  * Expand the other regions which have "Help" prefixes for examples of specific ways to use

  * Integration Services features within this script task. */

 #endregion

 #region Namespaces

 using System;

 using System.Data;

 using System.Drawing;

 using System.Drawing.Drawing2D;

using System.Drawing.Imaging;

using Microsoft.SqlServer.Dts.Runtime;

using System.Windows.Forms;

#endregion

namespace ST_f8e4dbfd074e4f9296466d7c3bc1f83d

{

    ///

    /// ScriptMain is the entry point class of the script.  Do not change the name, attributes,

    /// or parent of this class.

    ///
   [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]

    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

  {

       public void Main()

       {

             try

             {// TODO: Add your code here

                 string ImageFileName;

                 ImageFileName = Dts.Variables["ImageFileName"].Value.ToString();

                 // Get the scale factor.

                 using (Image img = Image.FromFile(ImageFileName))

                 {

                     int percent = 25;

                     //int i;

                     //for (i= img.Height; i> 100; i= (img.Height * 90) / 100)

                     //{

                     //    percent = percent - 10;

                     //}

                     int width = Convert.ToInt32(img.Width * (percent * 0.01));

                     int height = Convert.ToInt32(img.Height * (percent * 0.01));

                     ImageFormat format = img.RawFormat;

                     Image thumbNail = new Bitmap(width, height, img.PixelFormat);

                     Graphics g = Graphics.FromImage(thumbNail);

                     g.CompositingQuality = CompositingQuality.HighQuality;

                     g.SmoothingMode = SmoothingMode.HighQuality;

                     g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                     Rectangle rect = new Rectangle(0, 0, width, height);

                     g.DrawImage(img, rect);                   

                     img.Dispose();

                     thumbNail.Save(ImageFileName, format);

                 }

                 Dts.TaskResult = (int)ScriptResults.Success;

             }

             catch (Exception ex)

             {

                 MessageBox.Show(ex.Message.ToString());

             }

      }

         #region ScriptResults declaration

         ///

        /// This enum provides a convenient shorthand within the scope of this class for setting the

        /// result of the script.

        ///

        /// This code was generated automatically.

        ///
        enum ScriptResults

        {

            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,

            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure

        };

        #endregion

  }

}

Regards,
Nirav Gajjar

SSIS Package to Read IISLog

SSIS Package to Read IISLog

You need LogParser 2.2 installed on your machine.

I need iislog every 1 hour so i have set this package to execute every hour and imports second last files to database.
Before you start creating package you have to create one batch file and one table.

Step1 : First of all you have to create batch file with below script and save it to E:\BatchFiles\Process_IISLog.bat

"c:\program files (x86)\log parser 2.2\LogParser.exe" "SELECT s-computername,LogFileName ,rOWnUMBER,S-SITENAME,S-IP,CS-METHOD,CS-URI-STEM,CS-URI-QUERY,S-PORT,CS-USERNAME,C-IP, SC-STATUS,SC-SUBSTATUS, SC-WIN32-STATUS,SC-BYTES,CS-BYTES,TIME-TAKEN INTO TestDB1.dbo.IISLogDetails FROM %1" -i:W3C -o:SQL -server:ExpertsClick -database:testdb1 -username:sa -password:pwd

Step2 : Create table to store IIS Log.

USE [TestDB1]
GO
/****** Object: Table [dbo].[IISLogDetails] Script Date: 07-02-2013 21:20:19 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[IISLogDetails](
[HostName] [varchar](25) NULL,
[LogFileName] [varchar](150) NULL,
[RowNumber] [int] NULL,
[LogFolder] [varchar](25) NULL,
[IP] [varchar](25) NULL,
[HTTP_Method] [varchar](5) NULL,
[WebURL] [varchar](100) NULL,
[QueryString] [varchar](250) NULL,
[Port] [int] NULL,
[UserName] [varchar](25) NULL,
[ClientIP] [varchar](25) NULL,
[StatusNumber] [int] NULL,
[SubStatus] [int] NULL,
[Win32Status] [int] NULL,
[SCBytes] [int] NULL,
[CSBytes] [int] NULL,
[TimeTaken] [int] NULL
) ON [PRIMARY]
GO

SET ANSI_PADDING OFF
GO

Step3 : Create new integration service project with name “ReadIISLog”.

Step4 : Drag Foreeach Loop Container from toolbox.

Step5 : Drag script task under Foreeach Loop Container.

Step6 : Create two variables named “LogFileName” and “MainLogPath” with data type string.

5

Step7 : assign path to MainLogPath variable for iislog.
default path is “C:\WINDOWS\system32\LogFiles\W3SVC456758617”

Step8 : Right click on Foreach Loop Container and click on Edit.

1

Step9 : Click on Expressions button and select Directory in Property column and drag MainLogPath variable in Expression.

2

Step10 : Click OK on Property Expressions Editor.

Step11 : Select Variable Mapping in left penal of Foreeach Loop Editor and choose User::LogFileName in Variable and 0 as Index.

3

Step12: Select Script task editor, right click on it and click on Edit. Select User::LogFileName as ReadOnlyVariables.

4

Step13: Click on Edit Script and Paste Below Code.

.
/*
Microsoft SQL Server Integration Services Script Task
Write scripts using Microsoft Visual C# 2008.
The ScriptMain is the entry point class of the script.
*/
using System;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Data.SqlClient;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;

namespace ST_42efca1c007548f2bf3c1dfc977efd42.csproj
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{ <code>
 Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,</code>
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
public void Main()
{
try
{
string LogFileName = Dts.Variables["User::LogFileName"].Value.ToString();
FileInfo LogFile = new FileInfo(LogFileName);
DateTime LogFileDate = LogFile.LastWriteTime;
TimeSpan timespan = (DateTime.Now - LogFileDate);
if (timespan.Hours >= 1)
{
//From Command Line.
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "E:\\BatchFiles\\Process_IISLog.bat";
proc.StartInfo.Arguments = LogFile.FullName.ToString();
proc.StartInfo.RedirectStandardError = false;
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit();
//Move Imported file to oldlog folder.
File.Move(LogFileName, LogFile.DirectoryName.ToString() + "\\oldLog\\" + LogFile.Name.ToString());
}
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message.ToString());
}
}
}
}

Regards,
Nirav Gajjar