14.7. Exercises

  1. Suppose you define a Friends table using the data structure suggested in Chapter 13. Give two SQL commands that would return the number of active members of the Friends table.

  2. Again using the Friends data structure from Chapter 13, construct an SQL statement that returns all the active members who live in Indiana (or whatever state you might use) and whose second address is not empty.

  3. The code in frmAddFriend is a bit of a hack because it is tightly coupled with the user interface:

    sqlCommand = "INSERT INTO " + dbTableName + " (";
    for (i = 1; i < colNames.Length; i++)
    {
      if (colNames[i] == null)
        break;
      sqlCommand += colNames[i] + ",";
    }
    i = sqlCommand.LastIndexOf(',');
    sqlCommand = sqlCommand.Substring(0, i) + ") VALUES ('";
    // Now add the values
    sqlCommand += txtFirstName.Text + "','" +
                  txtMI.Text + "','" +
                  txtLastName.Text + "','" +
                  txtAddr1.Text + "','" +
                  txtAddr2.Text + "','" +
                  txtCity.Text + "','" +
                  txtState.Text + "','" +
                  txtZip.Text + "','" +
                  txtHome.Text + "','" +
                  txtCell.Text + "','" +
                  txtWork.Text + "','" +
                  txtEmail.Text + "','" +
                  txtBirthday.Text + "','" +
                  txtAnniversary.Text + "'," +
                  status.ToString() + ")";

    The reason it is tightly coupled is that I have tied the inputs to the textbox objects used to gather the information from the user. Because you don't want end user interface objects like textboxes in a non-UI class, the current code shouldn't be moved into class clsDB. However, it would be nice to move the database INSERT command out of the user interface ...

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.