// (C) 2002 Daniel Morgan\r
//\r
\r
+//\r
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)\r
+//\r
+// Permission is hereby granted, free of charge, to any person obtaining\r
+// a copy of this software and associated documentation files (the\r
+// "Software"), to deal in the Software without restriction, including\r
+// without limitation the rights to use, copy, modify, merge, publish,\r
+// distribute, sublicense, and/or sell copies of the Software, and to\r
+// permit persons to whom the Software is furnished to do so, subject to\r
+// the following conditions:\r
+// \r
+// The above copyright notice and this permission notice shall be\r
+// included in all copies or substantial portions of the Software.\r
+// \r
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+//\r
+\r
using System;\r
using System.Data;\r
using System.Data.SqlClient;\r
String connectionString = null;\r
String sql = null;\r
\r
- connectionString =
- "host=localhost;" +
- "dbname=test;" +
- "user=danmorg;" +
- "password=viewsonic";
+ connectionString = \r
+ "host=localhost;" +\r
+ "dbname=test;" +\r
+ "user=postgres";\r
\r
try {\r
+ string maxStrValue;\r
+\r
con = new SqlConnection(connectionString);\r
con.Open();\r
\r
+ // test SQL Query for an aggregate count(*)\r
sql = "select count(*) " + \r
"from sometable";\r
cmd = new SqlCommand(sql,con);\r
- Console.WriteLine("Executing...");\r
+ Console.WriteLine("Executing: " + sql);\r
Int64 rowCount = (Int64) cmd.ExecuteScalar();\r
Console.WriteLine("Row Count: " + rowCount);\r
\r
+ // test SQL Query for an aggregate min(text)\r
sql = "select max(tdesc) " + \r
"from sometable";\r
cmd = new SqlCommand(sql,con);\r
- Console.WriteLine("Executing...");\r
- String maxValue = (string) cmd.ExecuteScalar();\r
- Console.WriteLine("Max Value: " + maxValue);\r
+ Console.WriteLine("Executing: " + sql);\r
+ string minValue = (string) cmd.ExecuteScalar();\r
+ Console.WriteLine("Max Value: " + minValue);\r
+\r
+ // test SQL Query for an aggregate max(text)\r
+ sql = "select min(tdesc) " + \r
+ "from sometable";\r
+ cmd = new SqlCommand(sql,con);\r
+ Console.WriteLine("Executing: " + sql);\r
+ maxStrValue = (string) cmd.ExecuteScalar();\r
+ Console.WriteLine("Max Value: " + maxStrValue);\r
+\r
+ // test SQL Query for an aggregate max(int)\r
+ sql = "select min(aint4) " + \r
+ "from sometable";\r
+ cmd = new SqlCommand(sql,con);\r
+ Console.WriteLine("Executing: " + sql);\r
+ int maxIntValue = (int) cmd.ExecuteScalar();\r
+ Console.WriteLine("Max Value: " + maxIntValue.ToString());\r
+\r
+ // test SQL Query for an aggregate avg(int)\r
+ sql = "select avg(aint4) " + \r
+ "from sometable";\r
+ cmd = new SqlCommand(sql,con);\r
+ Console.WriteLine("Executing: " + sql);\r
+ decimal avgDecValue = (decimal) cmd.ExecuteScalar();\r
+ Console.WriteLine("Max Value: " + avgDecValue.ToString());\r
+\r
+ // test SQL Query for an aggregate sum(int)\r
+ sql = "select sum(aint4) " + \r
+ "from sometable";\r
+ cmd = new SqlCommand(sql,con);\r
+ Console.WriteLine("Executing: " + sql);\r
+ Int64 summed = (Int64) cmd.ExecuteScalar();\r
+ Console.WriteLine("Max Value: " + summed);\r
+\r
+ // test a SQL Command is (INSERT, UPDATE, DELETE)\r
+ sql = "insert into sometable " +\r
+ "(tid,tdesc,aint4,atimestamp) " +\r
+ "values('qqq','www',234,NULL)";\r
+ cmd = new SqlCommand(sql,con);\r
+ Console.WriteLine("Executing: " + sql);\r
+ object objResult1 = cmd.ExecuteScalar();\r
+ if(objResult1 == null)\r
+ Console.WriteLine("Result is null. (correct)");\r
+ else\r
+ Console.WriteLine("Result is not null. (not correct)");\r
+\r
+ // test a SQL Command is not (INSERT, UPDATE, DELETE)\r
+ sql = "SET DATESTYLE TO 'ISO'";\r
+ cmd = new SqlCommand(sql,con);\r
+ Console.WriteLine("Executing: " + sql);\r
+ object objResult2 = cmd.ExecuteScalar();\r
+ if(objResult2 == null)\r
+ Console.WriteLine("Result is null. (correct)");\r
+ else\r
+ Console.WriteLine("Result is not null. (not correct)");\r
\r
}\r
catch(Exception e) {\r
Console.WriteLine(e.ToString());\r
}\r
finally {\r
- if(con != null)
- if(con.State == ConnectionState.Open)
+ if(con != null)\r
+ if(con.State == ConnectionState.Open)\r
con.Close();\r
}\r
}\r