1 /* SqlTest.cs - based on PostgresTest.cs
3 * Copyright (C) 2002 Gonzalo Paniagua Javier
4 * Copyright (C) 2002 Daniel Morgan
5 * Copyright (C) 2002 Tim Coleman
8 * Gonzalo Paniagua Javier <gonzalo@gnome-db.org>
9 * PORTING FROM C TO C# AUTHOR:
10 * Daniel Morgan <danmorg@sc.rr.com>
11 * PORTING TO SQL SERVER AUTHOR:
12 * Tim Coleman <tim@timcoleman.com>
14 * Permission was given from the original author, Gonzalo Paniagua Javier,
15 * to port and include his original work in Mono.
17 * The original work falls under the LGPL, but the port to C# falls
18 * under the X11 license.
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License as
22 * published by the Free Software Foundation; either version 2 of the
23 * License, or (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Library General Public License for more details.
30 * You should have received a copy of the GNU General Public
31 * License along with this program; see the file COPYING. If not,
32 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
33 * Boston, MA 02111-1307, USA.
38 using System.Data.SqlClient;
41 namespace Test.Mono.Data.SqlClient {
45 // execute SQL CREATE TABLE Command using ExecuteNonQuery()
46 static void CreateTable (IDbConnection cnc) {
48 IDbCommand createCommand = cnc.CreateCommand();
50 createCommand.CommandText =
51 "create table mono_sql_test (" +
53 "binary_value binary (8), " +
54 "char_value char(50), " +
55 "datetime_value datetime, " +
56 "decimal_value decimal(15, 3), " +
57 "float_value float, " +
58 "image_value image, " +
60 "money_value money, " +
61 "nchar_value nchar(50), " +
62 "ntext_value ntext, " +
63 "nvarchar_value nvarchar(20), " +
65 "smalldatetime_value smalldatetime, " +
66 "smallint_value smallint, " +
67 "smallmoney_value smallmoney, " +
69 "timestamp_value timestamp, " +
70 "tinyint_value tinyint, " +
71 "uniqueidentifier_value uniqueidentifier, " +
72 "varbinary_value varbinary (8), " +
73 "varchar_value varchar(20), " +
74 "null_bit_value bit, " +
75 "null_binary_value binary (8), " +
76 "null_char_value char(50), " +
77 "null_datetime_value datetime, " +
78 "null_decimal_value decimal(15, 3), " +
79 "null_float_value float, " +
80 "null_image_value image, " +
81 "null_int_value int, " +
82 "null_money_value int, " +
83 "null_nchar_value nchar(50), " +
84 "null_ntext_value ntext, " +
85 "null_nvarchar_value nvarchar(20), " +
86 "null_real_value real, " +
87 "null_smalldatetime_value smalldatetime, " +
88 "null_smallint_value smallint, " +
89 "null_smallmoney_value int, " +
90 "null_text_value text, " +
91 "null_tinyint_value tinyint, " +
92 "null_uniqueidentifier_value uniqueidentifier, " +
93 "null_varbinary_value varbinary (8), " +
94 "null_varchar_value varchar(20) " +
97 createCommand.ExecuteNonQuery ();
100 // execute SQL DROP TABLE Command using ExecuteNonQuery
101 static void DropTable (IDbConnection cnc) {
103 IDbCommand dropCommand = cnc.CreateCommand ();
105 dropCommand.CommandText =
106 "drop table mono_sql_test";
108 dropCommand.ExecuteNonQuery ();
111 // execute stored procedure using ExecuteScalar()
112 static object CallStoredProcedure (IDbConnection cnc) {
114 IDbCommand callStoredProcCommand = cnc.CreateCommand ();
117 callStoredProcCommand.CommandType =
118 CommandType.StoredProcedure;
119 callStoredProcCommand.CommandText =
122 data = callStoredProcCommand.ExecuteScalar ();
127 // execute SQL INSERT Command using ExecuteNonQuery()
128 static void InsertData (IDbConnection cnc) {
130 IDbCommand insertCommand = cnc.CreateCommand();
132 insertCommand.CommandText =
133 "insert into mono_sql_test (" +
147 "smalldatetime_value, " +
149 "smallmoney_value, " +
152 "uniqueidentifier_value, " +
153 "varbinary_value, " +
179 SqlParameterCollection parameters = ((SqlCommand) insertCommand).Parameters;
181 parameters.Add ("@p1", SqlDbType.Bit);
182 parameters.Add ("@p2", SqlDbType.Binary, 8);
183 parameters.Add ("@p3", SqlDbType.Char, 14);
184 parameters.Add ("@p4", SqlDbType.DateTime);
185 parameters.Add ("@p5", SqlDbType.Decimal);
186 parameters.Add ("@p6", SqlDbType.Float);
187 parameters.Add ("@p7", SqlDbType.Image);
188 parameters.Add ("@p8", SqlDbType.Int);
189 parameters.Add ("@p9", SqlDbType.Money);
190 parameters.Add ("@p10", SqlDbType.NChar, 16);
191 parameters.Add ("@p11", SqlDbType.NText);
192 parameters.Add ("@p12", SqlDbType.NVarChar, 19);
193 parameters.Add ("@p13", SqlDbType.Real);
194 parameters.Add ("@p14", SqlDbType.SmallDateTime);
195 parameters.Add ("@p15", SqlDbType.SmallInt);
196 parameters.Add ("@p16", SqlDbType.SmallMoney);
197 parameters.Add ("@p17", SqlDbType.Text);
198 parameters.Add ("@p18", SqlDbType.TinyInt);
199 parameters.Add ("@p19", SqlDbType.UniqueIdentifier);
200 parameters.Add ("@p20", SqlDbType.VarBinary, 8);
201 parameters.Add ("@p21", SqlDbType.VarChar, 17);
203 parameters ["@p1"].Value = true;
204 parameters ["@p2"].Value = new byte[2] {0x12,0x34};
205 parameters ["@p3"].Value = "This is a char";
206 parameters ["@p4"].Value = new DateTime (1959, 7, 17); // My mom's birthday!
208 parameters ["@p5"].Value = 123456789012.345;
209 parameters ["@p5"].Precision = 15;
210 parameters ["@p5"].Scale = 3;
212 parameters ["@p6"].Value = 3.1415926969696;
213 parameters ["@p7"].Value = new byte[4] {0xde, 0xad, 0xbe, 0xef};
214 parameters ["@p8"].Value = 1048000;
215 parameters ["@p9"].Value = 31337.456;
216 parameters ["@p10"].Value = "This is an nchar";
217 parameters ["@p11"].Value = "This is an ntext";
218 parameters ["@p12"].Value = "This is an nvarchar";
219 parameters ["@p13"].Value = 3.141592;
220 parameters ["@p14"].Value = new DateTime (1976, 10, 31); // My birthday!
221 parameters ["@p15"].Value = -22;
222 parameters ["@p16"].Value = 31337.456;
223 parameters ["@p17"].Value = "This is a text";
224 parameters ["@p18"].Value = 15;
225 parameters ["@p19"].Value = Guid.NewGuid ();
226 parameters ["@p20"].Value = new byte[2] {0x56,0x78};
227 parameters ["@p21"].Value = "This is a varchar";
229 insertCommand.ExecuteNonQuery ();
232 // execute a SQL SELECT Query using ExecuteReader() to retrieve
233 // a IDataReader so we retrieve data
234 static IDataReader SelectData (IDbConnection cnc) {
236 IDbCommand selectCommand = cnc.CreateCommand();
239 // FIXME: System.Data classes need to handle NULLs
240 // this would be done by System.DBNull ?
241 // FIXME: System.Data needs to handle more data types
243 selectCommand.CommandText =
245 "from mono_postgres_test";
248 selectCommand.CommandText =
263 "smalldatetime_value, " +
265 "smallmoney_value, " +
267 "timestamp_value, " +
269 "uniqueidentifier_value, " +
270 "varbinary_value, " +
273 "null_binary_value, " +
274 "null_char_value, " +
275 "null_datetime_value, " +
276 "null_decimal_value, " +
277 "null_float_value, " +
278 "null_image_value, " +
280 "null_money_value, " +
281 "null_nchar_value, " +
282 "null_ntext_value, " +
283 "null_nvarchar_value, " +
284 "null_real_value, " +
285 "null_smalldatetime_value, " +
286 "null_smallint_value, " +
287 "null_smallmoney_value, " +
288 "null_text_value, " +
289 "null_tinyint_value, " +
290 "null_uniqueidentifier_value, " +
291 "null_varbinary_value, " +
292 "null_varchar_value " +
293 "from mono_sql_test";
296 reader = selectCommand.ExecuteReader ();
301 // Tests a SQL Command (INSERT, UPDATE, DELETE)
302 // executed via ExecuteReader
303 static IDataReader SelectDataUsingInsertCommand (IDbConnection cnc) {
305 IDbCommand selectCommand = cnc.CreateCommand();
308 // This is a SQL INSERT Command, not a Query
309 selectCommand.CommandText =
310 "insert into mono_sql_test (" +
324 "smalldatetime_value, " +
326 "smallmoney_value, " +
329 "uniqueidentifier_value, " +
330 "varbinary_value, " +
356 SqlParameterCollection parameters = ((SqlCommand) selectCommand).Parameters;
358 parameters.Add ("@p1", SqlDbType.Bit);
359 parameters.Add ("@p2", SqlDbType.Binary, 8);
360 parameters.Add ("@p3", SqlDbType.Char, 14);
361 parameters.Add ("@p4", SqlDbType.DateTime);
362 parameters.Add ("@p5", SqlDbType.Decimal);
363 parameters.Add ("@p6", SqlDbType.Float);
364 parameters.Add ("@p7", SqlDbType.Image);
365 parameters.Add ("@p8", SqlDbType.Int);
366 parameters.Add ("@p9", SqlDbType.Money);
367 parameters.Add ("@p10", SqlDbType.NChar, 16);
368 parameters.Add ("@p11", SqlDbType.NText);
369 parameters.Add ("@p12", SqlDbType.NVarChar, 19);
370 parameters.Add ("@p13", SqlDbType.Real);
371 parameters.Add ("@p14", SqlDbType.SmallDateTime);
372 parameters.Add ("@p15", SqlDbType.SmallInt);
373 parameters.Add ("@p16", SqlDbType.SmallMoney);
374 parameters.Add ("@p17", SqlDbType.Text);
375 parameters.Add ("@p18", SqlDbType.TinyInt);
376 parameters.Add ("@p19", SqlDbType.UniqueIdentifier);
377 parameters.Add ("@p20", SqlDbType.VarBinary, 8);
378 parameters.Add ("@p21", SqlDbType.VarChar, 17);
380 parameters ["@p1"].Value = true;
381 parameters ["@p2"].Value = new byte[2] {0x9a,0xbc};
382 parameters ["@p3"].Value = "This is a char";
383 parameters ["@p4"].Value = DateTime.Now;
385 parameters ["@p5"].Value = 123456789012.345;
386 parameters ["@p5"].Precision = 15;
387 parameters ["@p5"].Scale = 3;
389 parameters ["@p6"].Value = 3.1415926969696;
390 parameters ["@p7"].Value = new byte[4] {0xfe, 0xeb, 0xda, 0xed};
391 parameters ["@p8"].Value = 1048000;
392 parameters ["@p9"].Value = 31337.456;
393 parameters ["@p10"].Value = "This is an nchar";
394 parameters ["@p11"].Value = "This is an ntext";
395 parameters ["@p12"].Value = "This is an nvarchar";
396 parameters ["@p13"].Value = 3.141592;
397 parameters ["@p14"].Value = new DateTime (1978, 6, 30); // My brother's birthday!
398 parameters ["@p15"].Value = -22;
399 parameters ["@p16"].Value = 31337.456;
400 parameters ["@p17"].Value = "This is a text";
401 parameters ["@p18"].Value = 15;
402 parameters ["@p19"].Value = Guid.NewGuid ();
403 parameters ["@p20"].Value = new byte[2] {0xde, 0xef};
404 parameters ["@p21"].Value = "This is a varchar";
406 reader = selectCommand.ExecuteReader ();
411 // Tests a SQL Command not (INSERT, UPDATE, DELETE)
412 // executed via ExecuteReader
413 static IDataReader SelectDataUsingCommand (IDbConnection cnc) {
415 IDbCommand selectCommand = cnc.CreateCommand();
418 // This is a SQL Command, not a Query
419 selectCommand.CommandText =
422 reader = selectCommand.ExecuteReader ();
428 // execute an SQL UPDATE Command using ExecuteNonQuery()
429 static void UpdateData (IDbConnection cnc) {
431 IDbCommand updateCommand = cnc.CreateCommand();
433 updateCommand.CommandText =
434 "update mono_sql_test " +
436 "bit_value = @p1, " +
437 "tinyint_value = @p2, " +
438 "smallint_value = @p3, " +
439 "int_value = @p4, " +
440 "char_value = @p5, " +
441 "nchar_value = @p6, " +
442 "varchar_value = @p7, " +
443 "nvarchar_value = @p8, " +
444 "text_value = @p9, " +
445 "ntext_value = @p10, " +
446 "uniqueidentifier_value = @p11, " +
447 "binary_value = @p12, " +
448 "varbinary_value = @p13 " +
449 "where smallint_value = @p14";
451 SqlParameterCollection parameters = ((SqlCommand) updateCommand).Parameters;
453 parameters.Add ("@p1", SqlDbType.Bit);
454 parameters.Add ("@p2", SqlDbType.TinyInt);
455 parameters.Add ("@p3", SqlDbType.SmallInt);
456 parameters.Add ("@p4", SqlDbType.Int);
457 parameters.Add ("@p5", SqlDbType.Char, 10);
458 parameters.Add ("@p6", SqlDbType.NChar, 10);
459 parameters.Add ("@p7", SqlDbType.VarChar, 14);
460 parameters.Add ("@p8", SqlDbType.NVarChar, 14);
461 parameters.Add ("@p9", SqlDbType.Text);
462 parameters.Add ("@p10", SqlDbType.NText);
463 parameters.Add ("@p11", SqlDbType.UniqueIdentifier);
464 parameters.Add ("@p12", SqlDbType.Binary, 8);
465 parameters.Add ("@p13", SqlDbType.VarBinary, 8);
466 parameters.Add ("@p14", SqlDbType.SmallInt);
468 parameters ["@p1"].Value = false;
469 parameters ["@p2"].Value = 2;
470 parameters ["@p3"].Value = 5;
471 parameters ["@p4"].Value = 3;
472 parameters ["@p5"].Value = "Mono.Data!";
473 parameters ["@p6"].Value = "Mono.Data!";
474 parameters ["@p7"].Value = "It was not me!";
475 parameters ["@p8"].Value = "It was not me!";
476 parameters ["@p9"].Value = "We got data!";
477 parameters ["@p10"].Value = "We got data!";
478 parameters ["@p11"].Value = Guid.NewGuid ();
479 parameters ["@p12"].Value = new byte[2] {0x57,0x3e};
480 parameters ["@p13"].Value = new byte[2] {0xa2,0xf7};
481 parameters ["@p14"].Value = -22;
483 updateCommand.ExecuteNonQuery ();
486 // used to do a min(), max(), count(), sum(), or avg()
487 // execute SQL SELECT Query using ExecuteScalar
488 static object SelectAggregate (IDbConnection cnc, String agg) {
490 IDbCommand selectCommand = cnc.CreateCommand();
493 Console.WriteLine("Aggregate: " + agg);
495 selectCommand.CommandType = CommandType.Text;
496 selectCommand.CommandText =
498 "from mono_sql_test";
500 data = selectCommand.ExecuteScalar ();
502 Console.WriteLine("Agg Result: " + data);
507 // used internally by ReadData() to read each result set
508 static void ReadResult(IDataReader rdr, DataTable dt) {
510 // number of columns in the table
\r
511 Console.WriteLine(" Total Columns: " +
\r
514 // display the schema
\r
515 foreach (DataRow schemaRow in dt.Rows) {
\r
516 foreach (DataColumn schemaCol in dt.Columns)
\r
517 Console.WriteLine(schemaCol.ColumnName +
\r
519 schemaRow[schemaCol]);
\r
520 Console.WriteLine();
\r
525 string output, metadataValue, dataValue;
\r
526 // Read and display the rows
\r
527 Console.WriteLine("Gonna do a Read() now...");
\r
528 while(rdr.Read()) {
\r
529 Console.WriteLine(" Row " + nRows + ": ");
\r
531 for(c = 0; c < rdr.FieldCount; c++) {
\r
532 // column meta data
\r
533 DataRow dr = dt.Rows[c];
\r
540 if (rdr.IsDBNull(c) == true)
\r
541 dataValue = " is NULL";
\r
542 else if ((Type) dr["DataType"] == typeof (byte[]))
545 BitConverter.ToString ((byte[]) rdr.GetValue (c)).Replace ("-", "").ToLower ();
551 // display column meta data and data
\r
552 output = metadataValue + dataValue;
\r
553 Console.WriteLine(output);
\r
557 Console.WriteLine(" Total Rows Retrieved: " +
\r
561 // Used to read data from IDataReader after calling IDbCommand:ExecuteReader()
562 static void ReadData(IDataReader rdr) {
567 Console.WriteLine("IDataReader has a Null Reference.");
571 DataTable dt = rdr.GetSchemaTable();
\r
572 if(rdr.RecordsAffected != -1) {
\r
574 // SQL INSERT, UPDATE, DELETE Commands
\r
575 // have RecordsAffected >= 0
\r
576 Console.WriteLine("Result is from a SQL Command (INSERT,UPDATE,DELETE). Records Affected: " + rdr.RecordsAffected);
\r
578 else if(dt == null)
\r
580 // SQL Commands not INSERT, UPDATE, nor DELETE
\r
581 // have RecordsAffected == -1
\r
582 // and GetSchemaTable() returns a null reference
\r
583 Console.WriteLine("Result is from a SQL Command not (INSERT,UPDATE,DELETE). Records Affected: " + rdr.RecordsAffected);
\r
586 // SQL SELECT Queries
\r
587 // have RecordsAffected = -1
\r
588 // and GetSchemaTable() returns a reference to a DataTable
\r
589 Console.WriteLine("Result is from a SELECT SQL Query. Records Affected: " + rdr.RecordsAffected);
\r
592 Console.WriteLine("Result Set " + results + "...");
\r
594 ReadResult(rdr, dt);
\r
597 } while(rdr.NextResult());
\r
598 Console.WriteLine("Total Result sets: " + results);
\r
604 /* Sql provider tests */
605 static void DoSqlTest (IDbConnection cnc) {
610 Console.WriteLine ("\tSql provider specific tests...\n");
612 /* Drops the mono_sql_test table. */
613 Console.WriteLine ("\t\tDrop table: ");
616 Console.WriteLine ("OK");
618 catch (SqlException e) {
619 Console.WriteLine("Error (don't worry about this one)" + e);
623 /* Creates a table with all supported data types */
624 Console.WriteLine ("\t\tCreate table with all supported types: ");
626 Console.WriteLine ("OK");
629 Console.WriteLine ("\t\tInsert values for all known types: ");
631 Console.WriteLine ("OK");
634 Console.WriteLine ("\t\tUpdate values: ");
636 Console.WriteLine ("OK");
639 Console.WriteLine ("\t\tInsert values for all known types: ");
641 Console.WriteLine ("OK");
643 /* Select aggregates */
644 SelectAggregate (cnc, "count(*)");
645 // FIXME: still having a problem with avg()
646 // because it returns a decimal.
647 // It may have something to do
648 // with culture not being set
650 //SelectAggregate (cnc, "avg(int_value)");
651 SelectAggregate (cnc, "min(varchar_value)");
652 SelectAggregate (cnc, "max(int_value)");
653 SelectAggregate (cnc, "sum(int_value)");
656 Console.WriteLine ("\t\tSelect values from the database: ");
657 reader = SelectData (cnc);
660 /* SQL Command via ExecuteReader/SqlDataReader */
661 /* Command is not INSERT, UPDATE, or DELETE */
662 Console.WriteLine("\t\tCall ExecuteReader with a SQL Command. (Not INSERT,UPDATE,DELETE).");
663 reader = SelectDataUsingCommand(cnc);
666 /* SQL Command via ExecuteReader/SqlDataReader */
667 /* Command is INSERT, UPDATE, or DELETE */
668 Console.WriteLine("\t\tCall ExecuteReader with a SQL Command. (Is INSERT,UPDATE,DELETE).");
669 reader = SelectDataUsingInsertCommand(cnc);
672 // Call a Stored Procedure named Version()
\r
673 Console.WriteLine("\t\tCalling stored procedure sp_server_info()");
\r
674 object obj = CallStoredProcedure(cnc);
\r
675 Console.WriteLine("Result: " + obj);
\r
677 Console.WriteLine("Database Server Version: " +
\r
678 ((SqlConnection)cnc).ServerVersion);
\r
681 Console.WriteLine ("Clean up...");
682 Console.WriteLine ("\t\tDrop table...");
684 Console.WriteLine("OK");
687 Console.WriteLine("Exception caught: " + e);
692 static void Main(string[] args) {
\r
693 SqlConnection cnc = new SqlConnection ();
\r
696 string connectionString =
703 string connectionString =
704 "Server=localhost;" +
709 cnc.ConnectionString = connectionString;
\r