Creating a StudentDB Class for ASP.NET 2.0 - The StudentDB class
(Page 5 of 5 )
The studentDB class contains the methods InsertStudent() and UpdateStudent() which accept a parameter of type Student, and the method DeleteStudent() which accepts an integer value that represents the Student ID. Both InsertStudent() and UpdateStudent() methods do the insertion and the updating on the Students table by using the Student instance passed as a parameter to them.
In other words, the Student instance passed to those methods represents a record in the Students table, and by calling a method like UpdateStudent() and passing it that Student instance, the database record (that this Student instance represents) will be updated with the values of the properties of that Student instance. The code that we write in this small series will crystallize this concept for you -- and after this series I will teach you another technique to perform the same operations on the Students table using other classes.
The StudentDB class also contains the GetAllStudents() and GetStudent() methods which we will develop and discuss. Note that all these methods are static and we don't need to create instances to use them; it makes sense if you think about it. The only part that would be an instance is the object passed to the method as a parameter. You create an instance of the Student class and pass it to a method like InsertStudent() which will do its job and insert a record that represents that Student object into the database. Now let's look at this class:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
// additional namespaces needed for this class
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Collections.Generic;
public class StudentDB
{
private readonly static string connString;
static StudentDB()
{
connString = WebConfigurationManager.ConnectionStrings
["SchoolConnectionString"].ConnectionString;
}
public static int InsertStudent(Student student)
{ }
public static void UpdateStudent(Student student)
{ }
public static void DeleteStudent(int studentId)
{ }
public static Student GetStudent(int studentId)
{ }
public static DataTable GetAllStudentsInDataTable()
{ }
public static List<Student> GetAllStudentsInCollection()
{ }
}
These are the methods we are going to develop in the series. For now just keep in mind that all the members are static and the connString is a private readonly string member that is assigned its value using the static constructor with the syntax we have discussed in the previous section. Also note that we have added three additional namespace references. The first is a namespace reference to the ADO.NET SQL Server Data Provider and the second is needed by the constructor's code because the WebConfigurationManager class lives in the namespace System.Web.Configuration. The third namespace is needed because we are using C# Generics in the GetAllStudentsInCollection() method.
C# Generics is a great new feature that has been added in .NET Framework 2.0 which gives you the ability to create a strongly typed collection without writing any code. we simply use the .NET-provided generic collection types, like List<>, and tell it what type we need to use to create a strongly typed collection. In our case, we need a collection of Student objects, so we have used the syntax List<Student>. As you will see, you don't have to create a collection class and use an inner collection object, like the ArrayList, along with public methods, like Add(Student student), that accept the strongly typed object with which you want to work. All what you have to do is use generics.
In the next article, we are going develop the GetStudent() and GetAllStudentsInCollection() methods.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |