Thursday, July 9, 2009

XML SQL Server - The Best Way To Process Complex Data Arrays

In this article I'll try to demonstrate one of the best ways to process arrays of data inside SQL Server by using XML.

Most developers have faced the challenge of passing arrays of data inside a SQL Server Database by using Stored Procedures.

I have seen other implementations to solve this problem, like using comma separated values, but from my point of view using XML is by far the best way, as it allows to pass arrays of data and complex structures at the same time.

I'll start with a simple example where we want to send to a SQL Server Database the following data:

ID 1
Name Albert Einstein

Email aeinstein@amazingpeople.com


ID 2

Name Salvador Dali

Email sdali@amazingpeople.com

The first step is of course to build the stored procedure to process our XML data.
For demonstration purposes we'll just select the data and display it. On a real scenario you might want to execute some other (CRUD) operations.
In the code bellow I'll use the OPENXML and a cursor to make things a litle bit more interesting:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Ricardo Matos http://matosdotnet.com/
-- Create date:
-- Description: Demonstration of the OpenXML feature
-- =============================================
CREATE PROCEDURE [dbo].[SP_ProcessXML_People]
-- Add the parameters for the stored procedure here
@xmlDocument xml
AS
BEGIN

SET NOCOUNT ON;

DECLARE @docHandle int;

DECLARE @ID nvarchar(20);
DECLARE @Name nvarchar(50);
DECLARE @Email nvarchar(50);

EXEC sp_xml_preparedocument @docHandle OUTPUT, @xmlDocument;

DECLARE cusor_XMLCursor CURSOR FOR
SELECT
[ID],
[Name],
[Email]

FROM OPENXML(@docHandle, N'/PEOPLE/PEOPLE',1)
WITH (
[ID] nvarchar(20) 'ID'
,[Name] nvarchar(50) 'Name'
,[Email] nvarchar(50) 'Email'
)
VENDORS;
----
OPEN cusor_XMLCursor;
--
FETCH NEXT FROM cusor_XMLCursor INTO
@ID, @Name, @Email;
--
WHILE @@FETCH_STATUS = 0
BEGIN
--
-- Do Inserts , Updates Or Deletes Here...
-- For Demonstration purposes I will simply do a SELECT
SELECT @ID, @Name,@Email;
--
FETCH NEXT FROM cusor_XMLCursor INTO
@ID, @Name, @Email;

END
--
CLOSE cusor_XMLCursor;
DEALLOCATE cusor_XMLCursor;

EXEC sp_xml_removedocument @docHandle;

RETURN;

END

Now we can test our stored procedure by using some real XML and see if it works properly:

DECLARE @RC int
DECLARE @xmlDocument xml

-- TODO: Set parameter values here.

SET @xmlDocument = N'<?xml version="1.0" standalone="yes"?>
<PEOPLE>
<PEOPLE>
<ID>1</ID>
<Name>Albert Einstein</Name>
<Email>aeinstein@amazingpeople.com</Email>
</PEOPLE>
<PEOPLE>
<ID>2</ID>
<Name>Salvador Dali</Name>
<Email>sdali@amazingpeople.com</Email>
</PEOPLE>
</PEOPLE>'
;


EXECUTE @RC = [Ricardo].[dbo].[SP_ProcessXML_People]
@xmlDocument


The result should be the following:

OK, we got this far but how to use it from within our application?
The only requirement is to call the stored procedure and pass a string with the XML content.
We could use Linq To XMl to create the required XML and pass the result to the stored procedure, but for compatibility with older versions of the .NET framework I'll just build an example based on the ADO.NET model, so here it goes in C#:


DataSet ds = new DataSet("PEOPLE");

DataTable dt = new DataTable("PEOPLE");

string _ID = "ID";
string _Name = "Name";
string _Email = "Email";

dt.Columns.Add(new DataColumn(_ID,Type.GetType("System.Int32")));
dt.Columns.Add(new DataColumn(_Name, Type.GetType("System.String")));
dt.Columns.Add(new DataColumn(_Email, Type.GetType("System.String")));

ds.Tables.Add(dt);

DataRow dr = dt.NewRow();

dr[_ID] = 1;
dr[_Name] = "Albert Einstein";
dr[_Email] = "aeinstein@amazingpeople.com";

dt.Rows.Add(dr);

dr = dt.NewRow();

dr[_ID] = 2;
dr[_Name] = "Salvador Dali";
dr[_Email] = "sdali@amazingpeople.com";

dt.Rows.Add(dr);

CallStoredProcedure_SP_ProcessXML_People(ds.GetXml());

(*) I have not posted the definition of the function because it is out of the scope of this article.

At the end we call the function CallStoredProcedure_SP_ProcessXML_People with the one argument which is the XML content as a string.

That's all it is needed...

Cheers

Wednesday, July 1, 2009

Cross functional teams, the key for IT Tailored Projects Success...

There are a lot of brand new technologies to develop new killer Web Enabled Information Systems covering different business models.

With the new strive on .NET and its features (LINQ, Silverligt, ASP.NET, WCF, SQL Server,RDLC Reporting), the information flow of business processes can be improved significantly like it never did before.

This will for certain impact Business and operations in the future, as we see the increase of the dependency on this new systems that will flourish gradually.

However for the projects development success, Cross-Functional teams really do matter to fill any existing GAP between Technology and Business/Operations Processes...

Knowledge is the key, but extensive knowledge of Operations and Technology might be just to overwhelming.

The development Team must be aware of the latest technologies that can be used, and the business/Operations/Process analysts/Managers must be aware of the Processes/Operations/Services being covered as well of a plan to make it more efficient.

The challenge is to make Technology team and Business team communicate and work together targeting the defined previously agreed improvement plan.

Translating this to your business, you would be surprised to see how operations can be improved when supported by a proper Information system tailored for your particular business.

Developers (Engineers and or Programmers) and business/Operations/Process analysts/Managers designing new platforms and taking it up to a new level...

The questions is if this knowledge exists in-house? If yes Great, if not outsource or build temporary team to support project development, but the costs might increase significantly...

Remember it takes only two persons to build such a team, a software Engineer/Developer and Operations/business/Process manager, both can deploy today what in the past had to be delivered by extensive teams.

Pretty much the same way it is needed a designer and a tailor to make the perfect suite, the same applies here...


Best Regards.

Tuesday, June 30, 2009

Silverlight and Moonlight, the .NET RIAS

For those who do not know, these are the names for the .NET implementation of Rich Internet Applications, but for the operating systems (Windows, Mac's and Linux respectively).

Silverlight is one of the recent goodies and it is spreading all over the internet as it enables a new and rich internet experiences to client browsers.

Basically it delivers the good looks as experienced before with flash objects, but with the power of .net framework in the background (Web Services, ASP.net), Yummy...

The Silverlight's stable version at the time of this article is 2 and Moonlight, its Linux brother is 1.

I confess, I have surrendered to this technologies since quite some sometime ago....

Nevertheless there is a gap between Silverlight and Moonlight, where as expected Moonlight features are a little bit behind in time then Silverlight's.

This becomes quite obvious as developers tend to use the latest versions of Silverlight to include all the new goodies in their gadgets, and who could blame them?

I have seen spread across the web several Silverlight implementations using the version 3 which his not release as stable yet.

However for a Linux user even after installing the stable version of Moonlight, it probably still not be able to experience most of the Silverlight gadgets out there...

In the mono website the Moonlight 2 will be released in next September, so a catch up can be expected, but again when Microsoft somewhere in the near future releases Silverlight 3 as stable the loop starts again...

But nevertheless I do congratulate the mono team for their efforts not only in Moonlight but in mono in general.

Cross-Platform applications, and in this particular case RIAS, just rule...

Over & out...

Monday, June 29, 2009

Linux Operating Systems

Two of my favourite Linux Operating Systems are Ubuntu and OpenSuse.

Ubuntu is very nice as it as available many software packages. The downside of Ubuntu are the predominately problems i have every time i upgrade the operating Systems (twice a year). Whenever this upgrades happen i have to set-up everything again from scratch as it starts to malfunction. This might be ok for an advanced user but it is certainly a reason to make some users to simply give up on it.

OpendSuse is very stable, it doesn´t have so many software packages available as Ubuntu but it rocks as it as always available the latest packages of mono. If you are .NET software developer, this should be definably your choice. Most of the new cool Apps coming out on Linux are using mono so I would guess that OpenSuse might be the best choice for present and future...

There is something i really appreciate in Linux OS's, you do not need an Anti-Virus, the list of virus created for Linux based system can be counted with our hand fingers and are all known and patched. If you really think about it, how cool it is not to have that performance killer Anti-Virus or Anti-Spyware and its annoying messages on your machine? Wouldn't be nice to just not needing to have it? With a Linux based software you can have that feeling...

Of course there are many others Linux OS's, feel free to try as many as you can, but me?

I have made up my mind, i like these ones better...

What about Windows you may ask?

It is still a keeper i would say... ;)

Wednesday, June 17, 2009

Lift Off

Hello Everyone!! =)

Welcome to my Blog and by the way my new online Home Page.
In this web Site i will post information about my development projects.
You are invited to check it out at:

http://matosdotnet.com/ (Requires Silverlight)


Feel free to drop me a comment...

Regards.
 
http://matosdotnet.com/