Click here to Skip to main content
Email Password   helpLost your password?

Introduction

Summary:

Microsoft has developed and released Application Blocks for Data Access and Exception Management for using in .NET applications. These application blocks provide the .NET developer not only the ready to use code inside the application, but also the code which was built by encapsulating the best practices by Microsoft.

In this discussion we are going to examine the following:

Advantages of Data Access Application Block

Microsoft has developed this wrapper on top of the ADO.NET layer by encapsulating the performance and memory management related best practices for Microsoft SQL Server. So we need not worry about the data access performance and memory leaks.

This comes as a single assembly with a class consisting of useful methods which serve all kinds data access requirements. By using these simple calls in our code, we can reduce the amount of custom code we actually write in our conventional applications. Moreover we can also skip the over head of testing and maintenance of our custom code.

Microsoft provides the source code files, along with the Quick start samples and documentation. We can use the source code as it is or we can even customize as per our application needs.

Installing the Data Access Application Block

This is a free software and you can download it from here.

The current version of this block is 2.0. And we need to have Visual Studio. NET installed on the target machine.

Your installation should create an entry in the Start > Programs > menu called �Microsoft Application Blocks for .NET�, where it will show links to source code and samples.

Sample screenshot

Fig 1: After installing the Application Block.

Select the Data Access Application Block option by navigating to the path shown in the figure. It then opens the solution in the VS.NET IDE.

Sample screenshot

Fig 2: Open the Data Access Application Block solution in the IDE.

Click on the SQLHelper.cs file to look inside the code. It creates a namespace with the name Microsoft.ApplicationBlocks.Data with two sealed classes SqlHelper and SqlHelperParameterCache created inside that.

Now right click on the project name and click Build. Now you should see the assembly created at the following path:

C:\Program Files\Microsoft Application Blocks for .NET\Data Access v2\Code\CS\Microsoft.ApplicationBlocks.Data\bin\Debug

So we have built the Data Access block assembly, now we create a Web Application in which we refer this assembly. In our web application, we use a simple web form to connect to SQL Server 2000 and access the data from the database.

Create a sample .NET application using Data Access Block.

Step 1: Select File > New > Project from the file menu, select the ASP.NET Web Application and enter the project name and path accordingly.

Sample screenshot

Fig 3: Creating a sample web application.

Step 2: Now we add the Data Access Block to our project. To do this, right click on the references node and click Add Reference. Click on the Browse and select the Microsoft.ApplicationBlocks.Data.dll and click Ok button.

Sample screenshot

Fig 4: Adding the Data Access Block reference.

Step 3: Open the WebForm1 and drag a datagrid from the toolbox onto it.

Sample screenshot

Fig 5: Adding the datagrid to the WebForm1.

Step 4: Add the line for using the Data Access Block assembly.

Sample screenshot

Fig 6: Using Microsoft.ApplicationBlocks.Data namespace.

Step 5: Add the following code in the Page_Load event procedure.

This code creates a connection to the SQL Server database.

We have used the ExecuteDataset static method of the SqlHelper class to fetch the top 10 records from Employees table.

Sample screenshot

Fig 7: Call SqlHelper.ExecuteDataset method

In the above code, I have used my own credentials for connecting SQL Server. Please change those accordingly.

Step 6: Run the WebForm1.aspx page in the browser to see the output.

Sample screenshot

Fig 8: Result displayed in WebForm1.aspx

Conclusion

In this discussion I have tried my best in exploring the Data Access Block with a simple example. Try to explore other useful methods available in SqlHelper which serve for variety of data access purposes. For wide information on this topic, visit microsoft.com.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalfor Oracle ???
alhambra-eidos
8:30 20 Oct '09  
any solution ???

thankss

AE

GeneralArrgggghhhhhhhh
Pete O'Hanlon
0:58 5 Nov '07  
No. No. No. No. Never ever ever use direct SQL statements like this in production code. You leave yourself wide open to SQL Injection Attacks. As an article author, it is behoven of you to actually promote good practice - something that you have singularly failed to do here.

Deja View - the feeling that you've seen this post before.

GeneralHi
mezzo
0:28 5 Nov '07  
Hi Sekhar,

Thank you for your reply about application blocks in http://www.thescripts.com/forum/showthread.php?p=2918725#post2918725

I would like you to send me your sample applications, it would be a great help if you also have a login screen so i can start from there Smile

Thanks and Regards

Monther
USM - CS School
m_enayah@hotmail.com
GeneralHi
Ramesh Duppati
21:49 7 Sep '06  
Ite very nice

D.Ramesh

GeneralThis is terrible
marcroach
9:59 27 Mar '06  
This is a regurgitated example of the ApplicationBlocks.Data. At least be a little creative.
GeneralPuur
Anonymous
3:33 7 Oct '05  
Dis is puur. Mad I wos disepointid. ConfusedCry I wont to no data blok beter. You tell me no guud how uz it. Unsure Confused
GeneralRe: Puur
MCDBA
3:34 7 Oct '05  
LOL Check your grammer, newb.
GeneralBuild Error
norm
22:44 21 Jul '05  
I just downloaded Enterprise Library Application Blocks from MSDN, the latest version June 2005 (... here ...). I open the solution for "Data Access Application Block \ Data Design" and tried to compile it:

The referenced component 'Caching.Configuration.Design' could not be found.
The referenced component 'Caching.Cryptography.Design' could not be found.
The referenced component 'Caching.Database.Configuration.Design' could not be found.
The referenced component 'ExceptionHandling.Logging.Configuration.Design' could not be found.
The referenced component 'Logging.Configuration.Design' could not be found.
The referenced component 'Logging.Sinks.Database.Configuration.Design' could not be found.
The referenced component 'Security.ActiveDirectory.Configuration.Design' could not be found.
The referenced component 'Security.Cache.CachingStore.Configuration.Design' could not be found.
The referenced component 'Security.Configuration.Design' could not be found.
The referenced component 'Security.Cryptography.Configuration.Design' could not be found.
The referenced component 'Security.Database.Authentication.Configuration.Design' could not be found.
The referenced component 'Security.Database.Configuration.Design' could not be found.


What should I do next? Same with "Data Access Application Block \ Data". Thanks in advance.

Norman Fung (c)
GeneralArticle
Jonathan Bays
18:11 8 Nov '04  
The article was also only useful for C# programmers. Although the method for creating application blocks for data access aren't very different in VB.NET, it would have helped had you mentioned this as the beginning of the article. Beginners may not understand why your screen shot of the code doesn't match their view if they are coding in VB.NET.
GeneralRe: Article
kiran1331
10:02 23 Mar '06  
This is very sickD'Oh!
GeneralVery misleading
DiBrun
23:55 15 Sep '04  
I read CodeProject regularly. Its the absolute best place for good .NET insight and code examples etc.
When I read your title my expectations were (great, a quick intro / overview of the benefits of the Microsoft Application Block for Data Access.

However, substitute 2 of your above code lines with the following 2 standard ADO.NET code and you get the same result:


DataSet dsEmployees = new DataSet();
new SqlDataAdapter(sqlStatement,sqlConnection).Fill( dsEmployees );


So now one would be left with the impression that there is no benefit in using the Data Access Application Block.
However, I'm quite sure that since Microsoft has invested time in making the Block, it must offer some improvements.

GeneralRe: Very misleading
fadee
1:20 16 Sep '04  
Well being very tough on critisim, the title of the document is very catchy: but the contents, frankly not impressive. I opened the article while considering it to be good analysis but...

But still, it is excellent for beginners.

eff_kay

-------------------
Therez No Place like ... 127.0.0.1
Generalyup, good job
norm
22:19 21 Jul '05  
fadee wrote:

But still, it is excellent for beginners.


Exactly...

Norman Fung
GeneralRe: Very misleading
Anonymous
11:03 23 May '05  
Surely the two lines of code can give the same result. But the whole purpose of Application block is to use it as a business object. When it comes to using stored procs this app block is a gr8 use.
General1. With/Without App Block + 2. NHibernate
norm
22:22 21 Jul '05  
Show me (please please please) - compare data access "with" and "without" application block.

I also would be very in how it relates to Object Relational mapper like NHibernate or Object Space

Norman Fung
GeneralRe: Very misleading
eliakaris
15:40 1 Nov '05  
I Agree completely.
This is not what Microsoft means when they say the Data Access Block. This is Sql Server specific... This is only for people who know they will always use sql server. The block's power comes in the fact that you can easily change the database with no code change.

this article does not demonstrate that.
GeneralRe: Very misleading
Ghayas Ur Rehman
8:12 16 Feb '09  
This data access block contains all methods static, this is not good for distributed application envoirnment or asp.net.

give me your feedback at mr.ghayas@gmail.com

Ghayas Ur Rehman
Masters of Computer Science

GeneralDemo project ?!?
Colin Angus Mackay
6:05 6 Sep '04  
What demo project - It is a 1Mb word document.

You have received some good comments on improving the quality of this article. It would make the article much more useful if you followed them.

1. Screenshots cropped to the relevant area - and not squashed up. We want to actually see what is in them, not line the pockets of our optitians.
2. Text code snippets rather than screenshots.
3. Making the demo project a zip of downloadable source code
4. Mark the article as for Beginners.

I really like the tutorial style you've taken, and with a little work this could be a most excellent article.


"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

Not getting the response you want from a question asked in an online forum: How to Ask Questions the Smart Way!


GeneralNot good
Alsius
5:26 6 Sep '04  

1. Screenshots are too big.
Most people do not need whole VS.NET IDE or Windows XP on screenshots. It is a good practice to publish screnshots relative to the application only.

2. Lack of features information
I don't see any info about comparsion of Data Access Application Block (DAAB) with other ways to access database. if this article for beginners than such info should be here, overwise article is too simple
GeneralRe: Not good
Sekhar Lanka
21:30 6 Sep '04  
Thanks for your feedback.
My intension was to give a feel of Data Access Block, so i didnot talked about other ways of accessing data.
I would try to put text screenshots.

Thanks
Sekhar Lanka.
Smile

GeneralModification
m a y s a m
23:30 28 Aug '04  
It would be much better if you changed the sample code from picture to text.
GeneralRe: Modification
fabulous
8:52 29 Aug '04  
True, I cannot see anything in the source code. Text is best for source code.

Let's make things simpler than possible.


Last Updated 6 Sep 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010