TestExecuteScalar.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // Test/ExecuteScalar.cs
  3. //
  4. // Test the ExecuteScalar method in the
  5. // System.Data.SqlClient.SqlCommand class
  6. //
  7. // ExecuteScalar is meant to be lightweight
  8. // compared to ExecuteReader and only
  9. // returns one column and one row as one object.
  10. //
  11. // It is meant for SELECT SQL statements that
  12. // use an aggregate/group by function, such as,
  13. // count(), sum(), avg(), min(), max(), etc...
  14. //
  15. // The object that is returned you do an
  16. // explicit cast. For instance, to retrieve a
  17. // Count of rows in a PostgreSQL table, you
  18. // would use "SELECT COUNT(*) FROM SOMETABLE"
  19. // which returns a number of oid type 20 which is
  20. // a PostgreSQL int8 which maps to
  21. // the .NET type System.Int64. You
  22. // have to explicitly convert this returned object
  23. // to the type you are expecting, such as, an Int64
  24. // is returned for a COUNT().
  25. // would be:
  26. // Int64 myCount = (Int64) cmd.ExecuteScalar(selectStatement);
  27. //
  28. // Author:
  29. // Daniel Morgan <[email protected]>
  30. //
  31. // (C) 2002 Daniel Morgan
  32. //
  33. //
  34. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  35. //
  36. // Permission is hereby granted, free of charge, to any person obtaining
  37. // a copy of this software and associated documentation files (the
  38. // "Software"), to deal in the Software without restriction, including
  39. // without limitation the rights to use, copy, modify, merge, publish,
  40. // distribute, sublicense, and/or sell copies of the Software, and to
  41. // permit persons to whom the Software is furnished to do so, subject to
  42. // the following conditions:
  43. //
  44. // The above copyright notice and this permission notice shall be
  45. // included in all copies or substantial portions of the Software.
  46. //
  47. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  48. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  49. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  50. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  51. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  52. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  53. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  54. //
  55. using System;
  56. using System.Data;
  57. using System.Data.SqlClient;
  58. namespace TestSystemDataSqlClient
  59. {
  60. class TestSqlDataReader
  61. {
  62. static void Test() {
  63. SqlConnection con = null;
  64. SqlCommand cmd = null;
  65. String connectionString = null;
  66. String sql = null;
  67. connectionString =
  68. "host=localhost;" +
  69. "dbname=test;" +
  70. "user=postgres";
  71. try {
  72. string maxStrValue;
  73. con = new SqlConnection(connectionString);
  74. con.Open();
  75. // test SQL Query for an aggregate count(*)
  76. sql = "select count(*) " +
  77. "from sometable";
  78. cmd = new SqlCommand(sql,con);
  79. Console.WriteLine("Executing: " + sql);
  80. Int64 rowCount = (Int64) cmd.ExecuteScalar();
  81. Console.WriteLine("Row Count: " + rowCount);
  82. // test SQL Query for an aggregate min(text)
  83. sql = "select max(tdesc) " +
  84. "from sometable";
  85. cmd = new SqlCommand(sql,con);
  86. Console.WriteLine("Executing: " + sql);
  87. string minValue = (string) cmd.ExecuteScalar();
  88. Console.WriteLine("Max Value: " + minValue);
  89. // test SQL Query for an aggregate max(text)
  90. sql = "select min(tdesc) " +
  91. "from sometable";
  92. cmd = new SqlCommand(sql,con);
  93. Console.WriteLine("Executing: " + sql);
  94. maxStrValue = (string) cmd.ExecuteScalar();
  95. Console.WriteLine("Max Value: " + maxStrValue);
  96. // test SQL Query for an aggregate max(int)
  97. sql = "select min(aint4) " +
  98. "from sometable";
  99. cmd = new SqlCommand(sql,con);
  100. Console.WriteLine("Executing: " + sql);
  101. int maxIntValue = (int) cmd.ExecuteScalar();
  102. Console.WriteLine("Max Value: " + maxIntValue.ToString());
  103. // test SQL Query for an aggregate avg(int)
  104. sql = "select avg(aint4) " +
  105. "from sometable";
  106. cmd = new SqlCommand(sql,con);
  107. Console.WriteLine("Executing: " + sql);
  108. decimal avgDecValue = (decimal) cmd.ExecuteScalar();
  109. Console.WriteLine("Max Value: " + avgDecValue.ToString());
  110. // test SQL Query for an aggregate sum(int)
  111. sql = "select sum(aint4) " +
  112. "from sometable";
  113. cmd = new SqlCommand(sql,con);
  114. Console.WriteLine("Executing: " + sql);
  115. Int64 summed = (Int64) cmd.ExecuteScalar();
  116. Console.WriteLine("Max Value: " + summed);
  117. // test a SQL Command is (INSERT, UPDATE, DELETE)
  118. sql = "insert into sometable " +
  119. "(tid,tdesc,aint4,atimestamp) " +
  120. "values('qqq','www',234,NULL)";
  121. cmd = new SqlCommand(sql,con);
  122. Console.WriteLine("Executing: " + sql);
  123. object objResult1 = cmd.ExecuteScalar();
  124. if(objResult1 == null)
  125. Console.WriteLine("Result is null. (correct)");
  126. else
  127. Console.WriteLine("Result is not null. (not correct)");
  128. // test a SQL Command is not (INSERT, UPDATE, DELETE)
  129. sql = "SET DATESTYLE TO 'ISO'";
  130. cmd = new SqlCommand(sql,con);
  131. Console.WriteLine("Executing: " + sql);
  132. object objResult2 = cmd.ExecuteScalar();
  133. if(objResult2 == null)
  134. Console.WriteLine("Result is null. (correct)");
  135. else
  136. Console.WriteLine("Result is not null. (not correct)");
  137. }
  138. catch(Exception e) {
  139. Console.WriteLine(e.ToString());
  140. }
  141. finally {
  142. if(con != null)
  143. if(con.State == ConnectionState.Open)
  144. con.Close();
  145. }
  146. }
  147. [STAThread]
  148. static void Main(string[] args)
  149. {
  150. Test();
  151. }
  152. }
  153. }