TestExecuteScalar.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. using System;
  34. using System.Data;
  35. using System.Data.SqlClient;
  36. namespace TestSystemDataSqlClient
  37. {
  38. class TestSqlDataReader
  39. {
  40. static void Test() {
  41. SqlConnection con = null;
  42. SqlCommand cmd = null;
  43. String connectionString = null;
  44. String sql = null;
  45. connectionString =
  46. "host=localhost;" +
  47. "dbname=test;" +
  48. "user=postgres";
  49. try {
  50. string maxStrValue;
  51. con = new SqlConnection(connectionString);
  52. con.Open();
  53. // test SQL Query for an aggregate count(*)
  54. sql = "select count(*) " +
  55. "from sometable";
  56. cmd = new SqlCommand(sql,con);
  57. Console.WriteLine("Executing: " + sql);
  58. Int64 rowCount = (Int64) cmd.ExecuteScalar();
  59. Console.WriteLine("Row Count: " + rowCount);
  60. // test SQL Query for an aggregate min(text)
  61. sql = "select max(tdesc) " +
  62. "from sometable";
  63. cmd = new SqlCommand(sql,con);
  64. Console.WriteLine("Executing: " + sql);
  65. string minValue = (string) cmd.ExecuteScalar();
  66. Console.WriteLine("Max Value: " + minValue);
  67. // test SQL Query for an aggregate max(text)
  68. sql = "select min(tdesc) " +
  69. "from sometable";
  70. cmd = new SqlCommand(sql,con);
  71. Console.WriteLine("Executing: " + sql);
  72. maxStrValue = (string) cmd.ExecuteScalar();
  73. Console.WriteLine("Max Value: " + maxStrValue);
  74. // test SQL Query for an aggregate max(int)
  75. sql = "select min(aint4) " +
  76. "from sometable";
  77. cmd = new SqlCommand(sql,con);
  78. Console.WriteLine("Executing: " + sql);
  79. int maxIntValue = (int) cmd.ExecuteScalar();
  80. Console.WriteLine("Max Value: " + maxIntValue.ToString());
  81. // test SQL Query for an aggregate avg(int)
  82. sql = "select avg(aint4) " +
  83. "from sometable";
  84. cmd = new SqlCommand(sql,con);
  85. Console.WriteLine("Executing: " + sql);
  86. decimal avgDecValue = (decimal) cmd.ExecuteScalar();
  87. Console.WriteLine("Max Value: " + avgDecValue.ToString());
  88. // test SQL Query for an aggregate sum(int)
  89. sql = "select sum(aint4) " +
  90. "from sometable";
  91. cmd = new SqlCommand(sql,con);
  92. Console.WriteLine("Executing: " + sql);
  93. Int64 summed = (Int64) cmd.ExecuteScalar();
  94. Console.WriteLine("Max Value: " + summed);
  95. // test a SQL Command is (INSERT, UPDATE, DELETE)
  96. sql = "insert into sometable " +
  97. "(tid,tdesc,aint4,atimestamp) " +
  98. "values('qqq','www',234,NULL)";
  99. cmd = new SqlCommand(sql,con);
  100. Console.WriteLine("Executing: " + sql);
  101. object objResult1 = cmd.ExecuteScalar();
  102. if(objResult1 == null)
  103. Console.WriteLine("Result is null. (correct)");
  104. else
  105. Console.WriteLine("Result is not null. (not correct)");
  106. // test a SQL Command is not (INSERT, UPDATE, DELETE)
  107. sql = "SET DATESTYLE TO 'ISO'";
  108. cmd = new SqlCommand(sql,con);
  109. Console.WriteLine("Executing: " + sql);
  110. object objResult2 = cmd.ExecuteScalar();
  111. if(objResult2 == null)
  112. Console.WriteLine("Result is null. (correct)");
  113. else
  114. Console.WriteLine("Result is not null. (not correct)");
  115. }
  116. catch(Exception e) {
  117. Console.WriteLine(e.ToString());
  118. }
  119. finally {
  120. if(con != null)
  121. if(con.State == ConnectionState.Open)
  122. con.Close();
  123. }
  124. }
  125. [STAThread]
  126. static void Main(string[] args)
  127. {
  128. Test();
  129. }
  130. }
  131. }