Creating a Data Tier Part 1 A Data Tier is needed in order to connect your presentation tier to the database management system. By creating a DataTier you are providing a means to develop multiple presentations, web pages, mobile apps and windows forms with one data access tier. This is so that the presentation can be developed separately from both the Business tier and data tier of you apps providing more portability and modularity to your business apps. In order to create a Data Tier you must first create a dataset that will be holding a local copy of the database data in your app. If you have not done so, please follow the instructions I have laid out on how to add a new data source to you project. Once a new data source has been added to your project you will need to add a new class to your VB project. You do this by right clicking on your solution name in your solution explorer window on the right. Select add new item, then select class. Give a name in the window to your class (here we will say it is CustomerDataTier). This new class will have 2 attributes and three methods that you need to create. In addition to this, you will also need to include the following import statements at the top of the class. Imports System.Data Imports System.Data.Sql Imports System.Data.SqlClient Note: You must know what an attribute of a class is, constructor, accessor and mutator in order to do this tutorial. In addition students should be aware of additional object oriented terms such as Public, Private and Protected. Students should have learned these terms in their pre-requisite courses. The attributes that you need are: 1. A reference to the CustomerDataSet 2. A reference to the CusomerDataSetTableAdapter The attributes of the class may look like the following (keep in mind that I am using my customer information table I created earlier and that is why the names are used bellow). Note: I use the prefix to my variables to indicate the types of objects that they are referencing. Here I use the prefix ds for DataSet and ta for TableAdapter. Private dsCustomerInfo As CustomerInfoDataSet Private taCustomerInfo As CustomerInfoDataSetTableAdapters.CustomerInformationTableAdapter Both of these classes are automatically added to your project when you add a new dataset to the project through the data sources window. Like all references in object oriented programming, you must instantiate an instance of these classes in the constructor to the class. The Methods that you need are: 1. The constructor (in VB this is called NEW) 2. An accessor that for this example we will call getDataSet 3. And a mutator that for this example we will call setDB The Constructor A constructor is called every time the class is instantiated. It is inside this method you want to initialize all your attributes. This includes instantiating your references to both the dataset and the table adapter that you have provided. In VB the constructor takes on a name New. The constructor may look like the one provided bellow. Public Sub New() dsCustomerInfo = New CustomerInfoDataSet() taCustomerInfo = New CustomerInfoDataSetTableAdapters.CustomerInformationTableAdapter() End Sub Notice that with VB, constructors will all have the label New. This is very different from other OOP (Object Oriented Programming) languages such as Java and C++ where the constructors have the same name as the class. The Accessor An accessor will be needed in order for your presentation tier (the form) to have access to the DataSet that you have instantiated inside the Data Tier. Within the accessor you will fill the DataSet using the fill method inside the table adapter. The fill method holds the SELECT sql statements that retrieve all the data from the tables in the DataSet. The table adapter attempts to make a connection to the database each time this Fill method is called. If for any reason, a connection cannot be made to the Database, an exception is thrown from the table adapter. This exception must be caught inside the Accessor of this data tier. The accessor may look like the one that is provided bellow. Public Function getDataSet() As CustomerInfoDataSet Try taCustomerInfo.Fill(dsCustomerInfo.CustomerInformation) Catch ex As SqlException MessageBox.Show("Error in Customer Tier", "Error Filling DataSet " + ex.Message) End Try Return dsCustomerInfo End Function A few things are worth noting here. Inside the Fill Method of the table adapter you will notice that it requires both the name of the DataSet reference and the name of the table you are filling from in your database. These names of course will be different for every Data Tier that you are creating in this class. In addition to this, please also notice that in the exception handling inside of this function, I am catching a specific exception object. In particular, we want to catch the sqpException object thrown so we can get the proper error message generated from our connections to the sqlserver. Also, this Function will return to the presentation tier a reference to the DataSet of type CustomerInfoDataSet. Again the name of the dataset that is return to the presentation tier will be different according to the name you give the dataset when you add them to your project. The Mutator The mutator for the data tier will be needed when the presentation tier wishes to update the database. These updates will be needed whenever a user of your business app wishes to edit existing data or adding new data to the database. The mutator will be a sub procedure in VB since it is not returning any values to the presentation tier. The mutator of the data tier may look like the following. Public Sub setDB() Try taCustomerInfo.Update(dsCustomerInfo.CustomerInformation) dsCustomerInfo.AcceptChanges() Catch ex As SqlException MessageBox.Show("Error in Customer Data Tier", "Error Updating DB " + ex.Message) End Try End Sub Note that there is a specific order on how updates occur in .Net. 1. You Must first End your current Edit (Done in the Presentation Tier Explained later) 2. You must Update the Data from the DataSet to the database (Done in this method). Any changes or modifications done to any row in the dataset is flagged for an update. This includes rows of data that may be deleted. When updating the DB from the dataset, only the rows that have been flagged will be updated. 3. You must then Accept changes in the DataSet. This method of the DataSet will reset all the flags inside the dataset for every row to unchanged. Summary After completing this class, you will be able to access the data to your database using any presentation tier that you wish. In addition to this, you may also use this data tier in a windows service or web service in order for a wider range of apps and presentations to access your data that you wish to provide. As your apps become more robust, you will be adding additional mutators and accessors in order to execute parameterized queries and updating custom data. This tutorial is purely used as a starting point at which the student can get a step into the idea of multi tiered application development.