How to import an Excel sheet into multiple SQL tables using SSIS
Today we will be looking on how to import an excel sheet into multiple tables. Let’s take as an example the following excel spreadsheet:
Looking at the excel above we can identify two problems:
1) We need to split a sheet into three tables
2) No identifier columns
For this task we will be using SQL Server Integration Services. So let’s run the SQL Server Business Intelligence Development Studio and create a new Integration Services Project:
Now let’s create our data flow:
First of all we need to establish a connection to our Excel file. On the bottom , right-click on the Connection Managers and create a New Connection:
Next we will choose the Excel connection and point to our source file:
Now that we have our connection we need to add a Excel Source to our Data Fow:
Next double-click on the Excel source to change its settings like so:
If all is well we should be able to preview the data:
Now, one of the problems we identified earlier was the lack of a identifier column for both stores and products. So first we need to solve this with the help of the Derived Column Data Flow Transformation:
First we connect the Excel Source with the Derived Column. Then we double-click the Derived Column and configure it like so:
Ok, we now have the identifier columns but still the is no value for them. Let’s fill that gap. We need to ensure that every time the Excel is imported the same key is generated. We can do that with the help of MD5. So let’s add a Script Component to our Data Flow, setup and add the c# code for this task:
1) First we need to connect the Derived Column to our Script Component;
2) Secondly when we double-click the Script Component we must choose the Transformation Mode;
3) Next we go to the Input Columns and choose the new Derived Columns and also the STORE and COD Columns;
4) Finally we add our code to the component:
And the code:
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
Row.ProductId = Encode(Row.COD.ToLower());
Row.StoreId = Encode(Row.STORE.ToLower());
}
private Guid Encode(string text)
{
System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bs = System.Text.Encoding.UTF8.GetBytes(text);
bs = x.ComputeHash(bs);
System.Text.StringBuilder s = new System.Text.StringBuilder();
foreach (byte b in bs)
{
s.Append(b.ToString("x2").ToLower());
}
text = s.ToString();
return new Guid(text);
}
Like I said before, we will be using MD5 crypto to generate our unique columns. Sweet. One problem solved.
Now that we got our final source table we need to execute multiple operations on it. For that we will be using the MultiCast Transformation:
So, first lets populate the Products Table. We need a way to get the list of distinct products. For that we will use the Sort Transformation:
We first select the columns that we want to use and most importantly we must select “Remove rows with duplicate sort values” to have our distinct filter.
Let’s try it to see if it works. For that we will be adding a Grid Data Viewer to the connection between the Script Component and the Multicast. We, for test purpose only, will also add a Data Reader Destination and connect the Sort Transformation to the Data Reader Destination. Then will add a new Data Viewer to the connection.
So to add a Data Viewer simple right-click on the connection between components, click Add, select Grid type and click OK twice:
Notice now that we have a little goggles between our components:
So let’s run it:
When running we will be presented with a grid for the first Data viewer. Notice that our derived columns are now filled with unique values:
Cool.
The next Data Viewer is for the distinct products:
Nice.
Also notice that the data flow show the number of rows returned in each step:
Ok. The hard part is done. We now pretty much have to do a copy paste to fill the Store table:
We now have the distinct products and stores. Finally we will add the ADO NET Destination for both:
In the end:
Hope it helps J
ASP.net Webforms and the Passive View Design Pattern
This is more a personal reminder than an article.
From the view we can get the typed user name and set a message.
Now, let's update our default.aspx to become our view. First we need to add a textbox, named txtUserName, a button, named btnGreetUser and a literal that will hold our greeting message (named litMessage).
Next, in the code-behind file of our default.aspx page, we add the IHomeView and implement its properties:
Notice that:
- The page_load event of the page initializes a Presenter passing the instance of the view;
- Our View properties map to both the textbox, for getting the name, and to the literal, for setting the message;
- The button click handler calls a method of the Presenter.
Now that we've build the View we can't yet run the code: The Presenter is missing.
As I stated before, the Presenter will handle all te responses to user events, in this case, the button click event. So based on the needs of the view, our presenter will look like so:
Taking a closer look at the OnGreetUser() we can see that we take the user name from the view, build a simple greeting message and set back the message to the view thru the Message property of the view.
"That's all folks..."
We're done. If we run our application we can see how everything is hook up and a nice message is shown to the user when we types his name and presses the button.
Since there's a separation of the UI logic from the view implementation we can easly test our code:
Goo.gl Url Shortener API Wrapper
Just for the fun, there's a Goo.gl Url Shortener API wrapper on my codeplex corner.
GooglErrorResponse error = null;
var url = service.GetShortUrl("www.google.com", out error);
Console.WriteLine(url.Id);
var details = service.GetShortUrlDetails(url.Id, out error);
Console.WriteLine(details.Created);
Book review: A Guide to Claims-Based Identity and Access Control
It's been a while since my last book review. Today's book is A Guide to Claims-Based Identity and Access Control
First I would like to say thanks to Eugenio Pace (@eugenio_pace) for sending me the printed version, and to O'Reilly Media (@oreillymedia) for the digital version.using jQueryUI with Script# and the Script# Contrib project roadmap
Since my last post, I've started a new codeplex project to allow the usage of the jQueryUI in Script#.Getting started with Script# (ScriptSharp)
If you, like me, develop web applications, you got your hands all dirty with JavaScript.Script# brings productivity to Ajax and JavaScript development. Script# is a free tool that enables developers to author C# source code and subsequently compile it into regular script that works across all modern browsers, and in doing so, leverage the productivity and power of existing .NET tools as well as the Visual Studio IDE. Script# empowers you with a development methodology and approach that brings software engineering, long term maintainability and scalable development approaches for your Ajax applications, components and frameworks.
Script# is used extensively by developers within Microsoft building Ajax experiences in Windows Live, Office to name just a couple, as well as by a external developers and companies including Facebook. If you’re building Ajax-based RIA applications, you owe it to yourself to try Script# today and see if it can help improve your own Ajax development!
- The code we made in C# is almost identical to the code generated;
- All the client-side objects, like Window and Document, are available to C# code, so there is no new learning involved and with the benefit of compile time checks;
- All the documentation added to the C# code is also added to the JavaScript (on the debug file);
- We are working on a OO code on the client-side.
jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.
Book of the month: Programming Microsoft(R) ASP.NET MVC by Dino Esposito

Improve your code with NDepend
Although we all try to write the best code ever, we can't be sure unless we have some kind of code analysis software, like NDepend. This is one of those tools that any developer and architect should have in his toolbelt.
NDepend makes refactoring easier: With a set of standard rules, and the possibility to defines your own, we can exactly pinpoint improvement spots.
Welcome to the Code Query Language (CQL)
NDepend sees our code as a database, so we just need to query to find anything. For instance, querying our code for methods with more that 30 lines of code its as simple as SELECT TOP 10 METHODS WHERE NbLinesOfCode > 30 ORDER BY NbLinesOfCode DESC.
This is one of the more than 80 rules avalilable that run on your assemblies when you use NDepend. But what is great is that you can write your own set of rules using the CQL Query Editor with intellisense:

- NDepend CQL Editor
Control your dependency
We all know that type coupling can easily go out of control and increase the effort that any change introduces. Using the CQL rules with the Dependency Matrix window, one can spot tight coupling and fix it:

- Dependecy Windows
Always keep track
For me, one of the best features of NDepend is the ability to compare analysis, and this way track change evolutions.

- New code was added...
Full Report
Everytime that a analysis run, a full report is generated, so that you can, for instance, publish for your team easily understand the what's going on.

- Full Report
Who are my friends
Besides the standalone UI, NDepend works with Visual Studio and Reflector.

- Integration With Reflector and Visual Studio
What's your flavor?
NDepend is available not only for the .NET, but also for Java and C++. The new v3 fully integrates Visual Studio 2010 and a drastic performance and memory consumption improvement, to avoid slowing down VS. Sweeeet :)
What's next for you...



















