SqlDataAdapterTest.cs 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. //
  2. // SqlDataAdapterTest.cs - NUnit Test Cases for testing the
  3. // SqlDataAdapter class
  4. // Author:
  5. // Umadevi S ([email protected])
  6. // Sureshkumar T ([email protected])
  7. // Senganal T ([email protected])
  8. //
  9. // Copyright (c) 2004 Novell Inc., and the individuals listed
  10. // on the ChangeLog entries.
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Data;
  33. using System.Data.Common;
  34. using System.Data.SqlClient;
  35. using Mono.Data;
  36. using System.Configuration;
  37. using NUnit.Framework;
  38. namespace MonoTests.System.Data.SqlClient
  39. {
  40. [TestFixture]
  41. [Category ("sqlserver")]
  42. public class SqlDataAdapterTest
  43. {
  44. SqlDataAdapter adapter;
  45. SqlDataReader dr;
  46. DataSet data;
  47. string connectionString = ConnectionManager.Singleton.ConnectionString;
  48. SqlConnection conn;
  49. EngineConfig engine;
  50. [SetUp]
  51. public void SetUp ()
  52. {
  53. engine = ConnectionManager.Singleton.Engine;
  54. }
  55. [TearDown]
  56. public void TearDown ()
  57. {
  58. if (adapter != null) {
  59. adapter.Dispose ();
  60. adapter = null;
  61. }
  62. if (dr != null) {
  63. dr.Close ();
  64. dr = null;
  65. }
  66. if (conn != null) {
  67. conn.Close ();
  68. conn = null;
  69. }
  70. }
  71. [Test]
  72. public void Update_DeleteRow ()
  73. {
  74. conn = new SqlConnection (ConnectionManager.Singleton.ConnectionString);
  75. conn.Open ();
  76. DataTable dt = new DataTable ();
  77. adapter = new SqlDataAdapter ("SELECT * FROM employee", conn);
  78. SqlCommandBuilder builder = new SqlCommandBuilder (adapter);
  79. adapter.DeleteCommand = builder.GetDeleteCommand ();
  80. adapter.Fill (dt);
  81. DateTime now = DateTime.Now;
  82. DateTime doj = new DateTime (now.Year, now.Month, now.Day, now.Hour,
  83. now.Minute, now.Second);
  84. DateTime dob = new DateTime (now.Year, now.Month, now.Day, now.Hour,
  85. now.Minute, now.Second);
  86. dob.Subtract (new TimeSpan (20 * 365, 0, 0, 0));
  87. try {
  88. DataRow newRow = dt.NewRow ();
  89. newRow ["id"] = 6002;
  90. newRow ["fname"] = "boston";
  91. newRow ["dob"] = dob;
  92. newRow ["doj"] = doj;
  93. newRow ["email"] = "[email protected]";
  94. dt.Rows.Add (newRow);
  95. adapter.Update (dt);
  96. foreach (DataRow row in dt.Rows)
  97. if (((int) row ["id"]) == 6002)
  98. row.Delete ();
  99. adapter.Update (dt);
  100. SqlCommand cmd = conn.CreateCommand ();
  101. cmd.CommandText = "SELECT id, fname, lname, dob, doj, email FROM employee WHERE id = 6002";
  102. dr = cmd.ExecuteReader ();
  103. Assert.IsFalse (dr.Read ());
  104. dr.Close ();
  105. } finally {
  106. DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
  107. }
  108. }
  109. [Test]
  110. public void Update_InsertRow ()
  111. {
  112. conn = new SqlConnection (ConnectionManager.Singleton.ConnectionString);
  113. conn.Open ();
  114. DataTable dt = new DataTable ();
  115. adapter = new SqlDataAdapter ("SELECT * FROM employee", conn);
  116. SqlCommandBuilder builder = new SqlCommandBuilder (adapter);
  117. adapter.InsertCommand = builder.GetInsertCommand ();
  118. adapter.Fill (dt);
  119. DateTime now = DateTime.Now;
  120. DateTime doj = new DateTime (now.Year, now.Month, now.Day, now.Hour,
  121. now.Minute, now.Second);
  122. DateTime dob = new DateTime (now.Year, now.Month, now.Day, now.Hour,
  123. now.Minute, now.Second);
  124. dob.Subtract (new TimeSpan (20 * 365, 0, 0, 0));
  125. try {
  126. DataRow newRow = dt.NewRow ();
  127. newRow ["id"] = 6002;
  128. newRow ["fname"] = "boston";
  129. newRow ["dob"] = dob;
  130. newRow ["doj"] = doj;
  131. newRow ["email"] = "[email protected]";
  132. dt.Rows.Add (newRow);
  133. adapter.Update (dt);
  134. SqlCommand cmd = conn.CreateCommand ();
  135. cmd.CommandText = "SELECT id, fname, lname, dob, doj, email FROM employee WHERE id = 6002";
  136. dr = cmd.ExecuteReader ();
  137. Assert.IsTrue (dr.Read (), "#A1");
  138. Assert.AreEqual (6002, dr.GetValue (0), "#A2");
  139. Assert.AreEqual ("boston", dr.GetValue (1), "#A3");
  140. Assert.AreEqual (DBNull.Value, dr.GetValue (2), "#A4");
  141. Assert.AreEqual (dob, dr.GetValue (3), "#A5");
  142. Assert.AreEqual (doj, dr.GetValue (4), "#A6");
  143. Assert.AreEqual ("[email protected]", dr.GetValue (5), "#A7");
  144. Assert.IsFalse (dr.Read (), "#A8");
  145. dr.Close ();
  146. } finally {
  147. DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
  148. }
  149. }
  150. [Test]
  151. public void Update_UpdateRow ()
  152. {
  153. conn = new SqlConnection (ConnectionManager.Singleton.ConnectionString);
  154. conn.Open ();
  155. DataTable dt = new DataTable ();
  156. adapter = new SqlDataAdapter ("SELECT * FROM employee", conn);
  157. SqlCommandBuilder builder = new SqlCommandBuilder (adapter);
  158. adapter.UpdateCommand = builder.GetUpdateCommand ();
  159. adapter.Fill (dt);
  160. DateTime now = DateTime.Now;
  161. DateTime doj = new DateTime (now.Year, now.Month, now.Day, now.Hour,
  162. now.Minute, now.Second);
  163. DateTime dob = new DateTime (now.Year, now.Month, now.Day, now.Hour,
  164. now.Minute, now.Second);
  165. dob.Subtract (new TimeSpan (20 * 365, 0, 0, 0));
  166. try {
  167. DataRow newRow = dt.NewRow ();
  168. newRow ["id"] = 6002;
  169. newRow ["fname"] = "boston";
  170. newRow ["dob"] = dob;
  171. newRow ["doj"] = doj;
  172. newRow ["email"] = "[email protected]";
  173. dt.Rows.Add (newRow);
  174. adapter.Update (dt);
  175. foreach (DataRow row in dt.Rows)
  176. if (((int) row ["id"]) == 6002)
  177. row ["lname"] = "de Icaza";
  178. adapter.Update (dt);
  179. SqlCommand cmd = conn.CreateCommand ();
  180. cmd.CommandText = "SELECT id, fname, lname, dob, doj, email FROM employee WHERE id = 6002";
  181. dr = cmd.ExecuteReader ();
  182. Assert.IsTrue (dr.Read (), "#A1");
  183. Assert.AreEqual (6002, dr.GetValue (0), "#A2");
  184. Assert.AreEqual ("boston", dr.GetValue (1), "#A3");
  185. Assert.AreEqual ("de Icaza", dr.GetValue (2), "#A4");
  186. Assert.AreEqual (dob, dr.GetValue (3), "#A5");
  187. Assert.AreEqual (doj, dr.GetValue (4), "#A6");
  188. Assert.AreEqual ("[email protected]", dr.GetValue (5), "#A7");
  189. Assert.IsFalse (dr.Read (), "#A8");
  190. dr.Close ();
  191. } finally {
  192. DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
  193. }
  194. }
  195. /**
  196. The below test will not run everytime, since the region id column is unique
  197. so change the regionid if you want the test to pass.
  198. **/
  199. /*
  200. [Test]
  201. public void UpdateTest () {
  202. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  203. try {
  204. ConnectionManager.Singleton.OpenConnection ();
  205. DataTable dt = new DataTable();
  206. SqlDataAdapter da = null;
  207. da = new SqlDataAdapter("Select * from employee", conn);
  208. //SqlCommandBuilder cb = new SqlCommandBuilder (da);
  209. da.Fill(dt);
  210. DataRow dr = dt.NewRow();
  211. dr ["id"] = 6002;
  212. dr ["fname"] = "boston";
  213. dr ["dob"] = DateTime.Now.Subtract (new TimeSpan (20*365, 0, 0, 0));
  214. dr ["doj"] = DateTime.Now;
  215. dt.Rows.Add(dr);
  216. da.Update(dt);
  217. } finally {
  218. DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
  219. ConnectionManager.Singleton.CloseConnection ();
  220. }
  221. }
  222. private static void OnRowUpdatedTest (object sender, SqlRowUpdatedEventArgs e)
  223. {
  224. rowUpdated = true;
  225. }
  226. private static void OnRowUpdatingTest (object sender, SqlRowUpdatingEventArgs e)
  227. {
  228. rowUpdating = true;
  229. }
  230. private static bool rowUpdated = false;
  231. private static bool rowUpdating = false;
  232. [Test]
  233. public void RowUpdatedTest () {
  234. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  235. try {
  236. ConnectionManager.Singleton.OpenConnection ();
  237. DataTable dt = null;
  238. DataSet ds = new DataSet ();
  239. SqlDataAdapter da = null;
  240. da = new SqlDataAdapter("Select * from employee", conn);
  241. //SqlCommandBuilder cb = new SqlCommandBuilder (da);
  242. rowUpdated = false;
  243. rowUpdating = false;
  244. da.RowUpdated += new SqlRowUpdatedEventHandler (OnRowUpdatedTest);
  245. da.RowUpdating += new SqlRowUpdatingEventHandler (OnRowUpdatingTest);
  246. da.Fill (ds);
  247. dt = ds.Tables [0];
  248. dt.Rows[0][0] = 200;
  249. da.UpdateCommand = new SqlCommand ("Update employee set id = @id");
  250. da.Update (dt);
  251. dt.Rows[0][0] = 1;
  252. da.Update (dt);
  253. da.RowUpdated -= new SqlRowUpdatedEventHandler (OnRowUpdatedTest);
  254. da.RowUpdating -= new SqlRowUpdatingEventHandler (OnRowUpdatingTest);
  255. Assert.AreEqual (true, rowUpdated, "RowUpdated");
  256. Assert.AreEqual (true, rowUpdating, "RowUpdating");
  257. } finally {
  258. DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
  259. ConnectionManager.Singleton.CloseConnection ();
  260. }
  261. }
  262. */
  263. /**
  264. This needs a errortable created as follows
  265. id uniqueidentifier,name char(10) , with values
  266. Guid name
  267. {A12...} NULL
  268. NULL bbbbbb
  269. **/
  270. [Test]
  271. public void NullGuidTest()
  272. {
  273. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  274. try {
  275. ConnectionManager.Singleton.OpenConnection ();
  276. DBHelper.ExecuteNonQuery (conn, "create table #tmp_guid_table ( " +
  277. " id uniqueidentifier default newid (), " +
  278. " name char (10))");
  279. DBHelper.ExecuteNonQuery (conn, "insert into #tmp_guid_table (name) values (null)");
  280. DBHelper.ExecuteNonQuery (conn, "insert into #tmp_guid_table (id, name) values (null, 'bbbb')");
  281. SqlDataAdapter da = new SqlDataAdapter("select * from #tmp_guid_table", conn);
  282. DataSet ds = new DataSet();
  283. da.Fill(ds);
  284. Assert.AreEqual (1, ds.Tables.Count, "#1");
  285. Assert.AreEqual (DBNull.Value, ds.Tables [0].Rows [1] ["id"], "#2");
  286. } finally {
  287. ConnectionManager.Singleton.CloseConnection ();
  288. }
  289. // the bug 68804 - is that the fill hangs!
  290. Assert.AreEqual("Done","Done");
  291. }
  292. [Test]
  293. public void DefaultConstructorTest ()
  294. {
  295. adapter = new SqlDataAdapter ();
  296. Assert.AreEqual (MissingMappingAction.Passthrough,
  297. adapter.MissingMappingAction,
  298. "#1 Missing Mapping acttion default to Passthrough");
  299. Assert.AreEqual (MissingSchemaAction.Add,
  300. adapter.MissingSchemaAction,
  301. "#2 Missing Schme action default to Add");
  302. }
  303. [Test]
  304. public void OverloadedConstructorsTest ()
  305. {
  306. SqlCommand selCmd = new SqlCommand ("Select * from numeric_family");
  307. adapter = new SqlDataAdapter (selCmd);
  308. Assert.AreEqual (MissingMappingAction.Passthrough,
  309. adapter.MissingMappingAction,
  310. "#1 Missing Mapping acttion default to Passthrough");
  311. Assert.AreEqual (MissingSchemaAction.Add,
  312. adapter.MissingSchemaAction,
  313. "#2 Missing Schme action default to Add");
  314. Assert.AreSame (selCmd, adapter.SelectCommand,
  315. "#3 Select Command shud be a ref to the arg passed");
  316. conn = new SqlConnection (connectionString);
  317. String selStr = "Select * from numeric_family";
  318. adapter = new SqlDataAdapter (selStr, conn);
  319. Assert.AreEqual (MissingMappingAction.Passthrough,
  320. adapter.MissingMappingAction,
  321. "#4 Missing Mapping acttion default to Passthrough");
  322. Assert.AreEqual (MissingSchemaAction.Add,
  323. adapter.MissingSchemaAction,
  324. "#5 Missing Schme action default to Add");
  325. Assert.AreSame (selStr, adapter.SelectCommand.CommandText,
  326. "#6 Select Command shud be a ref to the arg passed");
  327. Assert.AreSame (conn, adapter.SelectCommand.Connection,
  328. "#7 cmd.connection shud be t ref to connection obj");
  329. selStr = "Select * from numeric_family";
  330. adapter = new SqlDataAdapter (selStr, connectionString);
  331. Assert.AreEqual (MissingMappingAction.Passthrough,
  332. adapter.MissingMappingAction,
  333. "#8 Missing Mapping action shud default to Passthrough");
  334. Assert.AreEqual (MissingSchemaAction.Add,
  335. adapter.MissingSchemaAction,
  336. "#9 Missing Schema action shud default to Add");
  337. Assert.AreSame (selStr,
  338. adapter.SelectCommand.CommandText,
  339. "#10");
  340. Assert.AreEqual (connectionString,
  341. adapter.SelectCommand.Connection.ConnectionString,
  342. "#11 ");
  343. }
  344. [Test]
  345. public void Fill_Test_ConnState ()
  346. {
  347. //Check if Connection State is maintained correctly ..
  348. data = new DataSet ("test1");
  349. adapter = new SqlDataAdapter ("select id from numeric_family where id=1",
  350. connectionString);
  351. SqlCommand cmd = adapter.SelectCommand ;
  352. Assert.AreEqual (ConnectionState.Closed,
  353. cmd.Connection.State, "#1 Connection shud be in closed state");
  354. adapter.Fill (data);
  355. Assert.AreEqual (1, data.Tables.Count, "#2 One table shud be populated");
  356. Assert.AreEqual (ConnectionState.Closed, cmd.Connection.State,
  357. "#3 Connection shud be closed state");
  358. data = new DataSet ("test2");
  359. cmd.Connection.Open ();
  360. Assert.AreEqual (ConnectionState.Open, cmd.Connection.State,
  361. "#3 Connection shud be open");
  362. adapter.Fill (data);
  363. Assert.AreEqual (1, data.Tables.Count, "#4 One table shud be populated");
  364. Assert.AreEqual (ConnectionState.Open, cmd.Connection.State,
  365. "#5 Connection shud be open");
  366. cmd.Connection.Close ();
  367. // Test if connection is closed when exception occurs
  368. cmd.CommandText = "select id1 from numeric_family";
  369. try {
  370. adapter.Fill (data);
  371. } catch {
  372. if (cmd.Connection.State == ConnectionState.Open) {
  373. cmd.Connection.Close ();
  374. Assert.Fail ("# Connection Shud be Closed");
  375. }
  376. }
  377. }
  378. [Test]
  379. public void Fill_Test_Data ()
  380. {
  381. //Check if a table is created for each resultset
  382. String batchQuery = "Select id,type_bit,type_int from numeric_family;";
  383. batchQuery += "Select type_bit from numeric_family";
  384. adapter = new SqlDataAdapter (batchQuery, connectionString);
  385. data = new DataSet ("test1");
  386. adapter.Fill (data);
  387. Assert.AreEqual (2, data.Tables.Count,"#1 2 Table shud be created");
  388. //Check if Table and Col are named correctly for unnamed columns
  389. string query = "Select 10,20 from numeric_family;" ;
  390. query += "Select 10,20 from numeric_family";
  391. adapter = new SqlDataAdapter (query, connectionString);
  392. data = new DataSet ("test2");
  393. adapter.Fill (data);
  394. Assert.AreEqual (2, data.Tables.Count,
  395. "#2 2 Tables shud be created");
  396. Assert.AreEqual ("Table", data.Tables[0].TableName, "#3");
  397. Assert.AreEqual ("Table1", data.Tables[1].TableName, "#4");
  398. Assert.AreEqual ("Column1", data.Tables[0].Columns[0].ColumnName, "#5");
  399. Assert.AreEqual ("Column2", data.Tables[0].Columns[1].ColumnName, "#6");
  400. Assert.AreEqual ("Column1", data.Tables[1].Columns[0].ColumnName, "#7");
  401. Assert.AreEqual ("Column2", data.Tables[1].Columns[1].ColumnName, "#8");
  402. //Check if dup columns are named correctly
  403. query = "select A.id ,B.id , C.id from numeric_family A, ";
  404. query += "numeric_family B , numeric_family C";
  405. adapter = new SqlDataAdapter (query, connectionString);
  406. data = new DataSet ("test3");
  407. adapter.Fill (data);
  408. // NOTE msdotnet contradicts documented behavior
  409. // as per documentation the column names should be
  410. // id1,id2,id3 .. but msdotnet returns id,id1,id2
  411. Assert.AreEqual ("id", data.Tables[0].Columns[0].ColumnName,
  412. "#9 if colname is duplicated ,shud be col,col1,col2 etc");
  413. Assert.AreEqual ("id1", data.Tables[0].Columns[1].ColumnName,
  414. "#10 if colname is duplicated ,shud be col,col1,col2 etc");
  415. Assert.AreEqual ("id2", data.Tables[0].Columns[2].ColumnName,
  416. "#11 if colname is duplicated ,shud be col,col1,col2 etc");
  417. // Test if tables are created and named accordingly ,
  418. // but only for those queries returning result sets
  419. query = "update numeric_family set id=100 where id=50;";
  420. query += "select * from numeric_family";
  421. adapter = new SqlDataAdapter (query, connectionString);
  422. data = new DataSet ("test4");
  423. adapter.Fill (data);
  424. Assert.AreEqual (1 ,data.Tables.Count,
  425. "#12 Tables shud be named only for queries returning a resultset");
  426. Assert.AreEqual ("Table", data.Tables[0].TableName,
  427. "#13 The first resutlset shud have 'Table' as its name");
  428. // Test behavior with an outerjoin
  429. query = "select A.id,B.type_bit from numeric_family A LEFT OUTER JOIN ";
  430. query += "numeric_family B on A.id = B.type_bit";
  431. adapter = new SqlDataAdapter (query, connectionString);
  432. data = new DataSet ("test5");
  433. adapter.Fill (data);
  434. Assert.AreEqual (0, data.Tables[0].PrimaryKey.Length,
  435. "#14 Primary Key shudnt be set if an outer join is performed");
  436. Assert.AreEqual (0, data.Tables[0].Constraints.Count,
  437. "#15 Constraints shudnt be set if an outer join is performed");
  438. adapter = new SqlDataAdapter ("select id from numeric_family",
  439. connectionString);
  440. data = new DataSet ("test6");
  441. adapter.Fill (data, 1, 1, "numeric_family");
  442. Assert.AreEqual (1, data.Tables[0].Rows.Count, "#16");
  443. Assert.AreEqual (2, data.Tables[0].Rows[0][0], "#17");
  444. // only one test for DataTable.. DataSet tests covers others
  445. adapter = new SqlDataAdapter ("select id from numeric_family",
  446. connectionString);
  447. DataTable table = new DataTable ("table1");
  448. adapter.Fill (table);
  449. Assert.AreEqual (4, table.Rows.Count , "#18");
  450. }
  451. [Test]
  452. public void Fill_Test_PriKey ()
  453. {
  454. // Test if Primary Key & Constraints Collection is correct
  455. adapter = new SqlDataAdapter ("select id,type_bit from numeric_family",
  456. connectionString);
  457. adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
  458. data = new DataSet ("test1");
  459. adapter.Fill (data);
  460. Assert.AreEqual (1, data.Tables[0].PrimaryKey.Length,
  461. "#1 Primary Key shud be set");
  462. Assert.AreEqual (1, data.Tables[0].Constraints.Count,
  463. "#2 Constraints shud be set");
  464. Assert.AreEqual (4, data.Tables[0].Rows.Count,
  465. "#3 No Of Rows shud be 4");
  466. // Test if data is correctly merged
  467. adapter.Fill (data);
  468. Assert.AreEqual (4, data.Tables[0].Rows.Count,
  469. "#4 No of Row shud still be 4");
  470. // Test if rows are appended and not merged
  471. // when primary key is not returned in the result-set
  472. string query = "Select type_int from numeric_family";
  473. adapter.SelectCommand.CommandText = query;
  474. data = new DataSet ("test2");
  475. adapter.Fill (data);
  476. Assert.AreEqual (4, data.Tables[0].Rows.Count,
  477. "#5 No of Rows shud be 4");
  478. adapter.Fill (data);
  479. Assert.AreEqual (8, data.Tables[0].Rows.Count,
  480. "#6 No of Rows shud double now");
  481. }
  482. [Test]
  483. public void Fill_Test_Exceptions ()
  484. {
  485. adapter = new SqlDataAdapter ("select * from numeric_family",
  486. connectionString);
  487. data = new DataSet ("test1");
  488. try {
  489. adapter.Fill (data, -1, 0, "numeric_family");
  490. Assert.Fail ("#1 Exception shud be thrown:Incorrect Arguments");
  491. }catch (AssertionException e){
  492. throw e;
  493. }catch (Exception e){
  494. Assert.AreEqual (typeof(ArgumentException), e.GetType(),
  495. "#2 Incorrect Exception : " + e);
  496. }
  497. // conn is not closed due to a bug..
  498. // can be removed later
  499. adapter.SelectCommand.Connection.Close ();
  500. try {
  501. adapter.Fill (data , 0 , -1 , "numeric_family");
  502. Assert.Fail ("#3 Exception shud be thrown:Incorrect Arguments");
  503. }catch (AssertionException e){
  504. throw e;
  505. }catch (Exception e){
  506. Assert.AreEqual (typeof(ArgumentException), e.GetType(),
  507. "#4 Incorrect Exception : " + e);
  508. }
  509. // conn is curr not closed.. can be removed later
  510. adapter.SelectCommand.Connection.Close ();
  511. /*
  512. // NOTE msdotnet contradicts documented behavior
  513. // InvalidOperationException is expected if table is not valid
  514. try {
  515. adapter.Fill (data , 0 , 0 , "invalid_talbe_name");
  516. }catch (InvalidOperationException e) {
  517. ex= e;
  518. }catch (Exception e){
  519. Assert.Fail ("#5 Exception shud be thrown : incorrect arugments ");
  520. }
  521. Assert.IsNotNull (ex , "#6 Exception shud be thrown : incorrect args ");
  522. adapter.SelectCommand.Connection.Close (); // tmp .. can be removed once the bug if fixed
  523. ex=null;
  524. */
  525. try {
  526. adapter.Fill ( null , 0 , 0 , "numeric_family");
  527. Assert.Fail ( "#7 Exception shud be thrown : Invalid Dataset");
  528. }catch (AssertionException e){
  529. throw e ;
  530. }catch (ArgumentNullException) {
  531. }catch (Exception e) {
  532. Assert.AreEqual (typeof(SystemException), e.GetType(),
  533. "#8 Incorrect Exception : " + e);
  534. }
  535. // conn is currently not being closed..
  536. //need to be removed once behavior is fixed
  537. adapter.SelectCommand.Connection.Close ();
  538. adapter.SelectCommand.Connection = null;
  539. try {
  540. adapter.Fill (data);
  541. Assert.Fail ("#9 Exception shud be thrown : Invalid Connection");
  542. }catch (AssertionException e){
  543. throw e;
  544. }catch (Exception e){
  545. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  546. "#10 Incorrect Exception : " + e);
  547. }
  548. }
  549. bool FillErrorContinue = false;
  550. [Test]
  551. public void Fill_Test_FillErrorTest ()
  552. {
  553. string query = "select type_int from numeric_family where id=1 or id=4 ";
  554. DataSet ds = new DataSet ();
  555. DataTable table = ds.Tables.Add ("test");
  556. table.Columns.Add ("col", typeof (short));
  557. adapter = new SqlDataAdapter (query, connectionString);
  558. DataTableMapping mapping = adapter.TableMappings.Add ("numeric_family", "test");
  559. mapping.ColumnMappings.Add ("type_int", "col");
  560. try {
  561. adapter.Fill (ds, "numeric_family");
  562. Assert.Fail ("#A1");
  563. } catch (OverflowException) {
  564. } catch (ArgumentException ex) {
  565. // System.OverflowException: Value was either too large or too
  566. // small for an Int16
  567. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  568. #if NET_2_0
  569. Assert.IsNotNull (ex.InnerException, "#A3");
  570. #else
  571. Assert.IsNull (ex.InnerException, "#A3");
  572. #endif
  573. Assert.IsNotNull (ex.Message, "#A4");
  574. Assert.IsNull (ex.ParamName, "#A5");
  575. #if NET_2_0
  576. OverflowException inner = ex.InnerException as OverflowException;
  577. Assert.IsNotNull (inner, "#A6");
  578. Assert.AreEqual (typeof (OverflowException), inner.GetType (), "#A7");
  579. Assert.IsNull (inner.InnerException, "#A8");
  580. Assert.IsNotNull (inner.Message, "#A9");
  581. #endif
  582. }
  583. Assert.AreEqual (0, ds.Tables [0].Rows.Count, "#A10");
  584. adapter.FillError += new FillErrorEventHandler (ErrorHandler);
  585. FillErrorContinue = false;
  586. try {
  587. adapter.Fill (ds, "numeric_family");
  588. Assert.Fail ("#B1");
  589. } catch (OverflowException) {
  590. } catch (ArgumentException ex) {
  591. // System.OverflowException: Value was either too large or too
  592. // small for an Int16
  593. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  594. #if NET_2_0
  595. Assert.IsNotNull (ex.InnerException, "#B3");
  596. #else
  597. Assert.IsNull (ex.InnerException, "#B3");
  598. #endif
  599. Assert.IsNotNull (ex.Message, "#B4");
  600. Assert.IsNull (ex.ParamName, "#B5");
  601. #if NET_2_0
  602. OverflowException inner = ex.InnerException as OverflowException;
  603. Assert.IsNotNull (inner, "#B6");
  604. Assert.AreEqual (typeof (OverflowException), inner.GetType (), "#B7");
  605. Assert.IsNull (inner.InnerException, "#B8");
  606. Assert.IsNotNull (inner.Message, "#B9");
  607. #endif
  608. }
  609. Assert.AreEqual (0, ds.Tables [0].Rows.Count, "#B10");
  610. FillErrorContinue = true;
  611. int count = adapter.Fill (ds, "numeric_family");
  612. Assert.AreEqual (1, ds.Tables [0].Rows.Count, "#C1");
  613. Assert.AreEqual (1, count, "#C2");
  614. }
  615. void ErrorHandler (object sender, FillErrorEventArgs args)
  616. {
  617. args.Continue = FillErrorContinue;
  618. }
  619. [Test]
  620. public void GetFillParametersTest ()
  621. {
  622. string query = "select id, type_bit from numeric_family where id > @param1";
  623. adapter = new SqlDataAdapter (query, connectionString);
  624. IDataParameter[] param = adapter.GetFillParameters ();
  625. Assert.AreEqual (0, param.Length, "#1 size shud be 0");
  626. SqlParameter param1 = new SqlParameter ();
  627. param1.ParameterName = "@param1";
  628. param1.Value = 2;
  629. adapter.SelectCommand.Parameters.Add (param1);
  630. param = adapter.GetFillParameters ();
  631. Assert.AreEqual (1, param.Length, "#2 count shud be 1");
  632. Assert.AreEqual (param1, param[0], "#3 Params shud be equal");
  633. }
  634. [Test]
  635. public void FillSchemaTest ()
  636. {
  637. string query;
  638. // Test if connection is closed if excepton occurs during fill schema
  639. query = "select * from invalid_table";
  640. adapter = new SqlDataAdapter (query, connectionString);
  641. data = new DataSet ("test");
  642. try {
  643. adapter.FillSchema (data , SchemaType.Source);
  644. } catch {
  645. if (adapter.SelectCommand.Connection.State != ConnectionState.Closed) {
  646. Assert.Fail ("#0 Conn shud be closed if exception occurs");
  647. adapter.SelectCommand.Connection.Close();
  648. }
  649. }
  650. // Test Primary Key is set (since primary key column returned)
  651. query = "select id, type_int from numeric_family where id=1";
  652. adapter = new SqlDataAdapter (query, connectionString);
  653. data = new DataSet ("test1");
  654. adapter.FillSchema (data , SchemaType.Source);
  655. Assert.AreEqual (1, data.Tables[0].PrimaryKey.Length,
  656. "#1 Primary Key property must be set");
  657. // Test Primary Key is not set (since primary key column is returned)
  658. query = "select type_bit, type_int from numeric_family where id=1";
  659. adapter = new SqlDataAdapter (query, connectionString);
  660. data = new DataSet ("test2");
  661. adapter.FillSchema (data, SchemaType.Source);
  662. Assert.AreEqual (0, data.Tables[0].PrimaryKey.Length,
  663. "#2 Primary Key property should not be set");
  664. // Test multiple tables are created for a batch query
  665. query = "Select id ,type_bit from numeric_family;" ;
  666. query += "Select id,type_bit,type_int from numeric_family;";
  667. data = new DataSet ("test3");
  668. adapter = new SqlDataAdapter (query, connectionString);
  669. adapter.FillSchema (data , SchemaType.Source);
  670. Assert.AreEqual (2 , data.Tables.Count , "#3 A table shud be created for each Result Set");
  671. Assert.AreEqual (2 , data.Tables[0].Columns.Count , "#4 should have 2 columns");
  672. Assert.AreEqual (3 , data.Tables[1].Columns.Count , "#5 Should have 3 columns");
  673. // Test if table names and column names are filled correctly
  674. query = "select 10,20 from numeric_family;" ;
  675. query += "select 10,20 from numeric_family;";
  676. adapter = new SqlDataAdapter (query, connectionString);
  677. data = new DataSet ("test4");
  678. try {
  679. adapter.FillSchema (data , SchemaType.Source);
  680. }catch (Exception e){
  681. Assert.Fail ("#3 Unexpected Exception : " + e);
  682. }
  683. Assert.AreEqual ( "Table", data.Tables[0].TableName);
  684. Assert.AreEqual ( "Table1", data.Tables[1].TableName);
  685. Assert.AreEqual ( "Column1", data.Tables[0].Columns[0].ColumnName,
  686. "#6 Unnamed col shud be named as 'ColumnN'");
  687. Assert.AreEqual ( "Column2", data.Tables[0].Columns[1].ColumnName,
  688. "#7 Unnamed col shud be named as 'ColumnN'");
  689. Assert.AreEqual ( "Column1", data.Tables[1].Columns[0].ColumnName,
  690. "#8 Unnamed col shud be named as 'ColumnN'");
  691. Assert.AreEqual ( "Column2", data.Tables[1].Columns[1].ColumnName,
  692. "#9 Unnamed col shud be named as 'ColumnN'");
  693. Assert.AreEqual (ConnectionState.Closed, adapter.SelectCommand.Connection.State,
  694. "#10 Connection shud be closed");
  695. // Test if mapping works correctly
  696. // doesent work in both mono and msdotnet
  697. // gotto check if something is wrong
  698. /*
  699. query = "select id,type_bit from numeric_family";
  700. adapter = new SqlDataAdapter (query, connectionString);
  701. data = new DataSet ("test");
  702. DataTable table = data.Tables.Add ("numeric_family_1");
  703. table.Columns.Add ("id");
  704. table.Columns.Add ("type_bit");
  705. DataTableMapping map = adapter.TableMappings.Add("numeric_family_1",
  706. "numeric_family");
  707. map.ColumnMappings.Add ("id", "id_1");
  708. map.ColumnMappings.Add ("type_bit", "type_bit_1");
  709. adapter.FillSchema (data, SchemaType.Source, "numeric_family");
  710. foreach (DataTable tab in data.Tables){
  711. Console.WriteLine ("Table == {0}",tab.TableName);
  712. foreach (DataColumn col in tab.Columns)
  713. Console.WriteLine (" Col = {0} " , col.ColumnName);
  714. }
  715. */
  716. }
  717. [Test]
  718. public void MissingSchemaActionTest ()
  719. {
  720. adapter = new SqlDataAdapter (
  721. "select id,type_bit,type_int from numeric_family where id<=4",
  722. connectionString);
  723. data = new DataSet ();
  724. Assert.AreEqual (MissingSchemaAction.Add, adapter.MissingSchemaAction,
  725. "#1 Default Value");
  726. adapter.Fill (data);
  727. Assert.AreEqual (1, data.Tables.Count , "#1 One table shud be populated");
  728. Assert.AreEqual (3, data.Tables[0].Columns.Count, "#2 Missing cols are added");
  729. Assert.AreEqual (0, data.Tables[0].PrimaryKey.Length, "#3 Default Value");
  730. adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
  731. data.Reset();
  732. adapter.Fill (data);
  733. Assert.AreEqual (3, data.Tables[0].Columns.Count,
  734. "#4 Missing cols are added");
  735. Assert.AreEqual (1, data.Tables[0].PrimaryKey.Length, "#5 Default Value");
  736. adapter.MissingSchemaAction = MissingSchemaAction.Ignore ;
  737. data.Reset ();
  738. adapter.Fill (data);
  739. Assert.AreEqual (0, data.Tables.Count, "#6 Data shud be ignored");
  740. adapter.MissingSchemaAction = MissingSchemaAction.Error ;
  741. data.Reset();
  742. try {
  743. adapter.Fill (data);
  744. Assert.Fail ("#8 Exception shud be thrown: Schema Mismatch");
  745. } catch (InvalidOperationException ex) {
  746. Assert.AreEqual (typeof(InvalidOperationException), ex.GetType(),
  747. "#9");
  748. }
  749. // Test for invalid MissingSchema Value
  750. try {
  751. adapter.MissingSchemaAction = (MissingSchemaAction)(-5000);
  752. Assert.Fail ("#10 Exception shud be thrown: Invalid Value");
  753. #if NET_2_0
  754. } catch (ArgumentOutOfRangeException ex) {
  755. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#11");
  756. }
  757. #else
  758. } catch (ArgumentException ex) {
  759. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#11");
  760. }
  761. #endif
  762. // Tests if Data is filled correctly if schema is defined
  763. // manually and MissingSchemaAction.Error is set
  764. adapter.MissingSchemaAction = MissingSchemaAction.Error;
  765. data.Reset();
  766. DataTable table = data.Tables.Add ("Table");
  767. table.Columns.Add ("id");
  768. table.Columns.Add ("type_bit");
  769. table.Columns.Add ("type_int");
  770. adapter.Fill (data);
  771. Assert.AreEqual (1, data.Tables.Count, "#12");
  772. Assert.AreEqual (4, data.Tables[0].Rows.Count, "#13");
  773. }
  774. [Test]
  775. public void MissingMappingActionTest ()
  776. {
  777. adapter = new SqlDataAdapter ("select id,type_bit from numeric_family where id=1",
  778. connectionString);
  779. data = new DataSet ();
  780. Assert.AreEqual (adapter.MissingMappingAction,
  781. MissingMappingAction.Passthrough,
  782. "#1 Default Value");
  783. adapter.Fill(data);
  784. Assert.AreEqual (1, data.Tables.Count,
  785. "#2 One Table shud be created");
  786. Assert.AreEqual (2, data.Tables[0].Columns.Count,
  787. "#3 Two Cols shud be created");
  788. adapter.MissingMappingAction = MissingMappingAction.Ignore;
  789. data.Reset ();
  790. adapter.Fill (data);
  791. Assert.AreEqual (0, data.Tables.Count, "#4 No table shud be created");
  792. adapter.MissingMappingAction = MissingMappingAction.Error;
  793. data.Reset ();
  794. try {
  795. adapter.Fill (data);
  796. Assert.Fail ("#5 Exception shud be thrown : Mapping is missing");
  797. } catch (InvalidOperationException ex) {
  798. Assert.AreEqual (typeof(InvalidOperationException), ex.GetType(),
  799. "#6");
  800. }
  801. try {
  802. adapter.MissingMappingAction = (MissingMappingAction)(-5000);
  803. Assert.Fail ("#7 Exception shud be thrown : Invalid Value");
  804. #if NET_2_0
  805. } catch (ArgumentOutOfRangeException ex) {
  806. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (),
  807. "#8");
  808. }
  809. #else
  810. } catch (ArgumentException ex) {
  811. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#8");
  812. }
  813. #endif
  814. // Test if mapping the column and table names works correctly
  815. adapter.MissingMappingAction = MissingMappingAction.Error;
  816. data.Reset ();
  817. DataTable table = data.Tables.Add ("numeric_family_1");
  818. table.Columns.Add ("id_1");
  819. table.Columns.Add ("type_bit_1");
  820. table.Columns.Add ("type_int_1");
  821. DataTableMapping tableMap = adapter.TableMappings.Add ("numeric_family",
  822. "numeric_family_1");
  823. tableMap.ColumnMappings.Add ("id", "id_1");
  824. tableMap.ColumnMappings.Add ("type_bit", "type_bit_1");
  825. tableMap.ColumnMappings.Add ("type_int", "type_int_1");
  826. adapter.Fill (data,"numeric_family");
  827. Assert.AreEqual (1, data.Tables.Count ,
  828. "#8 The DataTable shud be correctly mapped");
  829. Assert.AreEqual (3, data.Tables[0].Columns.Count,
  830. "#9 The DataColumns shud be corectly mapped");
  831. Assert.AreEqual (1, data.Tables[0].Rows.Count,
  832. "#10 Data shud be populated if mapping is correct");
  833. }
  834. [Test] // bug #76433
  835. public void FillSchema_ValuesTest()
  836. {
  837. using (SqlConnection conn = new SqlConnection(connectionString)) {
  838. conn.Open();
  839. IDbCommand command = conn.CreateCommand();
  840. // Create Temp Table
  841. String cmd = "Create Table #tmp_TestTable (" ;
  842. cmd += "Field1 DECIMAL (10) NOT NULL,";
  843. cmd += "Field2 DECIMAL(19))";
  844. command.CommandText = cmd;
  845. command.ExecuteNonQuery();
  846. DataSet dataSet = new DataSet();
  847. string selectString = "SELECT * FROM #tmp_TestTable";
  848. IDbDataAdapter dataAdapter = new SqlDataAdapter (
  849. selectString, conn);
  850. dataAdapter.FillSchema(dataSet, SchemaType.Mapped);
  851. Assert.AreEqual (1, dataSet.Tables.Count, "#1");
  852. Assert.IsFalse (dataSet.Tables[0].Columns[0].AllowDBNull,"#2");
  853. Assert.IsTrue (dataSet.Tables[0].Columns[1].AllowDBNull,"#3");
  854. }
  855. }
  856. [Test]
  857. public void Fill_CheckSchema ()
  858. {
  859. using (SqlConnection conn = new SqlConnection(connectionString)) {
  860. conn.Open();
  861. IDbCommand command = conn.CreateCommand();
  862. // Create Temp Table
  863. String cmd = "Create Table #tmp_TestTable (" ;
  864. cmd += "id int primary key,";
  865. cmd += "field int not null)";
  866. command.CommandText = cmd;
  867. command.ExecuteNonQuery();
  868. DataSet dataSet = new DataSet();
  869. string selectString = "SELECT * from #tmp_TestTable";
  870. IDbDataAdapter dataAdapter = new SqlDataAdapter (
  871. selectString,conn);
  872. dataAdapter.Fill (dataSet);
  873. Assert.AreEqual (1, dataSet.Tables.Count, "#A1");
  874. Assert.AreEqual (2, dataSet.Tables [0].Columns.Count, "#A2");
  875. Assert.IsTrue (dataSet.Tables [0].Columns [1].AllowDBNull, "#A3");
  876. Assert.AreEqual (0, dataSet.Tables [0].PrimaryKey.Length, "#A4");
  877. dataSet.Reset ();
  878. dataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
  879. dataAdapter.Fill (dataSet);
  880. Assert.AreEqual (1, dataSet.Tables.Count, "#B1");
  881. Assert.AreEqual (2, dataSet.Tables [0].Columns.Count, "#B2");
  882. Assert.IsFalse (dataSet.Tables [0].Columns [1].AllowDBNull, "#B3");
  883. if (ClientVersion == 7)
  884. Assert.AreEqual (0, dataSet.Tables [0].PrimaryKey.Length, "#B4");
  885. else
  886. Assert.AreEqual (1, dataSet.Tables [0].PrimaryKey.Length, "#B4");
  887. }
  888. }
  889. [Test]
  890. public void FillSchema_CheckSchema ()
  891. {
  892. using (SqlConnection conn = new SqlConnection(connectionString)) {
  893. conn.Open();
  894. IDbCommand command = conn.CreateCommand();
  895. // Create Temp Table
  896. String cmd = "Create Table #tmp_TestTable (" ;
  897. cmd += "id int primary key,";
  898. cmd += "field int not null)";
  899. command.CommandText = cmd;
  900. command.ExecuteNonQuery();
  901. DataSet dataSet = new DataSet();
  902. string selectString = "SELECT * from #tmp_TestTable";
  903. IDbDataAdapter dataAdapter = new SqlDataAdapter (
  904. selectString,conn);
  905. dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
  906. Assert.IsFalse (dataSet.Tables[0].Columns[1].AllowDBNull, "#1");
  907. dataSet.Reset ();
  908. dataAdapter.MissingSchemaAction = MissingSchemaAction.Add;
  909. dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
  910. Assert.IsFalse (dataSet.Tables[0].Columns[1].AllowDBNull, "#2");
  911. dataSet.Reset ();
  912. dataAdapter.MissingSchemaAction = MissingSchemaAction.Ignore;
  913. dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
  914. Assert.AreEqual (0, dataSet.Tables.Count, "#3");
  915. dataSet.Reset ();
  916. dataAdapter.MissingSchemaAction = MissingSchemaAction.Error;
  917. try {
  918. dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
  919. Assert.Fail ("#4 Error should be thrown");
  920. } catch (InvalidOperationException ex) {
  921. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#4");
  922. }
  923. }
  924. }
  925. [Test]
  926. public void CreateViewSSPITest ()
  927. {
  928. SqlConnection conn = new SqlConnection (ConfigurationSettings.AppSettings ["SSPIConnString"]);
  929. conn.Open ();
  930. string sql = "create view MONO_TEST_VIEW as select * from Numeric_family";
  931. SqlCommand dbcmd = new SqlCommand( sql, conn );
  932. dbcmd.ExecuteNonQuery();
  933. sql = "drop view MONO_TEST_VIEW";
  934. dbcmd = new SqlCommand( sql, conn );
  935. dbcmd.ExecuteNonQuery();
  936. conn.Close();
  937. }
  938. [Test]
  939. public void Fill_RelatedTables ()
  940. {
  941. SqlConnection conn = new SqlConnection(connectionString);
  942. using (conn) {
  943. conn.Open();
  944. IDbCommand command = conn.CreateCommand();
  945. DataSet dataSet = new DataSet();
  946. string selectString = "SELECT id, type_int from numeric_family where id < 3";
  947. DbDataAdapter dataAdapter = new SqlDataAdapter (selectString,conn);
  948. DataTable table2 = dataSet.Tables.Add ("table2");
  949. DataColumn ccol1 = table2.Columns.Add ("id", typeof (int));
  950. DataColumn ccol2 = table2.Columns.Add ("type_int", typeof (int));
  951. DataTable table1 = dataSet.Tables.Add ("table1");
  952. DataColumn pcol1 = table1.Columns.Add ("id", typeof (int));
  953. DataColumn pcol2 = table1.Columns.Add ("type_int", typeof (int));
  954. table2.Constraints.Add ("fk", pcol1, ccol1);
  955. //table1.Constraints.Add ("fk1", pcol2, ccol2);
  956. dataSet.EnforceConstraints = false;
  957. dataAdapter.Fill (dataSet, "table1");
  958. dataAdapter.Fill (dataSet, "table2");
  959. //Should not throw an exception
  960. dataSet.EnforceConstraints = true;
  961. Assert.AreEqual (2, table1.Rows.Count, "#1");
  962. Assert.AreEqual (2, table2.Rows.Count, "#2");
  963. }
  964. }
  965. #if NET_2_0
  966. [Test]
  967. public void UpdateBatchSizeTest ()
  968. {
  969. adapter = new SqlDataAdapter();
  970. Assert.AreEqual (1, adapter.UpdateBatchSize, "#1 The default value should be 1");
  971. adapter.UpdateBatchSize = 3;
  972. Assert.AreEqual (3, adapter.UpdateBatchSize, "#2 The value should be 3 after setting the property UpdateBatchSize to 3");
  973. }
  974. [Test]
  975. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  976. public void UpdateBatchSizeArgumentOutOfRangeTest ()
  977. {
  978. adapter = new SqlDataAdapter();
  979. adapter.UpdateBatchSize = -2;
  980. }
  981. #endif
  982. int ClientVersion {
  983. get {
  984. return (engine.ClientVersion);
  985. }
  986. }
  987. }
  988. #if NET_2_0
  989. [TestFixture]
  990. [Category ("sqlserver")]
  991. public class SqlDataAdapterInheritTest : DbDataAdapter
  992. {
  993. SqlConnection conn = null;
  994. [Test]
  995. public void FillDataAdapterTest () {
  996. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  997. try {
  998. ConnectionManager.Singleton.OpenConnection ();
  999. DataTable dt = new DataTable();
  1000. SqlCommand command = new SqlCommand ();
  1001. command.CommandText = "Select * from employee;";
  1002. command.Connection = conn;
  1003. SelectCommand = command;
  1004. Fill (dt, command.ExecuteReader ());
  1005. Assert.AreEqual (4, dt.Rows.Count, "#1");
  1006. Assert.AreEqual (6, dt.Columns.Count, "#2");
  1007. } finally {
  1008. DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
  1009. ConnectionManager.Singleton.CloseConnection ();
  1010. }
  1011. }
  1012. }
  1013. #endif
  1014. }