Free Newsletters

   All InfoWorld Newsletters
Strategic Developer | Martin Heller » Creating and Using a Data Access Layer with Visual Studio 2005

October 18, 2007 | Comments: (0)

Creating and Using a Data Access Layer with Visual Studio 2005

Every time I do this I have to figure it out again. (Sometimes I empathize with HAL, when he is being dismantled at the end of 2001: "My mind is going, Dave. I can feel it.") I'm blogging this as much for myself as for anyone else it might help.

Here is the magic spell you need:

  1. Create a Class Library project in your solution in the supported language of your choice. I like C#, but Visual Basic .NET also works. I usually end the project name with DAL so that I can remember what it is.
  2. Delete the Class1.cs (or .vb) source file that is created automatically.
  3. Right-click on the project and add a new DataSet item. The design surface for an XSD file will open.
  4. Open a data connection to the database you're wrapping.
  5. Select each table you want to use and drag it to the design surface.
  6. Build the data access layer project.
  7. Add a reference to the data access layer project to your UI project. It doesn't really matter whether the UI project is a Windows Forms application, an ASP.NET application, a console application, or a Windows Presentation Foundation application.
  8. Add a using (or Imports) statement for the table adapters of the data access layer to your UI project. IntelliSense will help out if you added the reference properly.

Now you can instantiate the table adapters in your code and call the appropriate pre-generated methods for whatever CRUD operations you need. If you need other operations, you can go back to the DAL and add them: right click at the bottom of the table in the DataSet diagram and add a query.

Posted by Martin Heller on October 18, 2007 08:06 AM


RATE THIS ARTICLE:





 

  •  
  • COMMENTS





Technology White Papers

 

InfoWorld Technology Marketplace

» Technology White Papers Library

Technology White Papers by Topic

Technology White Papers E-mail Alert

Find out when the latest white paper is available:
 
 
» BUY A LINK NOW

Sponsored Technology Links