Demonstrating Attributes and Reflection in .NET - Putting it all together (Page 6 of 8 )
Now that the SqlObjectManager class has been defined, we can write code to manage objects in the database. The following test code sets up the object manager, creates a Student table, and manages student records within the table:
static void Main(string[] args) { try { // used to manage our students SqlObjectManager om = new SqlObjectManager(); // create a connection to the database SqlConnection conn = new SqlConnection("server=localhost;uid=sa;" + "pwd=;database=mydb"); // open up the connection conn.Open(); // set the connection property of the object manager om.Connection = conn; // create a new student Student s = new Student(); // create a table definition for the student
// add 5 students to the database ExcerciseAddingStudents(om, 5); // retrieve the 5 studens and write // to the console ExcerciseRetrievingStudents(om, 5); // update the address field of the // student object and have the // object manager update the // database ExcerciseUpdatingStudents(om, 5); // retrieve the 5 studens and write // to the console ExcerciseRetrievingStudents(om, 5); // clean up the database by deleting // the 5 students we created ExcerciseDeletingStudents(om, 5); } catch(Exception ex) { Console.WriteLine("------------------------------------"); Console.WriteLine("An Exception was caught: "); Console.WriteLine("------------------------------------"); Console.WriteLine(ex.Message);
} }
The code block above creates an instance of the SqlObjectManager, sets the Connection property, creates the student table, adds five student records, retrieves the records, updates them, and finally deletes them. Here are the methods that the above code block invokes:
static void WriteStudentHeader() {
Console.WriteLine("ID\tName\t\t\tAddress\t\t" + "Number of Courses"); Console.WriteLine(new string('-', 80)); } static void WriteStudentDetails(Student s) { Console.WriteLine("{0}\t{1}\t{2}\t{3}", s.ID, s.FirstName.Trim() + " " + s.LastName.Trim(), s.Address.Trim(), s.NumberOfCourses); } static void ExcerciseDeletingStudents(SqlObjectManager om, int iNumStudents) { if(iNumStudents > 0) { Console.WriteLine("Deleting {0} student records", iNumStudents); // iterate through each of the students and // removes them for(int i = 0; i < iNumStudents; i++) { // construct the student Student storedStudent = new Student(); storedStudent.ID = i; // delete the student om.DeleteObject(storedStudent); } } else { Console.WriteLine("The number of students requested " + "was invalid."); } } static void ExcerciseUpdatingStudents(SqlObjectManager om, int iNumStudents) {
// iterate through each of the students for(int i = 0; i < iNumStudents; i++) {
Student storedStudent = new Student(); storedStudent.ID = i; // retrieve the student om.RetrieveObject(storedStudent); // update the student storedStudent.Address = "Updated " + i; // update the repository om.UpdateRepository(storedStudent); } } else { Console.WriteLine("The number of students requested " + "was invalid."); } } static void ExcerciseRetrievingStudents(SqlObjectManager om, int iNumStudents) { if(iNumStudents > 0) { Console.WriteLine(""); Console.WriteLine("Listing {0} student records", iNumStudents); WriteStudentHeader(); // iterate through the list of students for(int i = 0; i < iNumStudents; i++) { Student storedStudent = new Student(); storedStudent.ID = i; // retrieve the object om.RetrieveObject(storedStudent); // write out the details WriteStudentDetails(storedStudent);
} } else { Console.WriteLine("The number of students requested " + "was invalid."); }
} static void ExcerciseAddingStudents(SqlObjectManager om, int iNumStudents) { if(iNumStudents > 0) { Console.WriteLine("Creating {0} student records", iNumStudents); // lets create 50 studen records for(int i = 0; i < 5; i++) { // create a new student Student newStudent = new Student(); // set the student properties newStudent.ID = i; newStudent.Address = i + " Smith Drive"; newStudent.FirstName = "FirstName " + i; newStudent.LastName = "LastName " + i; newStudent.NumberOfCourses = i; // add the student to the repository om.AddToRepository(newStudent); } } else { Console.WriteLine("The number of students requested " + "was invalid."); } }
When executed, the test code above produces the following output: