(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Data / Test / TestSqlParameters.cs
blob9c309d650b0a5f2d64a2b3f1f944cd5967ec32af
1 //
2 // TestSqlParameters.cs - test parameters for the PostgreSQL .NET Data Provider in Mono
3 // using PgSqlParameter and PgSqlParameterCollection
4 //
5 // Note: it currently only tests input parameters. Output is next on the list.
6 // Then output/input and return parameters.
7 //
8 // Author:
9 // Daniel Morgan <danmorg@sc.rr.com>
11 // (c)copyright 2002 Daniel Morgan
15 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
24 //
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
27 //
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using System;
38 using System.Collections;
39 using System.Data;
40 using Mono.Data.PostgreSqlClient;
42 namespace TestSystemDataPgSqlClient {
44 public class TestParameters {
45 public static void Main() {
46 Console.WriteLine("** Start Test...");
48 String connectionString = null;
49 connectionString =
50 "host=localhost;" +
51 "dbname=test;" +
52 "user=postgres";
54 PgSqlConnection con;
55 Console.WriteLine("** Creating connection...");
56 con = new PgSqlConnection(connectionString);
57 Console.WriteLine("** opening connection...");
58 con.Open();
60 string tableName = "pg_type";
62 string sql;
63 sql = "SELECT * FROM PG_TABLES WHERE TABLENAME = :inTableName";
65 Console.WriteLine("** Creating command...");
66 PgSqlCommand cmd = new PgSqlCommand(sql, con);
68 // add parameter for inTableName
69 Console.WriteLine("** Create parameter...");
70 PgSqlParameter parm = new PgSqlParameter("inTableName", DbType.String);
72 Console.WriteLine("** set dbtype of parameter to string");
73 parm.DbType = DbType.String;
75 Console.WriteLine("** set direction of parameter to input");
76 parm.Direction = ParameterDirection.Input;
78 Console.WriteLine("** set value to the tableName string...");
79 parm.Value = tableName;
81 Console.WriteLine("** add parameter to parameters collection in the command...");
82 cmd.Parameters.Add(parm);
84 PgSqlDataReader rdr;
85 Console.WriteLine("** ExecuteReader()...");
87 rdr = cmd.ExecuteReader();
89 Console.WriteLine("[][] And now we are going to our results [][]...");
90 int c;
91 int results = 0;
92 do {
93 results++;
94 Console.WriteLine("Result Set " + results + "...");
96 // get the DataTable that holds
97 // the schema
98 DataTable dt = rdr.GetSchemaTable();
100 // number of columns in the table
101 Console.WriteLine(" Total Columns: " +
102 dt.Columns.Count);
104 // display the schema
105 foreach (DataRow schemaRow in dt.Rows) {
106 foreach (DataColumn schemaCol in dt.Columns)
107 Console.WriteLine(schemaCol.ColumnName +
108 " = " +
109 schemaRow[schemaCol]);
110 Console.WriteLine();
113 string output, metadataValue, dataValue;
114 int nRows = 0;
116 // Read and display the rows
117 while(rdr.Read()) {
118 Console.WriteLine(" Row " + nRows + ": ");
120 for(c = 0; c < rdr.FieldCount; c++) {
121 // column meta data
122 DataRow dr = dt.Rows[c];
123 metadataValue =
124 " Col " +
125 c + ": " +
126 dr["ColumnName"];
128 // column data
129 if(rdr.IsDBNull(c) == true)
130 dataValue = " is NULL";
131 else
132 dataValue =
133 ": " +
134 rdr.GetValue(c);
136 // display column meta data and data
137 output = metadataValue + dataValue;
138 Console.WriteLine(output);
140 nRows++;
142 Console.WriteLine(" Total Rows: " +
143 nRows);
144 } while(rdr.NextResult());
145 Console.WriteLine("Total Result sets: " + results);
147 con.Close();