25 using NUnit.Framework;
31 [Test(Description =
"Counts the people in the table 'Person' with a SQL COUNT statement")]
33 Console.Out.WriteLine(
"Counting the number of rows in 'Person' table...");
36 IDbCommand statement = Connection.CreateCommand();
39 statement.CommandText =
"SELECT COUNT(*) FROM Person";
40 IDataReader result = statement.ExecuteReader();
42 Console.Out.WriteLine(
"Rows in 'Person' table: " + result.GetInt32(0));
48 Console.Out.WriteLine(
"Computing the average ages of people in 'Person' table...");
50 IDbCommand command = Connection.CreateCommand();
51 command.CommandText =
"SELECT AVG(age) FROM Person";
52 IDataReader reader = command.ExecuteReader();
55 Console.Out.WriteLine(
"Average age of people: {0}", reader.GetDouble(0));
62 Console.Out.WriteLine(
"Selecting all the people in 'Person' table who live in Africa...");
64 IDbCommand command = Connection.CreateCommand();
65 command.CommandText =
"SELECT name FROM Person WHERE lives_in = 'Africa' ORDER BY name";
67 IDataReader reader = command.ExecuteReader();
68 Console.Out.WriteLine(
"All people that live in Africa:");
69 while (reader.Read()) {
70 Console.Out.WriteLine(
" " + reader.GetString(0));
72 Console.Out.WriteLine();
79 IDbCommand command = Connection.CreateCommand();
80 command.CommandText =
" SELECT Person.name, MusicGroup.name " +
81 " FROM Person, ListensTo, MusicGroup " +
82 " WHERE MusicGroup.name IN ( 'Oasis', 'Beatles' ) " +
83 " AND Person.name = ListensTo.person_name " +
84 " AND ListensTo.music_group_name = MusicGroup.name " +
85 " ORDER BY MusicGroup.name, Person.name ";
86 IDataReader result = command.ExecuteReader();
87 Console.Out.WriteLine(
"All people that listen to either Beatles or Oasis:");
88 while (result.Read()) {
89 Console.Out.Write(
" " + result.GetString(0));
90 Console.Out.Write(
" listens to ");
91 Console.Out.WriteLine(result.GetString(1));
93 Console.Out.WriteLine();
98 IDbCommand command = Connection.CreateCommand();
99 command.CommandText =
"SELECT IDENTITY FROM Person";
101 using (IDataReader reader = command.ExecuteReader()) {
103 Console.Out.WriteLine(
"The latest identity for the table Person : {0}", reader.GetInt32(0));