SqlDataAdapterTest.cs 38 KB

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