Home > 2010 Winter BTI420 > Learning more about a Data Access Layer

Learning more about a Data Access Layer

This post covers more Data Access Layer (DAL) concepts, introduces DAL objects, and gets you started using them.

As suggested in today’s lecture notes introduction, it’s now time to learn more about a DAL, and how it enables you to modify the underlying (database table) data.

Look (again) at your dataset (DAL)

Assume that you have an existing dataset (DAL)…

Create a new web form, and then open its C# code-behind module. Inside the Page_Load method, begin typing the name of your dataset. Intellisense shows two items that begin with the dataset’s name:

<datasetname> – class – Represents a strongly typed in-memory cache of data

  • More about this soon…

<datasetname>TableAdapters – This is a “namespace” (class collection) of <datasetname>TableAdapter classes

  • Select this item, press dot, and then you can browse through the table and query adapters that you have created
  • Each one is a table adapter class – Represents the connection and commands used to retrieve and save data

You have learned that a table adapter has one or more methods. (These methods were created when you created and configured the table adapter.) Then, in the web form designer, for a GridView or DetailsView, you selected a method during the configuration of an ObjectDataSource object.

You should be curious enough to ask a couple of questions about a table adapter method:

  1. Does it take arguments?
  2. What is its return value?

Use Intellisense to help learn the answers

Let’s use Intellisense to help learn the answers to the questions in the previous section.

Create a new instance of a table adapter in code. We did this during a lecture a couple of weeks ago. To do this, you use syntax that’s similar to other instance-creation statements:

Classname instancename = new classname();

For our dataset, the classname is a two-part name: <datasetname>TableAdapters . <tableadaptername>

For example, in my code…

  • my dataset name is myNorthwind
  • the dataset has a table adapter that works with the nwCustomers table, named “Table_nwCustomers”

To create a new instance of this table adapter, the following syntax is used:

myNorthwindTableAdapters.Table_nwCustomers ta = new myNorthwindTableAdapters.Table_nwCustomers();

The “ta” instance can now be used in code. Let’s look at its methods, so we can answer the questions above. While there are many methods, let’s look only at two:

  • the table adapter has a GetData() method that returns all rows
  • it also has a GetDataByCountry() method, that takes a string “Country” argument (e.g. “USA”), and returns matching rows

The signature of the GetData() method is as follows:

myNorthwind.Table_nwCustomersDataTable Table_nwCustomers.GetData()

As you can see, its return type is a myNorthwind.Table_nwCustomersDataTable.

The signature of the GetDataByCountry() is as follows:

myNorthwind.Table_nwCustomersDataTable Table_nwCustomers.GetDataByCountry(string Country)

As you can see, it has the same return type. However, it needs an argument – a string called Country.

So, we have answered our questions. A table adapter method may or may not take arguments. The return type for these two “get data” methods is a data table of a specific type.


How did these methods get created?

You may remember that the third step of the TableAdapter Configuration Wizard shows you an “Advanced Options…” button. It showed you some advanced options, one of which was “Generate Insert, Update, and Delete statements”. By default, it was checked/selected.

Additionally, you may remember that the fourth step of the wizard had a checkbox for “Create methods to send updates to the database (GenerateDBDirectMethods)”. By default, it was checked/selected.

Well, leaving both in the checked/selected state brings some benefits. Specifically, the wizard generates Insert(), Update(), and Delete() methods. These methods can be used in the web form designer view (during the ObjectDataSource configuration), and also in code.

For example, the Insert() method enables new objects to be created (i.e. new rows are added to the underlying database table). The Insert() method requires arguments; one for each property (i.e. for each database table column).

This provides you with an object-oriented way of working with a table:

  • the table adapter, as noted above, “Represents the connection and commands used to retrieve and save data”
  • the GetData() command returns all the matching objects (as a strongly typed data table – more on this next)
  • the Insert() command enables a new object to be added
  • the Update() command enables the updating of an existing object
  • the Delete() command enables an object to be deleted

Use Intellisense again to look at these methods and their signatures.


The dataset’s class collection

From the first section above…

<datasetname> – class – Represents a strongly typed in-memory cache of data

Let’s explain this now.

Strongly typed means that the dataset is configured with data members (i.e. properties) and action members (i.e. methods) that use the same types as the data members in underlying data store (e.g. a database); for example, a “FirstName” property would be a string, and would map to a “FirstName” column in a database table

When we create a dataset in our project, we add table (and query) adapters. You already know this.

New info: When you create a table adapter, the dataset designer creates/generates a strongly typed “DataTable” class that matches the table adapter’s schema. This enables you to work with an object that exactly matches the configuration of the underlying data store object. A strongly typed data table class includes a collection of strongly typed data table rows. Each data table row matches conceptually to a “row” in a database table.

Let’s explore your dataset class, and locate these classes. Type the name of your dataset, and press dot, so that Intellisense shows you the data table classes that it has created/generated. Each table adapter has a group of four generated items. Only two are interesting to the beginner, and both are classes:

  • <tableadaptername>DataTable
  • <tableadaptername>Row

The return type of the GetData() method is a strongly typed data table (that matches the table adapter’s schema). When you call this method, it returns a NEW data table.

If you have an EXISTING data table, you use the Fill() method (which is the other method that the table adapter configuration wizard auto-generates). You provide the instance name of the existing strongly typed data table as an argument.


Using these objects in the web form designer

In web form design view, you can use a GridView or DetailsView control to display data. You already know this. Behind the scenes, the control’s ObjectDataSource calls a table adapter method (e.g. GetData()), and a data table is returned. The data table is then bound to the control.

New info: You can configure a GridView or DetailsView to modify (insert, update, delete) data. The behind-the-scenes data table collects the changes (new objects/rows, updated objects/rows, deleted objects/rows), and then the control calls the table adapter’s “update” method to persist the modifications back to the underlying data store.

Although the GridView and the DetailsView offer you data display and modification convenience, they cannot cover every use case. You will have to write code to perform these operations.

In your C# code, you can create instances of these objects:

  • table adapter
  • data table
  • data table row

We’ll see how to do this during the lecture. Then, you’ll see working examples on my “examples” web page.


Categories: 2010 Winter BTI420
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: