SqlTest.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /* SqlTest.cs - based on PostgresTest.cs
  2. *
  3. * Copyright (C) 2002 Gonzalo Paniagua Javier
  4. * Copyright (C) 2002 Daniel Morgan
  5. * Copyright (C) 2002 Tim Coleman
  6. *
  7. * ORIGINAL AUTHOR:
  8. * Gonzalo Paniagua Javier <[email protected]>
  9. * PORTING FROM C TO C# AUTHOR:
  10. * Daniel Morgan <[email protected]>
  11. * PORTING TO SQL SERVER AUTHOR:
  12. * Tim Coleman <[email protected]>
  13. *
  14. * Permission was given from the original author, Gonzalo Paniagua Javier,
  15. * to port and include his original work in Mono.
  16. *
  17. * The original work falls under the LGPL, but the port to C# falls
  18. * under the X11 license.
  19. *
  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.
  24. *
  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.
  29. *
  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.
  34. */
  35. using System;
  36. using System.Data;
  37. using System.Data.SqlClient;
  38. using System.Text;
  39. namespace Test.Mono.Data.SqlClient {
  40. class SqlTest {
  41. // execute SQL CREATE TABLE Command using ExecuteNonQuery()
  42. static void CreateTable (IDbConnection cnc) {
  43. IDbCommand createCommand = cnc.CreateCommand();
  44. createCommand.CommandText =
  45. "create table mono_sql_test (" +
  46. "bit_value bit, " +
  47. "binary_value binary (8), " +
  48. "char_value char(50), " +
  49. "datetime_value datetime, " +
  50. "decimal_value decimal(15, 3), " +
  51. "float_value float, " +
  52. "image_value image, " +
  53. "int_value int, " +
  54. "money_value money, " +
  55. "nchar_value nchar(50), " +
  56. "ntext_value ntext, " +
  57. "nvarchar_value nvarchar(20), " +
  58. "real_value real, " +
  59. "smalldatetime_value smalldatetime, " +
  60. "smallint_value smallint, " +
  61. "smallmoney_value smallmoney, " +
  62. "text_value text, " +
  63. "timestamp_value timestamp, " +
  64. "tinyint_value tinyint, " +
  65. "uniqueidentifier_value uniqueidentifier, " +
  66. "varbinary_value varbinary (8), " +
  67. "varchar_value varchar(20), " +
  68. "null_bit_value bit, " +
  69. "null_binary_value binary (8), " +
  70. "null_char_value char(50), " +
  71. "null_datetime_value datetime, " +
  72. "null_decimal_value decimal(15, 3), " +
  73. "null_float_value float, " +
  74. "null_image_value image, " +
  75. "null_int_value int, " +
  76. "null_money_value int, " +
  77. "null_nchar_value nchar(50), " +
  78. "null_ntext_value ntext, " +
  79. "null_nvarchar_value nvarchar(20), " +
  80. "null_real_value real, " +
  81. "null_smalldatetime_value smalldatetime, " +
  82. "null_smallint_value smallint, " +
  83. "null_smallmoney_value int, " +
  84. "null_text_value text, " +
  85. "null_tinyint_value tinyint, " +
  86. "null_uniqueidentifier_value uniqueidentifier, " +
  87. "null_varbinary_value varbinary (8), " +
  88. "null_varchar_value varchar(20) " +
  89. ")";
  90. createCommand.ExecuteNonQuery ();
  91. }
  92. // execute SQL DROP TABLE Command using ExecuteNonQuery
  93. static void DropTable (IDbConnection cnc) {
  94. IDbCommand dropCommand = cnc.CreateCommand ();
  95. dropCommand.CommandText =
  96. "drop table mono_sql_test";
  97. dropCommand.ExecuteNonQuery ();
  98. }
  99. // execute stored procedure using ExecuteScalar()
  100. static object CallStoredProcedure (IDbConnection cnc) {
  101. IDbCommand callStoredProcCommand = cnc.CreateCommand ();
  102. object data;
  103. callStoredProcCommand.CommandType =
  104. CommandType.StoredProcedure;
  105. callStoredProcCommand.CommandText =
  106. "sp_server_info";
  107. data = callStoredProcCommand.ExecuteScalar ();
  108. return data;
  109. }
  110. // execute SQL INSERT Command using ExecuteNonQuery()
  111. static void InsertData (IDbConnection cnc) {
  112. IDbCommand insertCommand = cnc.CreateCommand();
  113. insertCommand.CommandText =
  114. "insert into mono_sql_test (" +
  115. "bit_value, " +
  116. "binary_value, " +
  117. "char_value, " +
  118. "datetime_value, " +
  119. "decimal_value, " +
  120. "float_value, " +
  121. "image_value, " +
  122. "int_value, " +
  123. "money_value, " +
  124. "nchar_value, " +
  125. "ntext_value, " +
  126. "nvarchar_value, " +
  127. "real_value, " +
  128. "smalldatetime_value, " +
  129. "smallint_value, " +
  130. "smallmoney_value, " +
  131. "text_value, " +
  132. "tinyint_value, " +
  133. "uniqueidentifier_value, " +
  134. "varbinary_value, " +
  135. "varchar_value " +
  136. ") values (" +
  137. "@p1, " +
  138. "@p2, " +
  139. "@p3, " +
  140. "@p4, " +
  141. "@p5, " +
  142. "@p6, " +
  143. "@p7, " +
  144. "@p8, " +
  145. "@p9, " +
  146. "@p10, " +
  147. "@p11, " +
  148. "@p12, " +
  149. "@p13, " +
  150. "@p14, " +
  151. "@p15, " +
  152. "@p16, " +
  153. "@p17, " +
  154. "@p18, " +
  155. "@p19, " +
  156. "@p20, " +
  157. "@p21 " +
  158. ")";
  159. SqlParameterCollection parameters = ((SqlCommand) insertCommand).Parameters;
  160. parameters.Add ("@p1", SqlDbType.Bit);
  161. parameters.Add ("@p2", SqlDbType.Binary, 8);
  162. parameters.Add ("@p3", SqlDbType.Char, 14);
  163. parameters.Add ("@p4", SqlDbType.DateTime);
  164. parameters.Add ("@p5", SqlDbType.Decimal);
  165. parameters.Add ("@p6", SqlDbType.Float);
  166. parameters.Add ("@p7", SqlDbType.Image);
  167. parameters.Add ("@p8", SqlDbType.Int);
  168. parameters.Add ("@p9", SqlDbType.Money);
  169. parameters.Add ("@p10", SqlDbType.NChar, 16);
  170. parameters.Add ("@p11", SqlDbType.NText);
  171. parameters.Add ("@p12", SqlDbType.NVarChar, 19);
  172. parameters.Add ("@p13", SqlDbType.Real);
  173. parameters.Add ("@p14", SqlDbType.SmallDateTime);
  174. parameters.Add ("@p15", SqlDbType.SmallInt);
  175. parameters.Add ("@p16", SqlDbType.SmallMoney);
  176. parameters.Add ("@p17", SqlDbType.Text);
  177. parameters.Add ("@p18", SqlDbType.TinyInt);
  178. parameters.Add ("@p19", SqlDbType.UniqueIdentifier);
  179. parameters.Add ("@p20", SqlDbType.VarBinary, 8);
  180. parameters.Add ("@p21", SqlDbType.VarChar, 17);
  181. parameters ["@p1"].Value = true;
  182. parameters ["@p2"].Value = new byte[2] {0x12,0x34};
  183. parameters ["@p3"].Value = "This is a char";
  184. parameters ["@p4"].Value = new DateTime (1959, 7, 17); // My mom's birthday!
  185. parameters ["@p5"].Value = 123456789012.345;
  186. parameters ["@p5"].Precision = 15;
  187. parameters ["@p5"].Scale = 3;
  188. parameters ["@p6"].Value = 3.1415926969696;
  189. parameters ["@p7"].Value = new byte[4] {0xde, 0xad, 0xbe, 0xef};
  190. parameters ["@p8"].Value = 1048000;
  191. parameters ["@p9"].Value = 31337.456;
  192. parameters ["@p10"].Value = "This is an nchar";
  193. parameters ["@p11"].Value = "This is an ntext";
  194. parameters ["@p12"].Value = "This is an nvarchar";
  195. parameters ["@p13"].Value = 3.141592;
  196. parameters ["@p14"].Value = new DateTime (1976, 10, 31); // My birthday!
  197. parameters ["@p15"].Value = -22;
  198. parameters ["@p16"].Value = 31337.456;
  199. parameters ["@p17"].Value = "This is a text";
  200. parameters ["@p18"].Value = 15;
  201. parameters ["@p19"].Value = Guid.NewGuid ();
  202. parameters ["@p20"].Value = new byte[2] {0x56,0x78};
  203. parameters ["@p21"].Value = "This is a varchar";
  204. insertCommand.ExecuteNonQuery ();
  205. }
  206. // execute SQL INSERT Command using ExecuteNonQuery()
  207. static void InsertEdgeCaseData (IDbConnection cnc) {
  208. IDbCommand insertCommand = cnc.CreateCommand();
  209. insertCommand.CommandText =
  210. "insert into mono_sql_test (" +
  211. "varbinary_value " +
  212. ") values (" +
  213. "@p20 " +
  214. ")";
  215. SqlParameterCollection parameters = ((SqlCommand) insertCommand).Parameters;
  216. parameters.Add ("@p20", SqlDbType.VarBinary, 8);
  217. parameters ["@p20"].Value = new byte[0] {};
  218. insertCommand.ExecuteNonQuery ();
  219. }
  220. // execute a SQL SELECT Query using ExecuteReader() to retrieve
  221. // a IDataReader so we retrieve data
  222. static IDataReader SelectData (IDbConnection cnc) {
  223. IDbCommand selectCommand = cnc.CreateCommand();
  224. IDataReader reader;
  225. // FIXME: System.Data classes need to handle NULLs
  226. // this would be done by System.DBNull ?
  227. // FIXME: System.Data needs to handle more data types
  228. /*
  229. selectCommand.CommandText =
  230. "select * " +
  231. "from mono_postgres_test";
  232. */
  233. selectCommand.CommandText =
  234. "select " +
  235. "bit_value, " +
  236. "binary_value, " +
  237. "char_value, " +
  238. "datetime_value, " +
  239. "decimal_value, " +
  240. "float_value, " +
  241. "image_value, " +
  242. "int_value, " +
  243. "money_value, " +
  244. "nchar_value, " +
  245. "ntext_value, " +
  246. "nvarchar_value, " +
  247. "real_value, " +
  248. "smalldatetime_value, " +
  249. "smallint_value, " +
  250. "smallmoney_value, " +
  251. "text_value, " +
  252. "timestamp_value, " +
  253. "tinyint_value, " +
  254. "uniqueidentifier_value, " +
  255. "varbinary_value, " +
  256. "varchar_value, " +
  257. "null_bit_value, " +
  258. "null_binary_value, " +
  259. "null_char_value, " +
  260. "null_datetime_value, " +
  261. "null_decimal_value, " +
  262. "null_float_value, " +
  263. "null_image_value, " +
  264. "null_int_value, " +
  265. "null_money_value, " +
  266. "null_nchar_value, " +
  267. "null_ntext_value, " +
  268. "null_nvarchar_value, " +
  269. "null_real_value, " +
  270. "null_smalldatetime_value, " +
  271. "null_smallint_value, " +
  272. "null_smallmoney_value, " +
  273. "null_text_value, " +
  274. "null_tinyint_value, " +
  275. "null_uniqueidentifier_value, " +
  276. "null_varbinary_value, " +
  277. "null_varchar_value " +
  278. "from mono_sql_test";
  279. reader = selectCommand.ExecuteReader ();
  280. return reader;
  281. }
  282. // Tests a SQL Command (INSERT, UPDATE, DELETE)
  283. // executed via ExecuteReader
  284. static IDataReader SelectDataUsingInsertCommand (IDbConnection cnc) {
  285. IDbCommand selectCommand = cnc.CreateCommand();
  286. IDataReader reader;
  287. // This is a SQL INSERT Command, not a Query
  288. selectCommand.CommandText =
  289. "insert into mono_sql_test (" +
  290. "bit_value, " +
  291. "binary_value, " +
  292. "char_value, " +
  293. "datetime_value, " +
  294. "decimal_value, " +
  295. "float_value, " +
  296. "image_value, " +
  297. "int_value, " +
  298. "money_value, " +
  299. "nchar_value, " +
  300. "ntext_value, " +
  301. "nvarchar_value, " +
  302. "real_value, " +
  303. "smalldatetime_value, " +
  304. "smallint_value, " +
  305. "smallmoney_value, " +
  306. "text_value, " +
  307. "tinyint_value, " +
  308. "uniqueidentifier_value, " +
  309. "varbinary_value, " +
  310. "varchar_value " +
  311. ") values (" +
  312. "@p1, " +
  313. "@p2, " +
  314. "@p3, " +
  315. "@p4, " +
  316. "@p5, " +
  317. "@p6, " +
  318. "@p7, " +
  319. "@p8, " +
  320. "@p9, " +
  321. "@p10, " +
  322. "@p11, " +
  323. "@p12, " +
  324. "@p13, " +
  325. "@p14, " +
  326. "@p15, " +
  327. "@p16, " +
  328. "@p17, " +
  329. "@p18, " +
  330. "@p19, " +
  331. "@p20, " +
  332. "@p21 " +
  333. ")";
  334. SqlParameterCollection parameters = ((SqlCommand) selectCommand).Parameters;
  335. parameters.Add ("@p1", SqlDbType.Bit);
  336. parameters.Add ("@p2", SqlDbType.Binary, 8);
  337. parameters.Add ("@p3", SqlDbType.Char, 14);
  338. parameters.Add ("@p4", SqlDbType.DateTime);
  339. parameters.Add ("@p5", SqlDbType.Decimal);
  340. parameters.Add ("@p6", SqlDbType.Float);
  341. parameters.Add ("@p7", SqlDbType.Image);
  342. parameters.Add ("@p8", SqlDbType.Int);
  343. parameters.Add ("@p9", SqlDbType.Money);
  344. parameters.Add ("@p10", SqlDbType.NChar, 16);
  345. parameters.Add ("@p11", SqlDbType.NText);
  346. parameters.Add ("@p12", SqlDbType.NVarChar, 19);
  347. parameters.Add ("@p13", SqlDbType.Real);
  348. parameters.Add ("@p14", SqlDbType.SmallDateTime);
  349. parameters.Add ("@p15", SqlDbType.SmallInt);
  350. parameters.Add ("@p16", SqlDbType.SmallMoney);
  351. parameters.Add ("@p17", SqlDbType.Text);
  352. parameters.Add ("@p18", SqlDbType.TinyInt);
  353. parameters.Add ("@p19", SqlDbType.UniqueIdentifier);
  354. parameters.Add ("@p20", SqlDbType.VarBinary, 8);
  355. parameters.Add ("@p21", SqlDbType.VarChar, 17);
  356. parameters ["@p1"].Value = true;
  357. parameters ["@p2"].Value = new byte[2] {0x9a,0xbc};
  358. parameters ["@p3"].Value = "This is a char";
  359. parameters ["@p4"].Value = DateTime.Now;
  360. parameters ["@p5"].Value = 123456789012.345;
  361. parameters ["@p5"].Precision = 15;
  362. parameters ["@p5"].Scale = 3;
  363. parameters ["@p6"].Value = 3.1415926969696;
  364. parameters ["@p7"].Value = new byte[4] {0xfe, 0xeb, 0xda, 0xed};
  365. parameters ["@p8"].Value = 1048000;
  366. parameters ["@p9"].Value = 31337.456;
  367. parameters ["@p10"].Value = "This is an nchar";
  368. parameters ["@p11"].Value = "This is an ntext";
  369. parameters ["@p12"].Value = "This is an nvarchar";
  370. parameters ["@p13"].Value = 3.141592;
  371. parameters ["@p14"].Value = new DateTime (1978, 6, 30); // My brother's birthday!
  372. parameters ["@p15"].Value = -22;
  373. parameters ["@p16"].Value = 31337.456;
  374. parameters ["@p17"].Value = "This is a text";
  375. parameters ["@p18"].Value = 15;
  376. parameters ["@p19"].Value = Guid.NewGuid ();
  377. parameters ["@p20"].Value = new byte[2] {0xde, 0xef};
  378. parameters ["@p21"].Value = "This is a varchar";
  379. reader = selectCommand.ExecuteReader ();
  380. return reader;
  381. }
  382. // Tests a SQL Command not (INSERT, UPDATE, DELETE)
  383. // executed via ExecuteReader
  384. static IDataReader SelectDataUsingCommand (IDbConnection cnc) {
  385. IDbCommand selectCommand = cnc.CreateCommand();
  386. IDataReader reader;
  387. // This is a SQL Command, not a Query
  388. selectCommand.CommandText =
  389. "SET FMTONLY OFF";
  390. reader = selectCommand.ExecuteReader ();
  391. return reader;
  392. }
  393. // execute an SQL UPDATE Command using ExecuteNonQuery()
  394. static void UpdateData (IDbConnection cnc) {
  395. IDbCommand updateCommand = cnc.CreateCommand();
  396. updateCommand.CommandText =
  397. "update mono_sql_test " +
  398. "set " +
  399. "bit_value = @p1, " +
  400. "tinyint_value = @p2, " +
  401. "smallint_value = @p3, " +
  402. "int_value = @p4, " +
  403. "char_value = @p5, " +
  404. "nchar_value = @p6, " +
  405. "varchar_value = @p7, " +
  406. "nvarchar_value = @p8, " +
  407. "text_value = @p9, " +
  408. "ntext_value = @p10, " +
  409. "uniqueidentifier_value = @p11, " +
  410. "binary_value = @p12, " +
  411. "varbinary_value = @p13 " +
  412. "where smallint_value = @p14";
  413. SqlParameterCollection parameters = ((SqlCommand) updateCommand).Parameters;
  414. parameters.Add ("@p1", SqlDbType.Bit);
  415. parameters.Add ("@p2", SqlDbType.TinyInt);
  416. parameters.Add ("@p3", SqlDbType.SmallInt);
  417. parameters.Add ("@p4", SqlDbType.Int);
  418. parameters.Add ("@p5", SqlDbType.Char, 10);
  419. parameters.Add ("@p6", SqlDbType.NChar, 10);
  420. parameters.Add ("@p7", SqlDbType.VarChar, 14);
  421. parameters.Add ("@p8", SqlDbType.NVarChar, 14);
  422. parameters.Add ("@p9", SqlDbType.Text);
  423. parameters.Add ("@p10", SqlDbType.NText);
  424. parameters.Add ("@p11", SqlDbType.UniqueIdentifier);
  425. parameters.Add ("@p12", SqlDbType.Binary, 8);
  426. parameters.Add ("@p13", SqlDbType.VarBinary, 8);
  427. parameters.Add ("@p14", SqlDbType.SmallInt);
  428. parameters ["@p1"].Value = false;
  429. parameters ["@p2"].Value = 2;
  430. parameters ["@p3"].Value = 5;
  431. parameters ["@p4"].Value = 3;
  432. parameters ["@p5"].Value = "Mono.Data!";
  433. parameters ["@p6"].Value = "Mono.Data!";
  434. parameters ["@p7"].Value = "It was not me!";
  435. parameters ["@p8"].Value = "It was not me!";
  436. parameters ["@p9"].Value = "We got data!";
  437. parameters ["@p10"].Value = "We got data!";
  438. parameters ["@p11"].Value = Guid.NewGuid ();
  439. parameters ["@p12"].Value = new byte[2] {0x57,0x3e};
  440. parameters ["@p13"].Value = new byte[2] {0xa2,0xf7};
  441. parameters ["@p14"].Value = -22;
  442. updateCommand.ExecuteNonQuery ();
  443. }
  444. // used to do a min(), max(), count(), sum(), or avg()
  445. // execute SQL SELECT Query using ExecuteScalar
  446. static object SelectAggregate (IDbConnection cnc, String agg) {
  447. IDbCommand selectCommand = cnc.CreateCommand();
  448. object data;
  449. Console.WriteLine("Aggregate: " + agg);
  450. selectCommand.CommandType = CommandType.Text;
  451. selectCommand.CommandText =
  452. "select " + agg +
  453. "from mono_sql_test";
  454. data = selectCommand.ExecuteScalar ();
  455. Console.WriteLine("Agg Result: " + data);
  456. return data;
  457. }
  458. // used internally by ReadData() to read each result set
  459. static void ReadResult(IDataReader rdr, DataTable dt) {
  460. // number of columns in the table
  461. Console.WriteLine(" Total Columns: " +
  462. dt.Rows.Count);
  463. // display the schema
  464. foreach (DataRow schemaRow in dt.Rows) {
  465. foreach (DataColumn schemaCol in dt.Columns)
  466. Console.WriteLine(schemaCol.ColumnName +
  467. " = " +
  468. schemaRow[schemaCol]);
  469. Console.WriteLine();
  470. }
  471. int nRows = 0;
  472. int c = 0;
  473. string output, metadataValue, dataValue;
  474. // Read and display the rows
  475. Console.WriteLine("Gonna do a Read() now...");
  476. while(rdr.Read()) {
  477. Console.WriteLine(" Row " + nRows + ": ");
  478. for(c = 0; c < rdr.FieldCount; c++) {
  479. // column meta data
  480. DataRow dr = dt.Rows[c];
  481. metadataValue =
  482. " Col " +
  483. c + ": " +
  484. dr["ColumnName"];
  485. // column data
  486. if (rdr.IsDBNull(c) == true)
  487. dataValue = " is NULL";
  488. else if ((Type) dr["DataType"] == typeof (byte[]))
  489. dataValue =
  490. ": 0x" +
  491. BitConverter.ToString ((byte[]) rdr.GetValue (c)).Replace ("-", "").ToLower ();
  492. else
  493. dataValue =
  494. ": " +
  495. rdr.GetValue(c);
  496. // display column meta data and data
  497. output = metadataValue + dataValue;
  498. Console.WriteLine(output);
  499. }
  500. nRows++;
  501. }
  502. Console.WriteLine(" Total Rows Retrieved: " +
  503. nRows);
  504. }
  505. // Used to read data from IDataReader after calling IDbCommand:ExecuteReader()
  506. static void ReadData(IDataReader rdr) {
  507. int results = 0;
  508. if(rdr == null) {
  509. Console.WriteLine("IDataReader has a Null Reference.");
  510. }
  511. else {
  512. do {
  513. DataTable dt = rdr.GetSchemaTable();
  514. if(rdr.RecordsAffected != -1) {
  515. // Results for
  516. // SQL INSERT, UPDATE, DELETE Commands
  517. // have RecordsAffected >= 0
  518. Console.WriteLine("Result is from a SQL Command (INSERT,UPDATE,DELETE). Records Affected: " + rdr.RecordsAffected);
  519. }
  520. else if(dt == null)
  521. // Results for
  522. // SQL Commands not INSERT, UPDATE, nor DELETE
  523. // have RecordsAffected == -1
  524. // and GetSchemaTable() returns a null reference
  525. Console.WriteLine("Result is from a SQL Command not (INSERT,UPDATE,DELETE). Records Affected: " + rdr.RecordsAffected);
  526. else {
  527. // Results for
  528. // SQL SELECT Queries
  529. // have RecordsAffected = -1
  530. // and GetSchemaTable() returns a reference to a DataTable
  531. Console.WriteLine("Result is from a SELECT SQL Query. Records Affected: " + rdr.RecordsAffected);
  532. results++;
  533. Console.WriteLine("Result Set " + results + "...");
  534. ReadResult(rdr, dt);
  535. }
  536. } while(rdr.NextResult());
  537. Console.WriteLine("Total Result sets: " + results);
  538. rdr.Close();
  539. }
  540. }
  541. /* Sql provider tests */
  542. static void DoSqlTest (IDbConnection cnc) {
  543. IDataReader reader;
  544. Object oDataValue;
  545. Console.WriteLine ("\tSql provider specific tests...\n");
  546. /* Drops the mono_sql_test table. */
  547. Console.WriteLine ("\t\tDrop table: ");
  548. try {
  549. DropTable (cnc);
  550. Console.WriteLine ("OK");
  551. }
  552. catch (SqlException e) {
  553. Console.WriteLine("Error (don't worry about this one)" + e);
  554. }
  555. try {
  556. /* Creates a table with all supported data types */
  557. Console.WriteLine ("\t\tCreate table with all supported types: ");
  558. CreateTable (cnc);
  559. Console.WriteLine ("OK");
  560. /* Inserts values */
  561. Console.WriteLine ("\t\tInsert values for all known types: ");
  562. InsertData (cnc);
  563. Console.WriteLine ("OK");
  564. /* Update values */
  565. Console.WriteLine ("\t\tUpdate values: ");
  566. UpdateData (cnc);
  567. Console.WriteLine ("OK");
  568. /* Inserts values */
  569. Console.WriteLine ("\t\tInsert values for all known types: ");
  570. InsertData (cnc);
  571. Console.WriteLine ("OK");
  572. /* Inserts edge case values */
  573. Console.WriteLine ("\t\tInsert values that require special coding: ");
  574. InsertEdgeCaseData (cnc);
  575. Console.WriteLine ("OK");
  576. /* Select aggregates */
  577. SelectAggregate (cnc, "count(*)");
  578. // FIXME: still having a problem with avg()
  579. // because it returns a decimal.
  580. // It may have something to do
  581. // with culture not being set
  582. // properly.
  583. //SelectAggregate (cnc, "avg(int_value)");
  584. SelectAggregate (cnc, "min(varchar_value)");
  585. SelectAggregate (cnc, "max(int_value)");
  586. SelectAggregate (cnc, "sum(int_value)");
  587. /* Select values */
  588. Console.WriteLine ("\t\tSelect values from the database: ");
  589. reader = SelectData (cnc);
  590. ReadData(reader);
  591. /* SQL Command via ExecuteReader/SqlDataReader */
  592. /* Command is not INSERT, UPDATE, or DELETE */
  593. Console.WriteLine("\t\tCall ExecuteReader with a SQL Command. (Not INSERT,UPDATE,DELETE).");
  594. reader = SelectDataUsingCommand(cnc);
  595. ReadData(reader);
  596. /* SQL Command via ExecuteReader/SqlDataReader */
  597. /* Command is INSERT, UPDATE, or DELETE */
  598. Console.WriteLine("\t\tCall ExecuteReader with a SQL Command. (Is INSERT,UPDATE,DELETE).");
  599. reader = SelectDataUsingInsertCommand(cnc);
  600. ReadData(reader);
  601. // Call a Stored Procedure named Version()
  602. Console.WriteLine("\t\tCalling stored procedure sp_server_info()");
  603. object obj = CallStoredProcedure(cnc);
  604. Console.WriteLine("Result: " + obj);
  605. Console.WriteLine("Database Server Version: " +
  606. ((SqlConnection)cnc).ServerVersion);
  607. /* Clean up */
  608. Console.WriteLine ("Clean up...");
  609. Console.WriteLine ("\t\tDrop table...");
  610. //DropTable (cnc);
  611. Console.WriteLine("OK");
  612. }
  613. catch(Exception e) {
  614. Console.WriteLine("Exception caught: " + e);
  615. }
  616. }
  617. [STAThread]
  618. static void Main(string[] args) {
  619. string connectionString = "";
  620. if(args.Length == 3 || args.Length == 4) {
  621. if(args.Length == 3) {
  622. connectionString = String.Format(
  623. "Server={0};" +
  624. "Database={1};" +
  625. "User ID={2};",
  626. args[0], args[1], args[2]);
  627. }
  628. else if(args.Length == 4) {
  629. connectionString = String.Format(
  630. "Server={0};" +
  631. "Database={1};" +
  632. "User ID={2};" +
  633. "Password={3}",
  634. args[0], args[1], args[2], args[3]);
  635. }
  636. }
  637. else {
  638. Console.WriteLine("Usage: mono SqlTest.exe sql_server database user_id password");
  639. return;
  640. }
  641. SqlConnection cnc = new SqlConnection ();
  642. cnc.ConnectionString = connectionString;
  643. cnc.Open();
  644. DoSqlTest(cnc);
  645. cnc.Close();
  646. }
  647. }
  648. }