Click here to Skip to main content
15,887,318 members
Articles / Programming Languages / C#

ADO.NET for the Object-Oriented Programmer – Part Two

Rate me:
Please Sign up or sign in to vote.
4.96/5 (61 votes)
19 Jan 2006CPOL28 min read 337.5K   3.5K   240  
In this second part of the article series, we will revisit the ADO.NET CRUD operations. Only this time, we will use an application with far better architecture. Once that is done, we will move on to data binding with objects.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;
using ObjectDataBinding.Model;

namespace ObjectDataBinding.DataAccess
{
    class AuthorListDAO
    {
    	#region Declarations

        #endregion

    	#region Constructor

        public AuthorListDAO()
        {
        }

    	#endregion

    	#region Methods

        public void LoadAuthorList(AuthorList authorList)
        {
            // Build query to get authors and their books
            StringBuilder sqlQuery = new StringBuilder();
            sqlQuery.Append("Select AuthorID, LastName, FirstName, SSNumber From Authors; ");
            sqlQuery.Append("Select BookID, SkuNumber, AuthorID, Title, Price From Books");

            // Get a data set from the query
            DataSet dataSet = DataProvider.GetDataSet(sqlQuery.ToString());

            // Create variables for data set tables
            DataTable authorsTable = dataSet.Tables[0];
            DataTable booksTable = dataSet.Tables[1];

            // Create a data relation from Authors (parent table) to Books (child table)
            DataColumn parentColumn = authorsTable.Columns["AuthorID"];
            DataColumn childColumn = booksTable.Columns["AuthorID"];
            DataRelation authorsToBooks = new DataRelation("AuthorsToBooks", parentColumn, childColumn);
            dataSet.Relations.Add(authorsToBooks);

            // Load our AuthorList from the data set
            AuthorItem nextAuthor = null;
            BookItem nextBook = null;
            foreach (DataRow parentRow in authorsTable.Rows)
            {
                // Create a new author
                bool dontCreateDatabaseRecord = false;
                nextAuthor = new AuthorItem(dontCreateDatabaseRecord);

                // Fill in author properties
                nextAuthor.ID = Convert.ToInt32(parentRow["AuthorID"]);
                nextAuthor.FirstName = parentRow["FirstName"].ToString();
                nextAuthor.LastName = parentRow["LastName"].ToString();
                nextAuthor.LastName = parentRow["LastName"].ToString();
                nextAuthor.SSNumber = parentRow["SSNumber"].ToString();

                // Get author's books
                DataRow[] childRows = parentRow.GetChildRows(authorsToBooks);

                // Create BookItem object for each of the authors books
                foreach (DataRow childRow in childRows)
                {
                    // Create a new book
                    nextBook = new BookItem();

                    // Fill in book's properties
                    nextBook.ID = Convert.ToInt32(childRow["BookID"]);
                    nextBook.SkuNumber = childRow["SkuNumber"].ToString();
                    nextBook.Title = childRow["Title"].ToString();
                    nextBook.Price = Convert.ToDecimal(childRow["Price"]);

                    // Add the book to the author
                    nextAuthor.Books.Add(nextBook);
                }

                // Add the author to the author list
                authorList.Add(nextAuthor);
            }

            // Dispose of the data set
            dataSet.Dispose();
        }

    	#endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Foresight Systems
United States United States
David Veeneman is a financial planner and software developer. He is the author of "The Fortune in Your Future" (McGraw-Hill 1998). His company, Foresight Systems, develops planning and financial software.

Comments and Discussions