Martin Heller
Contributing Writer

Creating and Using a Data Access Layer with Visual Studio 2005

analysis
Oct 18, 20072 mins

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: Create a Class Library project in your solution in the supported language of your choice. I like C#, but Visual Basic .NET also works.

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.

Martin Heller

Martin Heller is a contributing writer at InfoWorld. Formerly a web and Windows programming consultant, he developed databases, software, and websites from his office in Andover, Massachusetts, from 1986 to 2010. From 2010 to August of 2012, Martin was vice president of technology and education at Alpha Software. From March 2013 to January 2014, he was chairman of Tubifi, maker of a cloud-based video editor, having previously served as CEO.

Martin is the author or co-author of nearly a dozen PC software packages and half a dozen Web applications. He is also the author of several books on Windows programming. As a consultant, Martin has worked with companies of all sizes to design, develop, improve, and/or debug Windows, web, and database applications, and has performed strategic business consulting for high-tech corporations ranging from tiny to Fortune 100 and from local to multinational.

Martin’s specialties include programming languages C++, Python, C#, JavaScript, and SQL, and databases PostgreSQL, MySQL, Microsoft SQL Server, Oracle Database, Google Cloud Spanner, CockroachDB, MongoDB, Cassandra, and Couchbase. He writes about software development, data management, analytics, AI, and machine learning, contributing technology analyses, explainers, how-to articles, and hands-on reviews of software development tools, data platforms, AI models, machine learning libraries, and much more.

More from this author