Quantcast
Channel: Welcome To TechBrothersIT
Viewing all articles
Browse latest Browse all 1874

Load Files information to SQL Server Table

$
0
0
Scenario:
Let's say we want to load the files information from folder to SQL Server Table.
We want to load the File Name, File created datetime and File size in KiloBytes.

Solution:

Lets start by creating SQL Server Table in Test Database

use Test
go
CREATE TABLE dbo.FileInformation
(
Filename VARCHAR(100),
FileCreatedDateTime DATETIME,
FileSize INT
)

GO


SSIS Package:

Step 1: create new ssis package, In package create variable VarDirectoryPath String Type.
provide value to this variable C:\MyTestFolder

Step2: Create ADO.NET Connection to Test Database.Rename to "ADO.NET DBConnection"

Step3: Drag Script task to control flow pane, provide VarDirectortyPath as input variable as shown in picture


Step4: Edit the script task and paste the following code in Editor and Press OK.
/*
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 Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace ST_fb681f538a6d4b16a093c8c3eef34a4c.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{

#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion

/*
The execution engine calls this method when the task executes.
To access the object model, use the Dts property. Connections, variables, events,
and logging features are available as members of the Dts property as shown in the following examples.

To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
To post a log entry, call Dts.Log("This is my log text", 999, null);
To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);

To use the connections collection use something like the following:
ConnectionManager cm = Dts.Connections.Add("OLEDB");
cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";

Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.

To open Help, press F1.
*/

public void Main()
{


SqlConnection myADONETConnection = new SqlConnection();
myADONETConnection = (SqlConnection)(Dts.Connections["ADO.NET DBConnection"].AcquireConnection(Dts.Transaction) as SqlConnection);
MessageBox.Show(myADONETConnection.ConnectionString, "ADO.NET Connection");
myADONETConnection.Close();
myADONETConnection.Open();
string DirPath = Dts.Variables["User::VarDirectoryPath"].Value.ToString();
string[] files = Directory.GetFiles(DirPath);
SqlCommand sqlCmd= new SqlCommand();
sqlCmd.Connection = myADONETConnection;
foreach (string filename in files)
{
MessageBox.Show(" I Am in Each loop");
FileInfo file = new FileInfo(filename);
MessageBox.Show(file.Name+","+file.CreationTime+","+file.Length/1024);

sqlCmd.CommandText = "Insert into dbo.FileInformation Values('" + file.Name + "','" + file.CreationTime + "','" + file.Length/1024 + "')";
MessageBox.Show(sqlCmd.CommandText);

sqlCmd.ExecuteNonQuery();
}
myADONETConnection.Close();
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}


You are done, execute your package and you will see data in Test.dbo.FileInformation table.

I left MessageBoxes for debugging, please remove them after testing your package.

Viewing all articles
Browse latest Browse all 1874

Trending Articles