A.12. Chapter 14

A.12.1. Exercise 1 Solution

The two commands are:

SELECT * FROM Friends WHERE status = 1

and:

SELECT SUM(status) FROM Friends

The reason the aggregate SUM function works is that we assigned the value of 1 to an active member and 0 to inactive members. Therefore, SUM also produces a count of active members.

A.12.2. Exercise 2 Solution

The SELECT statement might be written as:

SELECT * FROM Friend WHERE status = 1 AND ADDR2 <> '' AND State = 'IN'

Note that the middle expression of the WHERE predicate is two single-quote marks, not a double-quote mark.

A.12.3. Exercise 3 Solution

There are several ways to do this. First, you could create a new static class (such as clsGlobalFriendData) and give it property members that match the data in the Friends data structure and use the property get and set methods to make the data available to clsDB. This would work because a static data item is instantiated at load time so there is always one (and only one) instance of the class. You could then copy the data from frmAddFriend into the static class object and then let clsDB retrieve it. While this works, it means that you always have a copy of that object hanging around chewing up resources when you may not need it. Also, there's just something about that approach that seems "messy" . . . sorta like using an H-bomb to kill an ant.

A less H-bombish approach would be to copy the contents of each textbox object into a string array and pass that string array to a new InsertFriend() ...

Get Beginning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.