2005-04-12 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / System.Data / Test / TestSqlParameters.cs
index 1858f5578300fe181805d4bad9842a6796f13dd7..9c309d650b0a5f2d64a2b3f1f944cd5967ec32af 100644 (file)
@@ -1,6 +1,6 @@
 //\r
 // TestSqlParameters.cs - test parameters for the PostgreSQL .NET Data Provider in Mono\r
-//                        using *Parameter and *ParameterCollection\r
+//                        using PgSqlParameter and PgSqlParameterCollection\r
 //\r
 // Note: it currently only tests input parameters.  Output is next on the list.\r
 //       Then output/input and return parameters.\r
 //\r
 // (c)copyright 2002 Daniel Morgan\r
 //\r
+
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
 \r
 using System;\r
 using System.Collections;\r
 using System.Data;\r
-using System.Data.SqlClient;\r
+using Mono.Data.PostgreSqlClient;\r
 \r
-namespace TestSystemDataSqlClient {\r
+namespace TestSystemDataPgSqlClient {\r
 \r
        public class TestParameters {\r
                public static void Main() {\r
@@ -28,9 +51,9 @@ namespace TestSystemDataSqlClient {
                                "dbname=test;" +
                                "user=postgres";
                                                \r
-                       SqlConnection con;\r
+                       PgSqlConnection con;\r
                        Console.WriteLine("** Creating connection...");\r
-                       con = new SqlConnection(connectionString);\r
+                       con = new PgSqlConnection(connectionString);\r
                        Console.WriteLine("** opening connection...");\r
                        con.Open();\r
                \r
@@ -40,24 +63,29 @@ namespace TestSystemDataSqlClient {
                        sql = "SELECT * FROM PG_TABLES WHERE TABLENAME = :inTableName";\r
                                                \r
                        Console.WriteLine("** Creating command...");\r
-                       SqlCommand cmd = new SqlCommand(sql, con);\r
+                       PgSqlCommand cmd = new PgSqlCommand(sql, con);\r
                        \r
                        // add parameter for inTableName\r
                        Console.WriteLine("** Create parameter...");\r
-                       SqlParameter parm = new SqlParameter("inTableName", SqlDbType.Text);\r
+                       PgSqlParameter parm = new PgSqlParameter("inTableName", DbType.String);\r
+                       \r
                        Console.WriteLine("** set dbtype of parameter to string");\r
                        parm.DbType = DbType.String;\r
+                       \r
                        Console.WriteLine("** set direction of parameter to input");\r
                        parm.Direction = ParameterDirection.Input;\r
+                       \r
                        Console.WriteLine("** set value to the tableName string...");\r
                        parm.Value = tableName;\r
                        \r
                        Console.WriteLine("** add parameter to parameters collection in the command...");\r
                        cmd.Parameters.Add(parm);\r
                        \r
-                       SqlDataReader rdr;\r
+                       PgSqlDataReader rdr;\r
                        Console.WriteLine("** ExecuteReader()...");\r
+                       \r
                        rdr = cmd.ExecuteReader();\r
+                       \r
                        Console.WriteLine("[][] And now we are going to our results [][]...");\r
                        int c;\r
                        int results = 0;\r
@@ -74,14 +102,15 @@ namespace TestSystemDataSqlClient {
                                        dt.Columns.Count);\r
 \r
                                // display the schema\r
-                               for(c = 0; c < dt.Columns.Count; c++) {\r
-                                       Console.WriteLine("   Column Name: " + \r
-                                               dt.Columns[c].ColumnName);\r
-                                       Console.WriteLine("          MaxLength: " +\r
-                                               dt.Columns[c].MaxLength);\r
-                                       Console.WriteLine("          Type: " +\r
-                                               dt.Columns[c].DataType);\r
+                               foreach (DataRow schemaRow in dt.Rows) {\r
+                                       foreach (DataColumn schemaCol in dt.Columns)\r
+                                               Console.WriteLine(schemaCol.ColumnName + \r
+                                                       " = " + \r
+                                                       schemaRow[schemaCol]);\r
+                                       Console.WriteLine();\r
                                }\r
+\r
+                               string output, metadataValue, dataValue;\r
                                int nRows = 0;\r
 \r
                                // Read and display the rows\r
@@ -89,13 +118,24 @@ namespace TestSystemDataSqlClient {
                                        Console.WriteLine("   Row " + nRows + ": ");\r
 \r
                                        for(c = 0; c < rdr.FieldCount; c++) {\r
+                                               // column meta data \r
+                                               DataRow dr = dt.Rows[c];\r
+                                               metadataValue = \r
+                                                       "    Col " + \r
+                                                       c + ": " + \r
+                                                       dr["ColumnName"];\r
+                                               \r
+                                               // column data\r
                                                if(rdr.IsDBNull(c) == true)\r
-                                                       Console.WriteLine("      " + \r
-                                                               rdr.GetName(c) + " is DBNull");\r
+                                                       dataValue = " is NULL";\r
                                                else\r
-                                                       Console.WriteLine("      " + \r
-                                                               rdr.GetName(c) + ": " +\r
-                                                               rdr[c].ToString());\r
+                                                       dataValue = \r
+                                                               ": " + \r
+                                                               rdr.GetValue(c);\r
+                                       \r
+                                               // display column meta data and data\r
+                                               output = metadataValue + dataValue;                                     \r
+                                               Console.WriteLine(output);\r
                                        }\r
                                        nRows++;\r
                                }\r