C#: Microsoft Enterprise Library: Data Access
By: Zack Turnbow
Introduction
Most developers, as they continue to produce software products, start to build libraries of reusable code. Much of it is the mundane tasks that are associated with standard software development.
That’s why Microsoft started sharing some of their proven code with the development community back in 2005. Since that initial release there have been several versions that have continued keeping up with new technologies and providing a much quicker development time with the most current version being 4.0 that was released on May of this year.
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
This series will delve into common uses for each of these “blocks” of code. For this series, version 3.1 will be used along with SQL 2005 Express with the AdventureWorks database.
Implementation
Start a new project, it can be a Windows or web application. Go ahead and add a reference to the data access application block.
Repeat the step to add a reference to the shared Common and Configuration dll as well. They will come into play later in the article.
On the designer for the Default.aspx drag a GridView control onto the work area. Before we jump right into the DAAB, let’s take a look at what would usually be done when accessing data from the database.
// Create a connection to the datbase
string conn = "Server=(local);Database=AdventureWorks;IntegratedSecurity=True;";
// Create the query to be executed
string query = "select * from Production.Product where ProductID = 1";
// Create the connection object then open the connection
SqlConnection sqlConn = new SqlConnection(conn);
sqlConn.Open();
// Create the command object then set the appropriate commamd type
SqlCommand sqlCmd = new SqlCommand(query, sqlConn);
sqlCmd.CommandType = CommandType.Text;
// Set the data source of the grid to the the ExecuteReader method
GridView1.DataSource = sqlCmd.ExecuteReader();
GridView1.DataBind();
// Make sure to close the connection
objConn.Close();
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
Now to achieve the same results using the DAAB, so first open up the web.config and add some connection information. I should look something like this:
<connectionStrings>
<add name="Production" connectionString="Server=zturnbow\sqlexpress;Database=AdventureWorks;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Go back to the code behind and add the following using statements:
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common;
Since everything is setup now, let’s write some code. The first thing is to create a database reference. It will need to be placed after the partial class declaration in the code behind. It will look like this:
Add code to create the SQL query string and access the database to retrieve the data and bind it to the grid.
// Create the SQL query
string strSql = "select * from Production.Product where ProductID = 1";
// Set the grid data source to the SQL helper class ExecuteReader method by supplying
// the proper information
GridView1.DataSource = db.ExecuteReader(CommandType.Text, strSql);
GridView1.DataBind();
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
It is simple to see that it requires less typing to get the same results. Many people think there is not much benefit but when doing development, the more typing involved, the more likely there are going to be mistakes.
The more mistakes (bugs), the more it takes time to finish a project.
What have we learned?
How to add a reference to a dll in a project.
How use the web.config file to hold database connection strings.
How to use the Enterprise Library Configuration Application Block to configure a database connection from the web.config
How to use the Enterprise Library Data Access Application Block to retrieve data from a database.
Attachments



Leave a Comment
#1 Pasupathi
Posted By: Pasupathi | 10.21.2008 at 5:19 PM
Thanks Man, I ma using the Enterprise Library first time. It is Very Useful for me.
#2 koko
Posted By: koko | 1.11.2009 at 7:38 AM
connection between c# and microsoft access
#3 International Direct Deposit
Posted By: International Direct Deposit | 12.07.2009 at 4:54 AM
Thanks for sharing lots of information. This is pretty useful for many students as well as professionals.
#4 free breakout games
Posted By: free breakout games | 3.07.2010 at 3:38 AM
Brilliant blog post.I have bookmarked it already. Greets, Taj.
#5 350-001
Posted By: 350-001 | 3.19.2010 at 4:35 AM
Great code, I hope, that it will help my in my project, but any way, you have done great work.
#6 installment loans no credit check
Posted By: installment loans no credit check | 4.02.2010 at 10:39 AM
I have been searching for this information almost two hours and finally I have found this your article. This entry is really useful for me and I think that and for other people. Reading this post I have known many new things about the development which I have not known before. Well, I will definitely read and other articles related with this subject. Thanks a lot for the informative entry and keep up publishing these great posts in the future. Regards.
#7 testking HP0-S21
Posted By: testking HP0-S21 | 5.11.2010 at 1:20 AM
I like your blog very much.
#8 t shirt pritning
Posted By: t shirt pritning | 5.13.2010 at 8:13 AM
Something that i have usually steered away from, but i am now learning through work
#9 laptop adapter manufacturer
Posted By: laptop adapter manufacturer | 5.21.2010 at 1:15 AM
release there have been several versions that have continued keeping up with new technologies and providing a much quicker
#10 coach handbags
Posted By: coach handbags | 5.24.2010 at 3:19 AM
The Far East aristocrat becomes fashion new influence … “the Russian always to choose best goods - - Chanel in the best brand the handbag and the coat,
#11 Polo shirt printing
Posted By: Polo shirt printing | 6.09.2010 at 8:26 AM
That is a really useful resource, thanks very much!
#12 coach outlet
Posted By: coach outlet | 6.11.2010 at 4:19 AM
Very good, look forward to view your other articles.
#13 michael jordan shoes
Posted By: michael jordan shoes | 6.18.2010 at 8:55 PM
good post!!thank you
#14 tiffany co
Posted By: tiffany co | 7.21.2010 at 2:38 AM
many people choose Tiffany & Co brand.Now For classic and quality genuine silver jewelry, we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/
#15 tiffany jewelry
Posted By: tiffany jewelry | 7.21.2010 at 2:38 AM
many people choose Tiffany & Co brand.Now For classic and quality genuine silver jewelry, we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/
#16 tiffany jewellery
Posted By: tiffany jewellery | 7.21.2010 at 2:38 AM
many people choose Tiffany & Co brand.Now For classic and quality genuine silver jewelry, we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/
#17 congcong
Posted By: congcong | 7.21.2010 at 3:26 AM
[url=http://www.luv-replica.com/GoodsSeries/Cartier-Ballon-Bleu-De-Cartier-31.html]Replica Cartier Ballon Bleu Watches[/url]
[url=http://www.luv-replica.com/GoodsSeries/Cartier-Must-21-Series-36.html]Replica Cartier 21 Must De Cartier Watches[/url]
[url=http://www.luv-replica.com/GoodsSeries/Cartier-La-Dona-De-Cartier-30.html]Replica Cartier La Dona Watches[/url]
[url=http://www.luv-replica.com/GoodsSeries/Cartier-Tank-Style-28.html]Replica Cartier Tank Watches[/url]
[url=http://www.luv-replica.com/GoodsSeries/Cartier-Pasha-Series-32.html]Replica Cartier Pasha Watches[/url]
[url=http://www.luv-replica.com/GoodsSeries/Cartier-Classic-Santos-Series-33.html]Replica Cartier Santos Watches[/url]
#18 tiffany ring
Posted By: tiffany ring | 7.26.2010 at 6:51 AM
Tiffany On Sale Offer a wide range of fashionable Tiffany & Co silver jewelry, including Necklaces, Pendants, Bracelets, Earrings, Rings and Accessories with fantastic discount.more http://www.tiffanyonsale.com/
#19 tiffany bracelet
Posted By: tiffany bracelet | 7.26.2010 at 6:51 AM
Tiffany On Sale Offer a wide range of fashionable Tiffany & Co silver jewelry, including Necklaces, Pendants, Bracelets, Earrings, Rings and Accessories with fantastic discount.more http://www.tiffanyonsale.com/
#20 tiffany necklace
Posted By: tiffany necklace | 7.26.2010 at 6:51 AM
Tiffany On Sale Offer a wide range of fashionable Tiffany & Co silver jewelry, including Necklaces, Pendants, Bracelets, Earrings, Rings and Accessories with fantastic discount.more http://www.tiffanyonsale.com/
#21 men gucci shoes
Posted By: men gucci shoes | 7.28.2010 at 10:38 PM
How to implement a Wrap Exception Policy.
#22 gucci bags
Posted By: gucci bags | 7.29.2010 at 10:32 PM
thank you
#23 cheap ugg boot store
Posted By: cheap ugg boot store | 8.06.2010 at 1:42 AM
To appreciate the beauty of life one can relish the works of artists and writers of renaissance period. Be it Da Vinci with the ethereal Mona Lisa, Rembrandt or Monet with classic cardy Water Lillies brought out the essence of life. www.uggsbailey.co.uk/.../sitemap.html clz
#24 Abercrombie and fitch
Posted By: Abercrombie and fitch | 8.10.2010 at 2:17 AM
I agree with your point of view, thank you for your article!
#25 jone
Posted By: jone | 8.13.2010 at 1:39 AM
dalarence04.blogcenter.de http://pals.fnm0.com/deayne/ lachary.mysportspress.com
#26 tiffany charm bracelets
Posted By: tiffany charm bracelets | 8.15.2010 at 8:59 PM
I hope this information would help me in my future programming, I should mention, your resource is very useful for me. I was searching for smth like this for a long time
#27 cooler bag manufacturer
Posted By: cooler bag manufacturer | 8.18.2010 at 10:13 PM
ven code with the development community back in 2005. Since that initial release there have been several versions that have continued keeping up with new technologies and providing a much quicker developmen
#28 brian atwood maniac pump
Posted By: brian atwood maniac pump | 8.20.2010 at 3:46 AM
If you don’t know about Brian Atwood,You should! Welcome to shop brian atwood shoes at http://www.brian-atwood.com BRIAN ATWOOD High-heeled sandals Purple(at www.brian-atwood.com/.../brian-atwood-hi ) is in stock in our store only one size in 36.5 EU. Elegant purple color, you will like it.
Back by popular demand... brian atwood maniac pump at www.brian-atwood.com/.../brian-atwood-ma the perfect wear-with-everything color. These wardrobe staples have 5" stiletto heels and hidden 1 1/2" seamless platforms. In a beautiful, very neutral nude tan.
These Brian Atwood suede 'Loca' studded pvc detail pumps at www.brian-atwood.com/.../brian-atwood-lo are so ravishing, that I'd totally be willing to pop an aspirin before wearing them, in order to dull the pending pain that the height of the heel will surely cause within one hour of rocking them. Take stye note of the unusual shape of the grommets and the dusty rose shade. Loves it!
Browse the variety of Christian Louboutin shoes at www.brian-atwood.com/.../christian-loubo that at wholesale price but with high quality, you can find your favorite boots, Pumps and Sandals here.
#29 brian atwood maniac pump
Posted By: brian atwood maniac pump | 8.20.2010 at 3:46 AM
If you don’t know about Brian Atwood,You should! Welcome to shop brian atwood shoes at http://www.brian-atwood.com BRIAN ATWOOD High-heeled sandals Purple(at www.brian-atwood.com/.../brian-atwood-hi ) is in stock in our store only one size in 36.5 EU. Elegant purple color, you will like it.
Back by popular demand... brian atwood maniac pump at www.brian-atwood.com/.../brian-atwood-ma the perfect wear-with-everything color. These wardrobe staples have 5" stiletto heels and hidden 1 1/2" seamless platforms. In a beautiful, very neutral nude tan.
These Brian Atwood suede 'Loca' studded pvc detail pumps at www.brian-atwood.com/.../brian-atwood-lo are so ravishing, that I'd totally be willing to pop an aspirin before wearing them, in order to dull the pending pain that the height of the heel will surely cause within one hour of rocking them. Take stye note of the unusual shape of the grommets and the dusty rose shade. Loves it!
Browse the variety of Christian Louboutin shoes at www.brian-atwood.com/.../christian-loubo that at wholesale price but with high quality, you can find your favorite boots, Pumps and Sandals here.
#30 brian atwood loca studded pumps
Posted By: brian atwood loca studded pumps | 8.20.2010 at 3:49 AM
Wonderful brian atwood shoes at www.brianatwoodcom.com/.../brian-atwood-br for your style.
BRIAN ATWOOD High-heeled sandals Purple(at www.brianatwoodcom.com/.../brian-atwood-hi ) is in stock in our store only one size in 36.5 EU. Elegant purple color, you will like it.
When I original saw these brian atwood maniac pump at www.brianatwoodcom.com/.../brian-atwood-ma the perfect wear-with-everything color. These wardrobe staples have 5" stiletto heels and hidden 1 1/2" seamless platforms. In a beautiful, very neutral nude tan.
These Brian Atwood suede 'Loca' studded pvc detail pumps at www.brianatwoodcom.com/.../loca-studded-pu are so ravishing, that I'd totally be willing to pop an aspirin before wearing them, in order to dull the pending pain that the height of the heel will surely cause within one hour of rocking them. Take stye note of the unusual shape of the grommets and the dusty rose shade. Loves it! www.brianatwoodcom.com/.../christian-loubo is a online website for Christian Louboutin shoes with with 70% off and original quality and free shipping.
#31 brian atwood loca studded pumps
Posted By: brian atwood loca studded pumps | 8.20.2010 at 3:49 AM
Wonderful brian atwood shoes at www.brianatwoodcom.com/.../brian-atwood-br for your style.
BRIAN ATWOOD High-heeled sandals Purple(at www.brianatwoodcom.com/.../brian-atwood-hi ) is in stock in our store only one size in 36.5 EU. Elegant purple color, you will like it.
When I original saw these brian atwood maniac pump at www.brianatwoodcom.com/.../brian-atwood-ma the perfect wear-with-everything color. These wardrobe staples have 5" stiletto heels and hidden 1 1/2" seamless platforms. In a beautiful, very neutral nude tan.
These Brian Atwood suede 'Loca' studded pvc detail pumps at www.brianatwoodcom.com/.../loca-studded-pu are so ravishing, that I'd totally be willing to pop an aspirin before wearing them, in order to dull the pending pain that the height of the heel will surely cause within one hour of rocking them. Take stye note of the unusual shape of the grommets and the dusty rose shade. Loves it! www.brianatwoodcom.com/.../christian-loubo is a online website for Christian Louboutin shoes with with 70% off and original quality and free shipping.
#32 prada bag
Posted By: prada bag | 8.20.2010 at 9:30 PM
e? I <a href=http://www.armani-watch.org/categories/armani-clothing-34-b0.html>armani mens clothing</a> thought. Then I knew: I <a href=http://www.pradabag.org/prada-briefcases/prada-briefcases-48-b0.html>prada briefcases</a> am in danger; I must <a href=http://www.louisvuittonreplica.net/fake-Louis-Vuitton-Speedy-25-123-b0.html>louis vuitton speedy bag</a> lock the door fast! Through <a href=http://www.louisvuittonreplica.net/fake-Louis-Vuitton-Speedy-30-121-b0.html>louis vuitton speedy 30</a> instinct and adrenaline, I jumped <a href=http://www.louisvuittonreplica.net/fake-Louis-Vuitton-Umbrella-44-b0.html>vintage louis vuitton umbrella</a> up quickly and locked the <a href=http://www.armani-watch.org/categories/armani-shirt-16-b0.html>armani shirts for me</a> front door,
#33 air max shoes
Posted By: air max shoes | 8.23.2010 at 2:23 AM
Once we get there, so many wonderful dreams will come true and the pieces of our black nike air max shoes lives will fit together like a completed jigsaw puzzle. http://www.sellnikeairmax.com/
#34 lv wallets
Posted By: lv wallets | 8.23.2010 at 11:06 AM
I hope this information would help me in my future programming, I should mention, your resource is very useful for me. I was searching for smth like this for a long time
#35 CHRISTIAN LOUBOUTIN Straratata 140 suede sandals
Posted By: CHRISTIAN LOUBOUTIN Straratata 140 suede sandals | 8.25.2010 at 3:02 AM
www.mallheels.com/.../christian-loubo CHRISTIAN LOUBOUTIN Straratata 140 suede sandals
Heel measures approximately 140mm / 5.5 inches with a 30mm / 1 inch platform. Color block your way to party perfection with Christian Louboutin's multicolored suede platform sandals. Contrast these strappy heels with a vibrant dress for a kaleidoscopic cocktail look. Look great with tights.
#36 Christian Louboutin Gres Mule 70 sandals
Posted By: Christian Louboutin Gres Mule 70 sandals | 8.25.2010 at 3:04 AM
www.christianlouboutin.com.hk/.../christian-loubo Christian Louboutin Gres Mule 70 sandals with a knot embellishment and delicate ankle strap. With a comfortable height, these dainty heels are the perfect choice for the dancefloor. Look great with tights.
#37 air max shoes
Posted By: air max shoes | 8.25.2010 at 10:35 PM
Once we get there, so many wonderful dreams will come true and the pieces of our black nike air max shoes lives will fit together like a completed jigsaw puzzle. http://www.sellnikeairmax.com/
#38 Ugg
Posted By: Ugg | 8.26.2010 at 6:38 AM
Got any reason to say no to cheap UGG boots? UGG boots that prevailed for years will still warm your frozen toes with the featured sheepskin leather,get more http://www.uggbootuksale.com/
#39 louis vuitton speedy 30
Posted By: louis vuitton speedy 30 | 9.02.2010 at 10:44 AM
louis vuitton speedy 30 www.louisvuitton4love.com/.../louis-vuitton-s
#40 louis vuitton thomas
Posted By: louis vuitton thomas | 9.02.2010 at 10:45 AM
louis vuitton thomas www.louisvuittonhouse.com/.../louis-vuitton-t