Click here to Skip to main content
15,887,267 members
Articles / Programming Languages / Visual Basic
Article

A Simple Data Manager Class

Rate me:
Please Sign up or sign in to vote.
1.40/5 (6 votes)
12 Nov 20072 min read 46.6K   434   16   14
A simple way to access your database using vb.net

Introduction

After developing many applications and websites I have always felt the need to create a class that gives me the power to program in as few lines as possible.

With that in mind, I became rather frustrated with the Microsoft database access classes. These classes are sold to us as "best practices", but they fail to abide by the best practices of coding that we all learned in programming 101.

If those helper classes are so "helpfull" then why do I still have define parameters before every sp call?

db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, 7)

If you're like me you must be sick of doing that over and over again. More then anything I really hate having to define all the parameter types every time I need to make a call to the database. What is even worse is that the microsoft data access classes make a call to

SqlCommandBuilder.DeriveParameters(cmd)

lol.. you must be kidding right? No I'm not! The code makes this call and yet it still requires you to define all your stored procedure parameter datatypes. I have the source code to the application block. Look for yourself if you have it. After that call the Microsoft code clones all the parameters and stuffs them into nasty array. Talk about over complicating everything and wasting memory. Clearly this is unacceptable code and should be droped from your application.

With these problems in mind, I went about creating a new class that does things my way. :)

The code I've posted here is basically an example of what an ideal database access class should look like.

Using the code

The code that I've uploaded is a simple class called DataManager. This class must be inherited from. It allows the derived class to make database calls in only three lines of code. It will handle all your connections, transactions, rollbacks, and dataset / datareader calls.

Public Class ExampleDAL

    Inherits DataManager

 

Public Sub ExampleSet(ByVal param1 As String, ByVal param2 As String, ByVal param3 As String)

    Me.Begin()

    Me.Exec("example1_set", param1, param2)  

    Me.Exec("example2_set", param3)

    Me.Commit()

End Sub

Public Function ExampleList() As Data.DataSet

    Me.Begin()

    Dim ds As Data.DataSet = Me.Exec("example1_list")

    Me.Commit()

    Return ds

End Function

 

End Class

Points of Interest

Usually, you will want to create classes for each of your application constructs. For example you might want to create a class called Accounts that derives from DataManager. This class will contain all your get, set, and list methods.

Please note that this is only an example of how to make a simple DataManager class. You will need to add more methods to execute datareaders, log functionality, and handle other situations for a real application.

The class only allows you execute stored procedures. This is all you should ever be doing anyway.

I've included inline debuging code that will help you see what calls are being made to the database in the debug window. I find this to be very helpfull since there are some situations where you do not have rights to use SQL Profiler on the database.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
00K
Web Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Dave Kreskowiak29-Aug-09 8:51
mveDave Kreskowiak29-Aug-09 8:51 
GeneralWTF is this all about. Pin
Chris Meech12-Nov-07 10:02
Chris Meech12-Nov-07 10:02 
GeneralRe: WTF is this all about. Pin
00K12-Nov-07 10:25
00K12-Nov-07 10:25 
GeneralRe: WTF is this all about. Pin
Chris Meech12-Nov-07 10:32
Chris Meech12-Nov-07 10:32 
GeneralRe: WTF is this all about. Pin
00K12-Nov-07 10:44
00K12-Nov-07 10:44 
GeneralRe: WTF is this all about. Pin
miies12-Nov-07 20:53
miies12-Nov-07 20:53 
GeneralRe: WTF is this all about. Pin
Thanks for all the fish13-Nov-07 4:54
Thanks for all the fish13-Nov-07 4:54 
GeneralRe: WTF is this all about. Pin
00K13-Nov-07 7:19
00K13-Nov-07 7:19 
GeneralRe: WTF is this all about. Pin
miies13-Nov-07 8:05
miies13-Nov-07 8:05 
GeneralRe: WTF is this all about. Pin
Azlan David15-Nov-07 7:29
Azlan David15-Nov-07 7:29 
Generalcontroversial... [modified] Pin
Thanks for all the fish12-Nov-07 9:16
Thanks for all the fish12-Nov-07 9:16 
GeneralRe: controversial... Pin
00K12-Nov-07 10:26
00K12-Nov-07 10:26 
GeneralRe: controversial... Pin
Thanks for all the fish12-Nov-07 10:34
Thanks for all the fish12-Nov-07 10:34 
GeneralRe: controversial... Pin
00K12-Nov-07 10:46
00K12-Nov-07 10:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.