19. April 2007 09:37
I bet alot of you are sitting at your desk today, coding away on your MOSS Webpart, and thinking to yourself "Geez, I really hate coding all this data access stuff, isn't there a better way!?"
Yes! The Enterprise Library 3.0 - April 2007 release.
So you need to grab some sql data and shove it in a dataset huh?
myDataset = CreateDatabase("DBName").ExecuteDataSet("SPName", params);
Seriously, thats it! One line! Don't even create a connection!
It works with Oracle too... Of course you can do about a million equally amazing things with the Enterprise Library, so why not use it with your SharePoint development starting right now?
So you just want to make a connection to a database (an external database, NOT your SharePoint database!). Open the configuration tool, load your sharepoint web.config, and add your connection string.
Add two references to your web part project:
But you can't just add these from anywhere... For code security reasons, Sharepoint is not going to let you use just any old DLL files, you've got to use Strong Named Assemblies. Now I know your thinking "Geez, I have to go add a key to all those projects and recompile" .... But the Standards and Practices team at Microsoft is one step ahead of you in this release. If you go to the installation directory for Enterprise Library, you'll find a batch of pre-compiled, pre-signed, dll files!
Make your life a little easier and add a using statement like this to your class:
using ENT = Microsoft.Practices.EnterpriseLibrary.Data;
Bet you didn't know you could assign an alias to a namespace? Well things inside EnterpriseLibrary.Data can get confused with System.Data, so it's best we alias the Enterprise Library with something short and sweet, like "ENT".
Ok, now have fun using things that begin with:
Where 'DBName' is the name you associated with your connection string using the configuration tool. Enterprise Library will go out, look it up, open the connection, and execute your commands, all the while cleaning up nicely after itself.
(Remember during deployment you'll need to take care of safe control entries and code access security for all the assemblies.)
There are piles of useful configuration, data, and logging tools inside the application blocks, take a look at the documentation and see what will work for you.