Repetitive Code

bugtracker911

New member
Joined
Aug 26, 2006
Messages
4
Programming Experience
Beginner
How can I prevent writing repetitive code like connecting to the database, quering, inserting data etc. All examples I have found keep repeating the connection string to open the database, which makes your code messy.

What I would like is a wrapper or something, to keep all the db functions together, and to make sure that all open connections are closed properly after querying. Nothing too complicated, I could try and knock something up myself, but I'm new to .net, and I want to get it done right.

Any help appreciated

Hope this makes sense.
 
Hi

Probably the best way would be to create a Data Access Layer that will handle all of your database operations for you. For example, you might create a class file that communicates with SQL Server and performs all operations such as executing a stored procedure or returning data in the form of a DataSet/DataTable or even a DataRow. This class would then be the only object in your application that would communicate with the database and any other area of your application that requires data would get it via this class.

The beauty of this approach is that if it is done right then you can change your back end database without interfering with the rest of your code.

For more information on this you should take a look at the Microsoft Patterns and Practices site at http://msdn.microsoft.com/practices/

You might also want to look into Microsoft's Application Blocks which are a good source of re-usable code when creating an architecture for your applications. This can be found here: http://msdn.microsoft.com/practices/guidetype/AppBlocks/


HTH
 
Back
Top