**** Merged from MCS ****
[mono-project.git] / mcs / class / Mono.Data.MySql / Test / TestParameters.cs
blob5c8ca16cf62a86dca6de721d4a192219184c8754
1 //
2 // TestParameters.cs - test parameters for the MySQL .NET Data Provider in Mono
3 // using *Parameter and *ParameterCollection
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
14 using System;
15 using System.Collections;
16 using System.Data;
17 using Mono.Data.MySql;
19 namespace TestMonoDataMySql {
21 public class TestParameters {
22 public static void Main() {
23 Console.WriteLine("** Start Test...");
25 String connectionString = null;
26 connectionString =
27 "dbname=test";
29 MySqlConnection con;
30 Console.WriteLine("** Creating connection...");
31 con = new MySqlConnection(connectionString);
32 Console.WriteLine("** opening connection...");
33 con.Open();
35 string charValue = "CD";
37 string sql;
38 sql = "SELECT char_value, int_value FROM mono_mysql_test WHERE char_value = :inCharValue";
40 Console.WriteLine("** Creating command...");
41 MySqlCommand cmd = new MySqlCommand(sql, con);
43 // add parameter for inTableName
44 Console.WriteLine("** Create parameter...");
45 MySqlParameter parm = new MySqlParameter("inCharValue", DbType.String);
47 Console.WriteLine("** set direction of parameter to input");
48 parm.Direction = ParameterDirection.Input;
50 Console.WriteLine("** set the parameter value...");
51 parm.Value = charValue;
53 Console.WriteLine("** add parameter to parameters collection in the command...");
54 cmd.Parameters.Add(parm);
56 MySqlDataReader rdr;
57 Console.WriteLine("** ExecuteReader()...");
59 rdr = cmd.ExecuteReader();
61 Console.WriteLine("[][] And now we are going to our results [][]...");
62 int c;
63 int results = 0;
64 do {
65 results++;
66 Console.WriteLine("Result Set " + results + "...");
68 // get the DataTable that holds
69 // the schema
70 DataTable dt = rdr.GetSchemaTable();
72 // number of columns in the table
73 Console.WriteLine(" Total Columns: " +
74 dt.Columns.Count);
76 // display the schema
77 foreach (DataRow schemaRow in dt.Rows) {
78 foreach (DataColumn schemaCol in dt.Columns)
79 Console.WriteLine(schemaCol.ColumnName +
80 " = " +
81 schemaRow[schemaCol]);
82 Console.WriteLine();
85 string output, metadataValue, dataValue;
86 int nRows = 0;
88 // Read and display the rows
89 while(rdr.Read()) {
90 Console.WriteLine(" Row " + nRows + ": ");
92 for(c = 0; c < rdr.FieldCount; c++) {
93 // column meta data
94 DataRow dr = dt.Rows[c];
95 metadataValue =
96 " Col " +
97 c + ": " +
98 dr["ColumnName"];
100 // column data
101 if(rdr.IsDBNull(c) == true)
102 dataValue = " is NULL";
103 else
104 dataValue =
105 ": " +
106 rdr.GetValue(c);
108 // display column meta data and data
109 output = metadataValue + dataValue;
110 Console.WriteLine(output);
112 nRows++;
114 Console.WriteLine(" Total Rows: " +
115 nRows);
116 } while(rdr.NextResult());
117 Console.WriteLine("Total Result sets: " + results);
119 con.Close();