ADONetTesterClass.cs 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  1. // Authors:
  2. // Rafael Mizrahi <[email protected]>
  3. // Erez Lotan <[email protected]>
  4. // Oren Gurfinkel <[email protected]>
  5. // Ofer Borstein
  6. //
  7. // Copyright (c) 2004 Mainsoft Co.
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Data;
  30. using System.Data.OracleClient ;
  31. using System.IO;
  32. using System.Collections;
  33. using MonoTests.System.Data.Utils.Data;
  34. using Sys = System;
  35. namespace MonoTests.System.Data.Utils {
  36. public class ADONetTesterClass : GHTBase {
  37. #region " Base Constructors "
  38. protected ADONetTesterClass(Sys.IO.TextWriter Logger, bool LogOnSuccess) : base(Logger,LogOnSuccess){}
  39. protected ADONetTesterClass(bool LogOnSuccess):base(Console.Out, LogOnSuccess){}
  40. protected ADONetTesterClass():base(Console.Out, false){}
  41. #endregion
  42. private MonoTests.System.Data.Utils.DataBaseServer DBType ;
  43. #region "----------- Build Update Commands --------------"
  44. protected void OracleDataAdapter_BuildUpdateCommands(ref Sys.Data.OracleClient.OracleDataAdapter oleDBda) {
  45. Sys.Data.OracleClient.OracleConnection Conn = oleDBda.SelectCommand.Connection;
  46. oleDBda.DeleteCommand = new Sys.Data.OracleClient.OracleCommand();
  47. oleDBda.InsertCommand = new Sys.Data.OracleClient.OracleCommand();
  48. oleDBda.UpdateCommand = new Sys.Data.OracleClient.OracleCommand();
  49. oleDBda.DeleteCommand.Connection = Conn;
  50. oleDBda.InsertCommand.Connection = Conn;
  51. oleDBda.UpdateCommand.Connection = Conn;
  52. oleDBda.DeleteCommand.Parameters.Add(new Sys.Data.OracleClient.OracleParameter("EmployeeID",Sys.Data.OracleClient.OracleType.Int32)) ;
  53. oleDBda.DeleteCommand.Parameters["EmployeeID"].SourceVersion = DataRowVersion.Original;
  54. oleDBda.DeleteCommand.Parameters["EmployeeID"].SourceColumn = "EmployeeID";
  55. oleDBda.InsertCommand.Parameters.Add(new Sys.Data.OracleClient.OracleParameter("LastName",Sys.Data.OracleClient.OracleType.VarChar ,20));
  56. oleDBda.InsertCommand.Parameters.Add(new Sys.Data.OracleClient.OracleParameter("FirstName",Sys.Data.OracleClient.OracleType.VarChar,10));
  57. oleDBda.InsertCommand.Parameters.Add(new Sys.Data.OracleClient.OracleParameter("EmployeeID",Sys.Data.OracleClient.OracleType.Int32));
  58. oleDBda.InsertCommand.Parameters["EmployeeID"].SourceColumn = "EmployeeID";
  59. oleDBda.InsertCommand.Parameters["LastName"].SourceColumn = "LastName";
  60. oleDBda.InsertCommand.Parameters["FirstName"].SourceColumn = "FirstName";
  61. oleDBda.UpdateCommand.Parameters.Add(new Sys.Data.OracleClient.OracleParameter("Title",Sys.Data.OracleClient.OracleType.VarChar,30));
  62. oleDBda.UpdateCommand.Parameters.Add(new Sys.Data.OracleClient.OracleParameter("EmployeeID",Sys.Data.OracleClient.OracleType.Int32));
  63. oleDBda.UpdateCommand.Parameters["EmployeeID"].SourceColumn = "EmployeeID";
  64. oleDBda.UpdateCommand.Parameters["Title"].SourceColumn = "Title";
  65. //for Oracle, ODBC
  66. string deleteSQL = "DELETE FROM Employees WHERE EmployeeID = :EmployeeID";
  67. string insertSQL = "INSERT INTO Employees (EmployeeID, LastName, FirstName) VALUES (:EmployeeID, :LastName, :FirstName)";
  68. string updateSQL = "UPDATE Employees SET Title = :Title WHERE EmployeeID = :EmployeeID";
  69. oleDBda.DeleteCommand.CommandText = deleteSQL;
  70. oleDBda.InsertCommand.CommandText = insertSQL;
  71. oleDBda.UpdateCommand.CommandText = updateSQL;
  72. }
  73. #endregion
  74. #region "----------- Sys.Data.Common.DBDataAdapter --------------"
  75. #region " DBDataAdapter - Fill / Fill Schema "
  76. protected void DbDataAdapter_Fill_Ds(Sys.Data.Common.DbDataAdapter dbDA) {
  77. DataSet dsResult, dsExpected ;
  78. int ExpectedRowsCount,ResultRowsCount;
  79. DataSet ds = new DataSet();
  80. ExpectedRowsCount = ReadDBData_Fill(dbDA,ref ds,false);
  81. // create expected dataset to compare result to
  82. dsExpected = ds.Copy();
  83. //make some changes, the fill method will overides those changes with data from DB.
  84. foreach (DataRow dr in ds.Tables[0].Select())
  85. dr["Country"] = "NeverNeverLand";
  86. ds.Tables[0].Columns.Remove("HomePhone"); //remove column, this column will be addedd during the fill process
  87. //ds.Tables.Remove(ds.Tables[1]); //remove the table, this table will be addedd during the fill process
  88. ds.AcceptChanges();
  89. // create source dataset to be filled
  90. dsResult = ds.Copy();
  91. //execute fill
  92. ResultRowsCount = dbDA.Fill(dsResult);
  93. CompareResults_Fill(dsResult,dsExpected);
  94. //close connection
  95. if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
  96. ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
  97. }
  98. protected void DbDataAdapter_Fill_Dt(Sys.Data.Common.DbDataAdapter dbDA) {
  99. DataSet dsResult, dsExpected ;
  100. int ExpectedRowsCount,ResultRowsCount;
  101. DataSet ds = new DataSet();
  102. ExpectedRowsCount = ReadDBData_Fill(dbDA,ref ds, false);
  103. //ds.Tables.Remove(ds.Tables[1]); //remove the table, fill only one table
  104. // create expected dataset to compare result to
  105. dsExpected = ds.Copy();
  106. //make some changes, the fill method will overides those changes with data from DB.
  107. foreach (DataRow dr in ds.Tables[0].Select())
  108. dr["Country"] = "NeverNeverLand";
  109. ds.Tables[0].Columns.Remove("HomePhone"); //remove column, this column will be addedd during the fill process
  110. ds.AcceptChanges();
  111. // create source dataset to be filled
  112. dsResult = ds.Copy();
  113. //execute fill
  114. ResultRowsCount = dbDA.Fill(dsResult.Tables["Table"]);
  115. CompareResults_Fill(dsResult,dsExpected);
  116. //close connection
  117. if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
  118. ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
  119. }
  120. protected void DbDataAdapter_Fill_Ds_Str(Sys.Data.Common.DbDataAdapter dbDA) {
  121. DataSet dsResult, dsExpected ;
  122. int ExpectedRowsCount,ResultRowsCount;
  123. DataSet ds = new DataSet();
  124. ExpectedRowsCount = ReadDBData_Fill(dbDA,ref ds, false);
  125. // create expected dataset to compare result to
  126. dsExpected = ds.Copy();
  127. //make some changes, the fill method will overides those changes with data from DB.
  128. foreach (DataRow dr in ds.Tables[0].Select())
  129. dr["Country"] = "NeverNeverLand";
  130. ds.Tables[0].Columns.Remove("HomePhone"); //remove column, this column will be addedd during the fill process
  131. //ds.Tables.Remove(ds.Tables[1]); //remove the table, this table will be addedd during the fill process
  132. ds.AcceptChanges();
  133. // create source dataset to be filled
  134. dsResult = ds.Copy();
  135. //execute fill
  136. ResultRowsCount = dbDA.Fill(dsResult,dsResult.Tables[0].TableName );
  137. CompareResults_Fill(dsResult,dsExpected);
  138. //close connection
  139. if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
  140. ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
  141. }
  142. protected void DbDataAdapter_Fill_Ds_Int_Int_Str(Sys.Data.Common.DbDataAdapter dbDA) {
  143. DataSet dsResult, dsExpected ;
  144. int ExpectedRowsCount,ResultRowsCount;
  145. DataSet ds = new DataSet();
  146. ExpectedRowsCount = ReadDBData_Fill(dbDA,ref ds, false);
  147. // create expected dataset to compare result to
  148. dsExpected = ds.Copy();
  149. //make some changes, the fill method will overides those changes with data from DB.
  150. foreach (DataRow dr in ds.Tables[0].Select())
  151. dr["Country"] = "NeverNeverLand";
  152. ds.Tables[0].Columns.Remove("HomePhone"); //remove column, this column will be addedd during the fill process
  153. //ds.Tables.Remove(ds.Tables[1]); //remove the table, this table will be addedd during the fill process
  154. ds.AcceptChanges();
  155. // create source dataset to be filled
  156. dsResult = ds.Copy();
  157. //execute fill
  158. ResultRowsCount = dbDA.Fill(dsResult,0,0,dsResult.Tables[0].TableName);
  159. CompareResults_Fill(dsResult,dsExpected);
  160. dsResult = ds.Copy();
  161. //modify expected dataset to match the expected result
  162. for (int i=0; i < dsExpected.Tables[0].Rows.Count ; i++) {
  163. if (i < 5 || i > 14) {
  164. dsExpected.Tables[0].Rows[i]["Country"] = "NeverNeverLand";
  165. dsExpected.Tables[0].Rows[i]["HomePhone"] = DBNull.Value;
  166. }
  167. }
  168. ResultRowsCount = dbDA.Fill(dsResult,5,10,dsResult.Tables[0].TableName);
  169. CompareResults_Fill(dsResult,dsExpected);
  170. //close connection
  171. if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
  172. ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
  173. }
  174. protected void DbDataAdapter_FillSchema_Ds_SchemaType(Sys.Data.Common.DbDataAdapter dbDA) {
  175. DataSet dsResult = new DataSet();
  176. DataSet dsExpected = new DataSet();
  177. // create expected dataset to compare result to
  178. ReadDBData_Fill(dbDA,ref dsExpected ,true);
  179. // Note: When handling batch SQL statements that return multiple results,
  180. // the implementation of FillSchema for the .NET Framework Data Provider for OLEDB
  181. // retrieves schema information for only the first result.
  182. // To retrieve schema information for multiple results, use Fill with the MissingSchemaAction set to AddWithKey
  183. // if (dbDA.GetType() == typeof(Sys.Data.OracleClient.OracleDataAdapter))
  184. // dsExpected.Tables.Remove(dsExpected.Tables[1]);
  185. //execute FillSchema
  186. //dsResult = dsExpected.Copy();
  187. DataTable[] dtArr = dbDA.FillSchema(dsResult,SchemaType.Mapped );
  188. //************ Fix .Net bug? (FillSchema method add AutoIncrement=true) *******************
  189. dsResult.Tables[0].Columns["EmployeeID"].AutoIncrement = false;
  190. CompareResults_Fill(dsResult,dsExpected);
  191. Exception exp = null;
  192. try {
  193. BeginCase("Check return value - Table[0]");
  194. Compare(dtArr[0],dsResult.Tables[0] );
  195. }
  196. catch(Exception ex) {exp = ex;}
  197. finally {EndCase(exp); exp = null;}
  198. // if (dbDA.GetType() != typeof(Sys.Data.OracleClient.OracleDataAdapter))
  199. // try
  200. // {
  201. // BeginCase("Check return value - Table[1]");
  202. // Compare(dtArr[1],dsResult.Tables[1]);
  203. // }
  204. // catch(Exception ex) {exp = ex;}
  205. // finally {EndCase(exp); exp = null;}
  206. //close connection
  207. if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
  208. ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
  209. }
  210. protected void DbDataAdapter_FillSchema_Dt_SchemaType(Sys.Data.Common.DbDataAdapter dbDA) {
  211. DataSet dsResult = new DataSet();
  212. DataSet dsExpected = new DataSet();
  213. // create expected dataset to compare result to
  214. ReadDBData_Fill(dbDA,ref dsExpected ,true);
  215. //dsExpected.Tables.Remove(dsExpected.Tables[1]);
  216. //execute FillSchema
  217. dsResult.Tables.Add("Table");
  218. DataTable dt = dbDA.FillSchema(dsResult.Tables[0],SchemaType.Mapped );
  219. //************ Fix .Net bug? (FillSchema method add AutoIncrement=true) *******************
  220. dsResult.Tables[0].Columns["EmployeeID"].AutoIncrement = false;
  221. CompareResults_Fill(dsResult,dsExpected);
  222. Exception exp = null;
  223. try {
  224. BeginCase("Check return value - Table[0]");
  225. Compare(dt,dsResult.Tables[0] );
  226. }
  227. catch(Exception ex) {exp = ex;}
  228. finally {EndCase(exp); exp = null;}
  229. //close connection
  230. if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
  231. ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
  232. }
  233. protected void DbDataAdapter_FillSchema_Ds_SchemaType_Str(Sys.Data.Common.DbDataAdapter dbDA) {
  234. DataSet dsResult = new DataSet();
  235. DataSet dsExpected = new DataSet();
  236. // create expected dataset to compare result to
  237. ReadDBData_Fill(dbDA,ref dsExpected ,true);
  238. //dsExpected.Tables.Remove(dsExpected.Tables[1]);
  239. //execute FillSchema
  240. dsResult.Tables.Add("Table");
  241. DataTable[] dtArr = dbDA.FillSchema(dsResult,SchemaType.Mapped,dsResult.Tables[0].TableName);
  242. //************ Fix .Net bug? (FillSchema method add AutoIncrement=true) *******************
  243. dsResult.Tables[0].Columns["EmployeeID"].AutoIncrement = false;
  244. CompareResults_Fill(dsResult,dsExpected);
  245. Exception exp = null;
  246. try {
  247. BeginCase("Check return value - Table[0]");
  248. Compare(dtArr[0],dsResult.Tables[0] );
  249. }
  250. catch(Exception ex) {exp = ex;}
  251. finally {EndCase(exp); exp = null;}
  252. //close connection
  253. if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
  254. ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
  255. }
  256. private int ReadDBData_Fill(Sys.Data.Common.DbDataAdapter dbDA, ref DataSet ds, bool ReadSchemaOnly) {
  257. int ExpectedRowsCount = 0;
  258. IDbDataAdapter Ida = (IDbDataAdapter)dbDA;
  259. IDbCommand ICmd = Ida.SelectCommand;
  260. IDbConnection IConn = ICmd.Connection;
  261. IDataReader Idr;
  262. IConn.ConnectionString = MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString;
  263. ICmd.CommandText = "SELECT EmployeeID, LastName, FirstName, Title, Address, City, Region, Country, Extension, HomePhone FROM Employees " ;
  264. IConn.Open();
  265. //get db type
  266. DBType = ConnectedDataProvider.GetDbType(((IDbDataAdapter)dbDA).SelectCommand.Connection.ConnectionString);
  267. // Execute data Reader - Get Expected results
  268. Idr = ICmd.ExecuteReader();
  269. // create temp dataset to insert results
  270. ExpectedRowsCount = DataReaderFill_Fill(ref ds,ref Idr,ReadSchemaOnly);
  271. Idr.Close();
  272. return ExpectedRowsCount;
  273. }
  274. private void CompareResults_Fill(DataSet dsResult,DataSet dsExpected ) {
  275. Exception exp = null;
  276. // try
  277. // {
  278. // BeginCase("Compare Rows count");
  279. // // ??????? Fill return count for first table only ??????
  280. // Compare(ExpectedRowsCount ,ResultRowsCount );
  281. // }
  282. // catch(Exception ex) {exp = ex;}
  283. // finally {EndCase(exp); exp = null;}
  284. try {
  285. BeginCase("Compare data");
  286. Compare(dsResult.GetXml() ,dsExpected.GetXml());
  287. }
  288. catch(Exception ex) {exp = ex;}
  289. finally {EndCase(exp); exp = null;}
  290. try {
  291. BeginCase("Compare schema");
  292. Compare(dsResult.GetXmlSchema() ,dsExpected.GetXmlSchema());
  293. }
  294. catch(Exception ex) {exp = ex;}
  295. finally {EndCase(exp); exp = null;}
  296. }
  297. private int DataReaderFill_Fill(ref DataSet dsExpected, ref IDataReader Idr,bool ReadSchemaOnly) {
  298. bool blnNextResults;
  299. int RowsAffected = 0;
  300. object[] objArr = null;
  301. DataTable SchemaTable = null;
  302. do {
  303. SchemaTable = Idr.GetSchemaTable();
  304. //add new table with the right amount of columns, the first table must be named "Table"
  305. if (dsExpected.Tables.Count == 0)
  306. dsExpected.Tables.Add(new DataTable("Table"));
  307. else
  308. dsExpected.Tables.Add();
  309. for (int i = 0 ; i < Idr.FieldCount; i++) {
  310. dsExpected.Tables[dsExpected.Tables.Count-1].Columns.Add(new DataColumn(Idr.GetName(i),Idr.GetFieldType(i)));
  311. if (ReadSchemaOnly) { // add schema info
  312. dsExpected.Tables[dsExpected.Tables.Count-1].Columns[i].AllowDBNull = (bool)SchemaTable.Rows[i]["AllowDBNull"];
  313. dsExpected.Tables[dsExpected.Tables.Count-1].Columns[i].AutoIncrement = (bool)SchemaTable.Rows[i]["IsAutoIncrement"];
  314. dsExpected.Tables[dsExpected.Tables.Count-1].Columns[i].ReadOnly = (bool)SchemaTable.Rows[i]["IsReadOnly"];
  315. dsExpected.Tables[dsExpected.Tables.Count-1].Columns[i].Unique = (bool)SchemaTable.Rows[i]["IsUnique"];
  316. if (dsExpected.Tables[dsExpected.Tables.Count-1].Columns[i].DataType == typeof(string))
  317. dsExpected.Tables[dsExpected.Tables.Count-1].Columns[i].MaxLength = (int)SchemaTable.Rows[i]["ColumnSize"];
  318. }
  319. }
  320. if (!ReadSchemaOnly) {
  321. //array that holds the current rows values
  322. objArr = new object[Idr.FieldCount];
  323. //fill the new table
  324. while (Idr.Read()) {
  325. Idr.GetValues(objArr);
  326. //update existing row, if no row is found - add it as new row
  327. dsExpected.Tables[dsExpected.Tables.Count-1].LoadDataRow(objArr,false);
  328. RowsAffected++;
  329. }
  330. }
  331. //get next record set
  332. blnNextResults = Idr.NextResult();
  333. }
  334. while (blnNextResults);
  335. // add primary key, fill method will update existing rows instead of insert new ones
  336. dsExpected.Tables[0].PrimaryKey = new DataColumn[] {dsExpected.Tables[0].Columns["EmployeeID"]};
  337. //if (ReadSchemaOnly) dsExpected.Tables[1].PrimaryKey = new DataColumn[] {dsExpected.Tables[1].Columns["CustomerID"]};
  338. dsExpected.AcceptChanges();
  339. return RowsAffected;
  340. }
  341. #endregion
  342. #region " DBDataAdapter - FillError "
  343. private bool blnReadDBData_Fill = false;
  344. protected void DbDataAdapter_FillError(Sys.Data.Common.DbDataAdapter dbDA) {
  345. Exception exp = null;
  346. IDbDataAdapter Ida = (IDbDataAdapter)dbDA;
  347. IDbCommand ICmd = Ida.SelectCommand;
  348. IDbConnection IConn = ICmd.Connection;
  349. IConn.ConnectionString = MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString;
  350. ICmd.CommandText = "SELECT CustomerID, CompanyName, City, Country, Phone FROM Customers ";
  351. IConn.Open();
  352. DataSet ds = new DataSet();
  353. ds.Tables.Add(new DataTable("Customers"));
  354. ds.Tables[0].Columns.Add("CustomerID",typeof(byte));
  355. //check FillError event
  356. dbDA.FillError += new FillErrorEventHandler(dbDA_FillError);
  357. blnReadDBData_Fill = false;
  358. try {
  359. BeginCase("FillError");
  360. try {
  361. dbDA.Fill(ds,"Customers");
  362. }
  363. catch (Exception ){};
  364. Compare(blnReadDBData_Fill ,true );
  365. }
  366. catch(Exception ex) {exp = ex;}
  367. finally {EndCase(exp); exp = null;}
  368. dbDA.FillError -= new FillErrorEventHandler(dbDA_FillError);
  369. //close connection
  370. if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
  371. ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
  372. }
  373. private void dbDA_FillError(object sender, FillErrorEventArgs args) {
  374. blnReadDBData_Fill = true;
  375. args.Continue = false;
  376. }
  377. #endregion
  378. #region " DBDataAdapter - Update "
  379. protected void DbDataAdapter_Update_Ds(Sys.Data.Common.DbDataAdapter dbDA) {
  380. int NumberOfAffectedRows = 0;
  381. Exception exp = null;
  382. // --------- get data from DB -----------------
  383. DataSet ds = PrepareDBData_Update(dbDA);
  384. // --------- prepare dataset for update method -----------------
  385. DataSet dsDB1 = ds.Copy();
  386. // --------- prepare dataset for DBConcurrencyException -----------------
  387. DataSet dsDB2 = ds.Copy();
  388. //update expecteed dataset
  389. dsDB2.Tables[0].Rows.Add(new object[] {9994,"Ofer", "Borshtein", "Delete"});
  390. dsDB2.Tables[0].Rows.Add(new object[] {9995,"Ofer", "Borshtein", "Update"});
  391. dsDB2.Tables[0].Rows.Find(9996).Delete();
  392. dsDB2.AcceptChanges();
  393. //make changes to the DataBase (through the dataset)
  394. dsDB1.Tables[0].Rows.Add(new object[] {9991,"Ofer","Borshtein","Insert"});
  395. dsDB1.Tables[0].Rows.Find(9992).Delete();
  396. dsDB1.Tables[0].Rows.Find(9993)["Title"] = "Jack the ripper";
  397. //execute update to db
  398. NumberOfAffectedRows = dbDA.Update(dsDB1);
  399. try {
  400. BeginCase("Number Of Affected Rows");
  401. Compare(NumberOfAffectedRows ,3 );
  402. }
  403. catch(Exception ex) {exp = ex;}
  404. finally {EndCase(exp); exp = null;}
  405. //get result from db in order to check them
  406. DataSet dsExpected = new DataSet(); //ds.Reset();
  407. dbDA.Fill(dsExpected);
  408. dsExpected.Tables[0].PrimaryKey = new DataColumn[] {dsExpected.Tables[0].Columns["EmployeeID"]};
  409. CompareResults_Update(dsDB1,dsDB2,ref dbDA);
  410. CompareResults_Update_Ds_Exception(dsDB2,ref dbDA);
  411. //Create rows which not exists in the DB but exists in the DS with row state = deleted
  412. //this will cause the Update to fail.
  413. dsDB1.Tables[0].Rows.Add(new object[] {9997,"Ofer", "Borshtein", "Delete"});
  414. dsDB1.Tables[0].Rows.Add(new object[] {9998,"Ofer", "Borshtein", "Delete"});
  415. dsDB1.AcceptChanges();
  416. dsDB1.Tables[0].Rows.Find(9997).Delete();
  417. dsDB1.Tables[0].Rows.Find(9998).Delete();
  418. //Check Sys.Data.DBConcurrencyException
  419. //The exception that is thrown by the DataAdapter during the update operation if the number of rows affected equals zero.
  420. try {
  421. BeginCase("Check DBConcurrencyException");
  422. try {
  423. NumberOfAffectedRows = dbDA.Update(dsDB1);
  424. }
  425. catch (DBConcurrencyException ex) {exp=ex;}
  426. Compare(exp.GetType(),typeof(DBConcurrencyException) );
  427. exp = null;
  428. }
  429. catch(Exception ex) {exp = ex;}
  430. finally {EndCase(exp); exp = null;}
  431. //close connection
  432. if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
  433. ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
  434. }
  435. private void CompareResults_Update_Ds_Exception(DataSet dsResultException,ref Sys.Data.Common.DbDataAdapter dbDA) {
  436. int NumberOfAffectedRows = 0;
  437. Exception exp = null;
  438. Exception e = null;
  439. // --------- check for DBConcurrencyException /UniqueConstraint -----------------
  440. // call AcceptChanges after each exception check in order to make sure that we check only the the current row
  441. try {
  442. BeginCase("DBConcurrencyException - Delete");
  443. dsResultException.Tables[0].Rows.Find(9994).Delete();
  444. //no row with row version delete exists - records affected = 0
  445. NumberOfAffectedRows = -1;
  446. try {NumberOfAffectedRows = dbDA.Update(dsResultException);}
  447. catch (DBConcurrencyException dbExp){e = dbExp;}
  448. Compare(e.GetType(),typeof(DBConcurrencyException));
  449. }
  450. catch(Exception ex) {exp = ex;}
  451. finally {EndCase(exp); exp = null; e = null;}
  452. try {
  453. BeginCase("Number Of Affected Rows - Delete Exception");
  454. Compare(NumberOfAffectedRows ,-1 );
  455. }
  456. catch(Exception ex) {exp = ex;}
  457. finally {EndCase(exp); exp = null;}
  458. dsResultException.AcceptChanges();
  459. try {
  460. BeginCase("DBConcurrencyException - Update");
  461. dsResultException.Tables[0].Rows.Find(9995)["Title"] = "Jack the ripper";
  462. //no row with row version Update exists - records affected = 0
  463. NumberOfAffectedRows = -1;
  464. try {NumberOfAffectedRows = dbDA.Update(dsResultException);}
  465. catch (DBConcurrencyException dbExp){e = dbExp;}
  466. Compare(e.GetType(),typeof(DBConcurrencyException));
  467. }
  468. catch(Exception ex) {exp = ex;}
  469. finally {EndCase(exp); exp = null; e = null;}
  470. try {
  471. BeginCase("Number Of Affected Rows - Update Exception");
  472. Compare(NumberOfAffectedRows ,-1 );
  473. }
  474. catch(Exception ex) {exp = ex;}
  475. finally {EndCase(exp); exp = null;}
  476. dsResultException.AcceptChanges();
  477. try {
  478. BeginCase("DBConcurrencyException - Insert");
  479. dsResultException.Tables[0].Rows.Add(new object[] {9996,"Ofer","Borshtein","Insert"});
  480. //no row with row version Insert exists - records affected = 0
  481. NumberOfAffectedRows = -1;
  482. try {NumberOfAffectedRows = dbDA.Update(dsResultException);}
  483. catch (Exception dbExp){e = dbExp;} //throw Sys.Exception
  484. Compare(e != null ,true);
  485. }
  486. catch(Exception ex) {exp = ex;}
  487. finally {EndCase(exp); exp = null; e = null;}
  488. try {
  489. BeginCase("Number Of Affected Rows - Insert Exception");
  490. Compare(NumberOfAffectedRows ,-1 );
  491. }
  492. catch(Exception ex) {exp = ex;}
  493. finally {EndCase(exp); exp = null;}
  494. dsResultException.AcceptChanges();
  495. }
  496. protected void DbDataAdapter_Update_Dt(Sys.Data.Common.DbDataAdapter dbDA) {
  497. int NumberOfAffectedRows = 0;
  498. Exception exp = null;
  499. // --------- get data from DB -----------------
  500. DataSet ds = PrepareDBData_Update(dbDA);
  501. // --------- prepare dataset for update method -----------------
  502. DataSet dsDB1 = ds.Copy();
  503. // --------- prepare dataset for DBConcurrencyException -----------------
  504. DataSet dsDB2 = ds.Copy();
  505. //update dataset
  506. dsDB2.Tables[0].Rows.Add(new object[] {9994,"Ofer", "Borshtein", "Delete"});
  507. dsDB2.Tables[0].Rows.Add(new object[] {9995,"Ofer", "Borshtein", "Update"});
  508. dsDB2.Tables[0].Rows.Find(9996).Delete();
  509. dsDB2.AcceptChanges();
  510. dsDB1.Tables[0].Rows.Add(new object[] {9991,"Ofer","Borshtein","Insert"});
  511. dsDB1.Tables[0].Rows.Find(9992).Delete();
  512. dsDB1.Tables[0].Rows.Find(9993)["Title"] = "Jack the ripper";
  513. //execute update to db
  514. NumberOfAffectedRows = dbDA.Update(dsDB1.Tables[0]);
  515. try {
  516. BeginCase("Number Of Affected Rows");
  517. Compare(NumberOfAffectedRows ,3 );
  518. }
  519. catch(Exception ex) {exp = ex;}
  520. finally {EndCase(exp); exp = null;}
  521. //get result from db in order to check them
  522. DataSet dsExpected = new DataSet(); //ds.Reset();
  523. dbDA.Fill(dsExpected);
  524. dsExpected.Tables[0].PrimaryKey = new DataColumn[] {dsExpected.Tables[0].Columns["EmployeeID"]};
  525. CompareResults_Update(dsDB1,dsDB2,ref dbDA);
  526. CompareResults_Update_Dt_Exception(dsDB2,ref dbDA);
  527. //Create rows which not exists in the DB but exists in the DS with row state = deleted
  528. //this will cause the Update to fail.
  529. dsDB1.Tables[0].Rows.Add(new object[] {9997,"Ofer", "Borshtein", "Delete"});
  530. dsDB1.Tables[0].Rows.Add(new object[] {9998,"Ofer", "Borshtein", "Delete"});
  531. dsDB1.AcceptChanges();
  532. dsDB1.Tables[0].Rows.Find(9997).Delete();
  533. dsDB1.Tables[0].Rows.Find(9998).Delete();
  534. //Check Sys.Data.DBConcurrencyException
  535. //The exception that is thrown by the DataAdapter during the update operation if the number of rows affected equals zero.
  536. try {
  537. BeginCase("Check DBConcurrencyException");
  538. try {
  539. NumberOfAffectedRows = dbDA.Update(dsDB1.Tables[0]);
  540. }
  541. catch (DBConcurrencyException ex) {exp=ex;}
  542. Compare(exp.GetType(),typeof(DBConcurrencyException) );
  543. exp = null;
  544. }
  545. catch(Exception ex) {exp = ex;}
  546. finally {EndCase(exp); exp = null;}
  547. //close connection
  548. if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
  549. ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
  550. }
  551. private void CompareResults_Update_Dt_Exception(DataSet dsResultException,ref Sys.Data.Common.DbDataAdapter dbDA) {
  552. int NumberOfAffectedRows = 0;
  553. Exception exp = null;
  554. Exception e = null;
  555. // --------- check for DBConcurrencyException /UniqueConstraint -----------------
  556. // call AcceptChanges after each exception check in order to make sure that we check only the the current row
  557. try {
  558. BeginCase("DBConcurrencyException - Delete");
  559. dsResultException.Tables[0].Rows.Find(9994).Delete();
  560. //no row with row version delete exists - records affected = 0
  561. NumberOfAffectedRows = -1;
  562. try {NumberOfAffectedRows = dbDA.Update(dsResultException.Tables[0]);}
  563. catch (DBConcurrencyException dbExp){e = dbExp;}
  564. Compare(e.GetType(),typeof(DBConcurrencyException));
  565. }
  566. catch(Exception ex) {exp = ex;}
  567. finally {EndCase(exp); exp = null; e = null;}
  568. try {
  569. BeginCase("Number Of Affected Rows - Delete Exception");
  570. Compare(NumberOfAffectedRows ,-1 );
  571. }
  572. catch(Exception ex) {exp = ex;}
  573. finally {EndCase(exp); exp = null;}
  574. dsResultException.AcceptChanges();
  575. try {
  576. BeginCase("DBConcurrencyException - Update");
  577. dsResultException.Tables[0].Rows.Find(9995)["Title"] = "Jack the ripper";
  578. //no row with row version Update exists - records affected = 0
  579. NumberOfAffectedRows = -1;
  580. try {NumberOfAffectedRows = dbDA.Update(dsResultException.Tables[0]);}
  581. catch (DBConcurrencyException dbExp){e = dbExp;}
  582. Compare(e.GetType(),typeof(DBConcurrencyException));
  583. }
  584. catch(Exception ex) {exp = ex;}
  585. finally {EndCase(exp); exp = null; e = null;}
  586. try {
  587. BeginCase("Number Of Affected Rows - Update Exception");
  588. Compare(NumberOfAffectedRows ,-1 );
  589. }
  590. catch(Exception ex) {exp = ex;}
  591. finally {EndCase(exp); exp = null;}
  592. dsResultException.AcceptChanges();
  593. try {
  594. BeginCase("DBConcurrencyException - Insert");
  595. dsResultException.Tables[0].Rows.Add(new object[] {9996,"Ofer","Borshtein","Insert"});
  596. //no row with row version Insert exists - records affected = 0
  597. NumberOfAffectedRows = -1;
  598. try {NumberOfAffectedRows = dbDA.Update(dsResultException.Tables[0]);}
  599. catch (Exception dbExp){e = dbExp;} //throw Sys.Exception
  600. Compare(e != null ,true);
  601. }
  602. catch(Exception ex) {exp = ex;}
  603. finally {EndCase(exp); exp = null; e = null;}
  604. try {
  605. BeginCase("Number Of Affected Rows - Insert Exception");
  606. Compare(NumberOfAffectedRows ,-1 );
  607. }
  608. catch(Exception ex) {exp = ex;}
  609. finally {EndCase(exp); exp = null;}
  610. dsResultException.AcceptChanges();
  611. }
  612. protected void DbDataAdapter_Update_Dr(Sys.Data.Common.DbDataAdapter dbDA) {
  613. int NumberOfAffectedRows = 0;
  614. Exception exp = null;
  615. // --------- get data from DB -----------------
  616. DataSet ds = PrepareDBData_Update(dbDA);
  617. // --------- prepare dataset for update method -----------------
  618. DataSet dsDB1 = ds.Copy();
  619. // --------- prepare dataset for DBConcurrencyException -----------------
  620. DataSet dsDB2 = ds.Copy();
  621. //update dataset
  622. dsDB2.Tables[0].Rows.Add(new object[] {9994,"Ofer", "Borshtein", "Delete"});
  623. dsDB2.Tables[0].Rows.Add(new object[] {9995,"Ofer", "Borshtein", "Update"});
  624. dsDB2.Tables[0].Rows.Find(9996).Delete();
  625. dsDB2.AcceptChanges();
  626. dsDB1.Tables[0].Rows.Add(new object[] {9991,"Ofer","Borshtein","Insert"});
  627. dsDB1.Tables[0].Rows.Find(9992).Delete();
  628. dsDB1.Tables[0].Rows.Find(9993)["Title"] = "Jack the ripper";
  629. //execute update to db
  630. DataRow[] drArr = new DataRow[dsDB1.Tables[0].Rows.Count] ;
  631. dsDB1.Tables[0].Rows.CopyTo(drArr,0);
  632. NumberOfAffectedRows = dbDA.Update(drArr);
  633. try {
  634. BeginCase("Number Of Affected Rows");
  635. Compare(NumberOfAffectedRows ,3 );
  636. }
  637. catch(Exception ex) {exp = ex;}
  638. finally {EndCase(exp); exp = null;}
  639. //get result from db in order to check them
  640. DataSet dsExpected = new DataSet(); //ds.Reset();
  641. dbDA.Fill(dsExpected);
  642. dsExpected.Tables[0].PrimaryKey = new DataColumn[] {dsExpected.Tables[0].Columns["EmployeeID"]};
  643. CompareResults_Update(dsDB1,dsDB2,ref dbDA);
  644. CompareResults_Update_Dr_Exception(dsDB2,ref dbDA);
  645. //Create rows which not exists in the DB but exists in the DS with row state = deleted
  646. //this will cause the Update to fail.
  647. dsDB1.Tables[0].Rows.Add(new object[] {9997,"Ofer", "Borshtein", "Delete"});
  648. dsDB1.Tables[0].Rows.Add(new object[] {9998,"Ofer", "Borshtein", "Delete"});
  649. dsDB1.AcceptChanges();
  650. dsDB1.Tables[0].Rows.Find(9997).Delete();
  651. dsDB1.Tables[0].Rows.Find(9998)[1] = "Updated!";
  652. drArr = new DataRow[dsDB1.Tables[0].Rows.Count];
  653. dsDB1.Tables[0].Rows.CopyTo(drArr,0);
  654. //Check Sys.Data.DBConcurrencyException
  655. //The exception that is thrown by the DataAdapter during the update operation if the number of rows affected equals zero.
  656. try {
  657. BeginCase("Check DBConcurrencyException");
  658. try {
  659. NumberOfAffectedRows = dbDA.Update(drArr);
  660. }
  661. catch (DBConcurrencyException ex) {exp=ex;}
  662. Compare(exp.GetType(),typeof(DBConcurrencyException) );
  663. exp = null;
  664. }
  665. catch(Exception ex) {exp = ex;}
  666. finally {EndCase(exp); exp = null;}
  667. //close connection
  668. if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
  669. ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
  670. }
  671. private void CompareResults_Update_Dr_Exception(DataSet dsResultException,ref Sys.Data.Common.DbDataAdapter dbDA) {
  672. int NumberOfAffectedRows = 0;
  673. Exception exp = null;
  674. Exception e = null;
  675. DataRow[] drArr = new DataRow[dsResultException.Tables[0].Rows.Count];
  676. // --------- check for DBConcurrencyException /UniqueConstraint -----------------
  677. // call AcceptChanges after each exception check in order to make sure that we check only the the current row
  678. try {
  679. BeginCase("DBConcurrencyException - Delete");
  680. dsResultException.Tables[0].Rows.Find(9994).Delete();
  681. dsResultException.Tables[0].Rows.CopyTo(drArr,0);
  682. //no row with row version delete exists - records affected = 0
  683. NumberOfAffectedRows = -1;
  684. try {NumberOfAffectedRows = dbDA.Update(drArr);}
  685. catch (DBConcurrencyException dbExp){e = dbExp;}
  686. Compare(e.GetType(),typeof(DBConcurrencyException));
  687. }
  688. catch(Exception ex) {exp = ex;}
  689. finally {EndCase(exp); exp = null; e = null;}
  690. try {
  691. BeginCase("Number Of Affected Rows - Delete Exception");
  692. Compare(NumberOfAffectedRows ,-1 );
  693. }
  694. catch(Exception ex) {exp = ex;}
  695. finally {EndCase(exp); exp = null;}
  696. dsResultException.AcceptChanges();
  697. try {
  698. BeginCase("DBConcurrencyException - Update");
  699. dsResultException.Tables[0].Rows.Find(9995)["Title"] = "Jack the ripper";
  700. dsResultException.Tables[0].Rows.CopyTo(drArr,0);
  701. //no row with row version Update exists - records affected = 0
  702. NumberOfAffectedRows = -1;
  703. try {NumberOfAffectedRows = dbDA.Update(drArr);}
  704. catch (DBConcurrencyException dbExp){e = dbExp;}
  705. Compare(e.GetType(),typeof(DBConcurrencyException));
  706. }
  707. catch(Exception ex) {exp = ex;}
  708. finally {EndCase(exp); exp = null; e = null;}
  709. try {
  710. BeginCase("Number Of Affected Rows - Update Exception");
  711. Compare(NumberOfAffectedRows ,-1 );
  712. }
  713. catch(Exception ex) {exp = ex;}
  714. finally {EndCase(exp); exp = null;}
  715. dsResultException.AcceptChanges();
  716. try {
  717. BeginCase("DBConcurrencyException - Insert");
  718. dsResultException.Tables[0].Rows.Add(new object[] {9996,"Ofer","Borshtein","Insert"});
  719. dsResultException.Tables[0].Rows.CopyTo(drArr,0);
  720. //no row with row version Insert exists - records affected = 0
  721. NumberOfAffectedRows = -1;
  722. try {NumberOfAffectedRows = dbDA.Update(drArr);}
  723. catch (Exception dbExp){e = dbExp;} //throw Sys.Exception
  724. Compare(e != null ,true);
  725. }
  726. catch(Exception ex) {exp = ex;}
  727. finally {EndCase(exp); exp = null; e = null;}
  728. try {
  729. BeginCase("Number Of Affected Rows - Insert Exception");
  730. Compare(NumberOfAffectedRows ,-1 );
  731. }
  732. catch(Exception ex) {exp = ex;}
  733. finally {EndCase(exp); exp = null;}
  734. dsResultException.AcceptChanges();
  735. }
  736. protected void DbDataAdapter_Update_Ds_Str(Sys.Data.Common.DbDataAdapter dbDA) {
  737. int NumberOfAffectedRows = 0;
  738. Exception exp = null;
  739. // --------- get data from DB -----------------
  740. DataSet ds = PrepareDBData_Update(dbDA);
  741. // --------- prepare dataset for update method -----------------
  742. DataSet dsDB1 = ds.Copy();
  743. // --------- prepare dataset for DBConcurrencyException -----------------
  744. DataSet dsDB2 = ds.Copy();
  745. //update dataset
  746. dsDB2.Tables[0].Rows.Add(new object[] {9994,"Ofer", "Borshtein", "Delete"});
  747. dsDB2.Tables[0].Rows.Add(new object[] {9995,"Ofer", "Borshtein", "Update"});
  748. dsDB2.Tables[0].Rows.Find(9996).Delete();
  749. dsDB2.AcceptChanges();
  750. dsDB1.Tables[0].Rows.Add(new object[] {9991,"Ofer","Borshtein","Insert"});
  751. dsDB1.Tables[0].Rows.Find(9992).Delete();
  752. dsDB1.Tables[0].Rows.Find(9993)["Title"] = "Jack the ripper";
  753. //execute update to db
  754. NumberOfAffectedRows = dbDA.Update(dsDB1,dsDB1.Tables[0].TableName);
  755. try {
  756. BeginCase("Number Of Affected Rows");
  757. Compare(NumberOfAffectedRows ,3 );
  758. }
  759. catch(Exception ex) {exp = ex;}
  760. finally {EndCase(exp); exp = null;}
  761. //get result from db in order to check them
  762. DataSet dsExpected = new DataSet(); //ds.Reset();
  763. dbDA.Fill(dsExpected);
  764. dsExpected.Tables[0].PrimaryKey = new DataColumn[] {dsExpected.Tables[0].Columns["EmployeeID"]};
  765. CompareResults_Update(dsDB1,dsDB2,ref dbDA);
  766. CompareResults_Update_Ds_Str_Exception(dsDB2,ref dbDA);
  767. //Create rows which not exists in the DB but exists in the DS with row state = deleted
  768. //this will cause the Update to fail.
  769. dsDB1.Tables[0].Rows.Add(new object[] {9997,"Ofer", "Borshtein", "Delete"});
  770. dsDB1.Tables[0].Rows.Add(new object[] {9998,"Ofer", "Borshtein", "Delete"});
  771. dsDB1.AcceptChanges();
  772. dsDB1.Tables[0].Rows.Find(9997).Delete();
  773. dsDB1.Tables[0].Rows.Find(9998).Delete();
  774. //Check Sys.Data.DBConcurrencyException
  775. //The exception that is thrown by the DataAdapter during the update operation if the number of rows affected equals zero.
  776. try {
  777. BeginCase("Check DBConcurrencyException");
  778. try {
  779. NumberOfAffectedRows = dbDA.Update(dsDB1,dsDB1.Tables[0].TableName);
  780. }
  781. catch (DBConcurrencyException ex) {exp=ex;}
  782. Compare(exp.GetType(),typeof(DBConcurrencyException) );
  783. exp = null;
  784. }
  785. catch(Exception ex) {exp = ex;}
  786. finally {EndCase(exp); exp = null;}
  787. //close connection
  788. if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
  789. ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
  790. }
  791. private void CompareResults_Update_Ds_Str_Exception(DataSet dsResultException,ref Sys.Data.Common.DbDataAdapter dbDA) {
  792. int NumberOfAffectedRows = 0;
  793. Exception exp = null;
  794. Exception e = null;
  795. // --------- check for DBConcurrencyException /UniqueConstraint -----------------
  796. // call AcceptChanges after each exception check in order to make sure that we check only the the current row
  797. try {
  798. BeginCase("DBConcurrencyException - Delete");
  799. dsResultException.Tables[0].Rows.Find(9994).Delete();
  800. //no row with row version delete exists - records affected = 0
  801. NumberOfAffectedRows = -1;
  802. try {NumberOfAffectedRows = dbDA.Update(dsResultException,dsResultException.Tables[0].TableName);}
  803. catch (DBConcurrencyException dbExp){e = dbExp;}
  804. Compare(e.GetType(),typeof(DBConcurrencyException));
  805. }
  806. catch(Exception ex) {exp = ex;}
  807. finally {EndCase(exp); exp = null; e = null;}
  808. try {
  809. BeginCase("Number Of Affected Rows - Delete Exception");
  810. Compare(NumberOfAffectedRows ,-1 );
  811. }
  812. catch(Exception ex) {exp = ex;}
  813. finally {EndCase(exp); exp = null;}
  814. dsResultException.AcceptChanges();
  815. try {
  816. BeginCase("DBConcurrencyException - Update");
  817. dsResultException.Tables[0].Rows.Find(9995)["Title"] = "Jack the ripper";
  818. //no row with row version Update exists - records affected = 0
  819. NumberOfAffectedRows = -1;
  820. try {NumberOfAffectedRows = dbDA.Update(dsResultException,dsResultException.Tables[0].TableName);}
  821. catch (DBConcurrencyException dbExp){e = dbExp;}
  822. Compare(e.GetType(),typeof(DBConcurrencyException));
  823. }
  824. catch(Exception ex) {exp = ex;}
  825. finally {EndCase(exp); exp = null; e = null;}
  826. try {
  827. BeginCase("Number Of Affected Rows - Update Exception");
  828. Compare(NumberOfAffectedRows ,-1 );
  829. }
  830. catch(Exception ex) {exp = ex;}
  831. finally {EndCase(exp); exp = null;}
  832. dsResultException.AcceptChanges();
  833. try {
  834. BeginCase("DBConcurrencyException - Insert");
  835. dsResultException.Tables[0].Rows.Add(new object[] {9996,"Ofer","Borshtein","Insert"});
  836. //no row with row version Insert exists - records affected = 0
  837. NumberOfAffectedRows = -1;
  838. try {NumberOfAffectedRows = dbDA.Update(dsResultException,dsResultException.Tables[0].TableName);}
  839. catch (Exception dbExp){e = dbExp;} //throw Sys.Exception
  840. Compare(e != null ,true);
  841. }
  842. catch(Exception ex) {exp = ex;}
  843. finally {EndCase(exp); exp = null; e = null;}
  844. try {
  845. BeginCase("Number Of Affected Rows - Insert Exception");
  846. Compare(NumberOfAffectedRows ,-1 );
  847. }
  848. catch(Exception ex) {exp = ex;}
  849. finally {EndCase(exp); exp = null;}
  850. dsResultException.AcceptChanges();
  851. }
  852. /*
  853. *
  854. * insert/update the database with data that will be used in testings
  855. *
  856. */
  857. protected void PrepareDataForTesting(string ConnectionString) {
  858. int iExists = 0;
  859. try {
  860. //create database specific date value
  861. string strBirthDateValue = "";
  862. switch (ConnectedDataProvider.GetDbType(ConnectionString)) {
  863. case DataBaseServer.DB2:
  864. strBirthDateValue = "'1988-05-31-15.33.44'";
  865. break;
  866. case DataBaseServer.Oracle:
  867. strBirthDateValue = "to_date('1988-05-31 15:33:44', 'yyyy-mm-dd HH24:mi:ss')";
  868. break;
  869. case DataBaseServer.PostgreSQL:
  870. strBirthDateValue = "to_timestamp('1988-05-31 15:33:44', 'yyyy-mm-dd HH24:mi:ss')";
  871. break;
  872. case DataBaseServer.SQLServer:
  873. case DataBaseServer.Sybase:
  874. strBirthDateValue = "'1988-May-31 15:33:44'";
  875. break;
  876. }
  877. OracleConnection con = new OracleConnection(ConnectionString);
  878. try {
  879. con.Open();
  880. }
  881. catch (Exception ex) {
  882. throw new Exception("PrepareDataForTesting failed trying to connect to DB using Connection string:" + ConnectionString + "\nException:" + ex.ToString(),ex);
  883. }
  884. OracleCommand cmd = new OracleCommand("",con);
  885. //update / insert to table Employees
  886. for (int i = 100; i <= 700; i +=100) {
  887. cmd.CommandText = "select 1 from Employees where EmployeeID = " + i.ToString();
  888. iExists = Sys.Convert.ToInt32(cmd.ExecuteScalar());
  889. if (iExists != 1) {
  890. cmd.CommandText = "insert into Employees (EmployeeID, LastName, FirstName, Title, BirthDate) " +
  891. " Values (" + i.ToString() +
  892. ",'Last" + i.ToString()
  893. + "','First" + i.ToString()
  894. + "', null, "
  895. + strBirthDateValue + ")";
  896. cmd.ExecuteNonQuery();
  897. }
  898. else {
  899. cmd.CommandText = "update Employees set "
  900. + " LastName = 'Last" + i.ToString()
  901. + "', FirstName = 'First" + i.ToString()
  902. + "', Title = null, BirthDate = "
  903. + strBirthDateValue
  904. + " where EmployeeID = " + i.ToString() ;
  905. Log(cmd.CommandText);
  906. cmd.ExecuteNonQuery();
  907. }
  908. }
  909. //update / insert to table Customers
  910. for (int i = 100; i <= 700; i +=100) {
  911. cmd.CommandText = "select 1 from Customers where CustomerID = 'GH" + i.ToString() + "'";
  912. iExists = Sys.Convert.ToInt32(cmd.ExecuteScalar());
  913. if (iExists != 1) {
  914. cmd.CommandText = "insert into Customers (CustomerID , CompanyName, Phone) Values ('GH" + i.ToString() + "','Company" + i.ToString() + "','00-" + i.ToString() + i.ToString() + "')";
  915. cmd.ExecuteNonQuery();
  916. }
  917. else {
  918. cmd.CommandText = "update Customers set CompanyName = 'Company" + i.ToString() + "', Phone = '00-" + i.ToString() + i.ToString() + "' where CustomerID = 'GH" + i.ToString() + "'";
  919. cmd.ExecuteNonQuery();
  920. }
  921. }
  922. cmd.CommandText = "select 1 from Customers where CustomerID = 'GH200'";
  923. iExists = Sys.Convert.ToInt32(cmd.ExecuteScalar());
  924. if (iExists != 1) {
  925. cmd.CommandText = "insert into Customers (CustomerID , CompanyName) Values ('GH200','Company200')";
  926. cmd.ExecuteNonQuery();
  927. }
  928. else {
  929. cmd.CommandText = "update Customers set CompanyName = 'Company200' where CustomerID = 'GH200'";
  930. cmd.ExecuteNonQuery();
  931. }
  932. con.Close();
  933. }
  934. catch (Exception ex) {
  935. throw new Exception("PrepareDataForTesting failed with exception:" + ex.ToString(),ex);
  936. }
  937. }
  938. /*
  939. * used to insert data to the database in order to check DataAdapter Update metods
  940. */
  941. protected DataSet PrepareDBData_Update(Sys.Data.Common.DbDataAdapter dbDA) {
  942. return PrepareDBData_Update(dbDA,false);
  943. }
  944. protected DataSet PrepareDBData_Update(Sys.Data.Common.DbDataAdapter dbDA,bool sqlConnectionString) {
  945. IDbDataAdapter Ida = (IDbDataAdapter)dbDA;
  946. IDbCommand ICmd = Ida.SelectCommand;
  947. ICmd.CommandText = "SELECT EmployeeID, LastName, FirstName, Title FROM Employees WHERE EmployeeID in (9991,9992,9993,9994,9995,9996)";
  948. IDbConnection IConn = ICmd.Connection;
  949. if (!sqlConnectionString) {
  950. IConn.ConnectionString = MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString;
  951. }
  952. IConn.Open();
  953. //Insert rows to be updated (insert,delete,update)
  954. IDbCommand cmd;
  955. if (sqlConnectionString) {
  956. cmd = new Sys.Data.SqlClient.SqlCommand();
  957. cmd.Connection = (Sys.Data.SqlClient.SqlConnection)IConn;
  958. }
  959. else {
  960. cmd = new Sys.Data.OracleClient.OracleCommand();
  961. cmd.Connection = (Sys.Data.OracleClient.OracleConnection)IConn;
  962. }
  963. //run execute after each command because DB2 doesn't support multiple commands
  964. cmd.CommandText = "DELETE FROM Employees WHERE EmployeeID in (9991,9992,9993,9994,9995,9996,9997,9998)";
  965. cmd.ExecuteNonQuery();
  966. //only for SQL Server
  967. DataBaseServer DBType = ConnectedDataProvider.GetDbType(IConn.ConnectionString);
  968. cmd.CommandText = "INSERT INTO Employees (EmployeeID, LastName, FirstName, Title) VALUES(9992, 'Ofer', 'Borshtein', 'delete')";
  969. //if (DBType == DataBaseServer.SQLServer) cmd.CommandText = "SET IDENTITY_INSERT Employees ON;" + cmd.CommandText;
  970. cmd.ExecuteNonQuery();
  971. cmd.CommandText = "INSERT INTO Employees (EmployeeID, LastName, FirstName, Title) VALUES(9993, 'Ofer', 'Borshtein', 'Update')";
  972. //if (DBType == DataBaseServer.SQLServer) cmd.CommandText = "SET IDENTITY_INSERT Employees ON;" + cmd.CommandText;
  973. cmd.ExecuteNonQuery();
  974. cmd.CommandText = "INSERT INTO Employees (EmployeeID, LastName, FirstName, Title) VALUES(9996, 'Ofer', 'Borshtein', 'Exp')";
  975. //if (DBType == DataBaseServer.SQLServer) cmd.CommandText = "SET IDENTITY_INSERT Employees ON;" + cmd.CommandText;
  976. cmd.ExecuteNonQuery();
  977. //cmd.CommandText += "INSERT INTO Employees (EmployeeID, LastName, FirstName, Title) VALUES(9991, 'Ofer', 'Borshtein', 'Insert'); ";
  978. //cmd.CommandText += "INSERT INTO Employees (EmployeeID, LastName, FirstName, Title) VALUES(9994, 'Ofer', 'Borshtein', 'Exp'); ";
  979. //cmd.CommandText += "INSERT INTO Employees (EmployeeID, LastName, FirstName, Title) VALUES(9995, 'Ofer', 'Borshtein', 'Exp'); ";
  980. //cmd.CommandText += "INSERT INTO Employees (EmployeeID, LastName, FirstName, Title) VALUES(9997, 'Ofer', 'Borshtein', 'delete'); ";
  981. //cmd.CommandText += "INSERT INTO Employees (EmployeeID, LastName, FirstName, Title) VALUES(9998, 'Ofer', 'Borshtein', 'delete'); ";
  982. DataSet ds = new DataSet();
  983. dbDA.Fill(ds);
  984. ds.Tables[0].PrimaryKey = new DataColumn[] {ds.Tables[0].Columns["EmployeeID"]};
  985. return ds;
  986. }
  987. private void CompareResults_Update(DataSet dsResult,DataSet dsResultException,ref Sys.Data.Common.DbDataAdapter dbDA) {
  988. Exception exp = null;
  989. //----------------- Compare dsDB with dsExcepted -----------------
  990. try {
  991. BeginCase("Insert Row ");
  992. Compare(dsResult.Tables[0].Rows.Find(9991) == null ,false );
  993. }
  994. catch(Exception ex) {exp = ex;}
  995. finally {EndCase(exp); exp = null;}
  996. try {
  997. BeginCase("Update Row ");
  998. Compare(dsResult.Tables[0].Rows.Find(9993)["Title"],"Jack the ripper");
  999. }
  1000. catch(Exception ex) {exp = ex;}
  1001. finally {EndCase(exp); exp = null;}
  1002. try {
  1003. BeginCase("Delete Row ");
  1004. Compare(dsResult.Tables[0].Rows.Find(9992) ,null);
  1005. }
  1006. catch(Exception ex) {exp = ex;}
  1007. finally {EndCase(exp); exp = null;}
  1008. }
  1009. #endregion
  1010. protected void DBDataAdapter_DefaultSourceTableName() {
  1011. Exception exp = null;
  1012. try {
  1013. BeginCase("DefaultSourceTableName");
  1014. Compare(Sys.Data.Common.DbDataAdapter.DefaultSourceTableName , "Table");
  1015. }
  1016. catch(Exception ex) {exp = ex;}
  1017. finally {EndCase(exp); exp = null;}
  1018. }
  1019. #endregion
  1020. #region "----------- Sys.Data.Common.DataAdapter --------------"
  1021. protected void DataAdapter_AcceptChangesDuringFill(Sys.Data.Common.DbDataAdapter dbDA) {
  1022. Exception exp = null;
  1023. IDbDataAdapter Ida = (IDbDataAdapter)dbDA;
  1024. IDbCommand ICmd = Ida.SelectCommand;
  1025. IDbConnection IConn = ICmd.Connection;
  1026. IConn.ConnectionString = MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString;
  1027. PrepareDataForTesting( MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  1028. IConn.Open();
  1029. //get the total rows count
  1030. ICmd.CommandText = "SELECT Count(*) FROM Customers where CustomerID in ('GH100','GH200','GH300','GH400','GH500','GH600','GH700')";
  1031. int ExpectedRows = Sys.Convert.ToInt32(ICmd.ExecuteScalar());
  1032. try {
  1033. BeginCase("Check that Expected rows count > 0");
  1034. Compare(ExpectedRows > 0 ,true);
  1035. }
  1036. catch(Exception ex) {exp = ex;}
  1037. finally {EndCase(exp); exp = null;}
  1038. ICmd.CommandText = "SELECT CustomerID, CompanyName, City, Country, Phone FROM Customers where CustomerID in ('GH100','GH200','GH300','GH400','GH500','GH600','GH700')";
  1039. DataSet ds = new DataSet();
  1040. dbDA.AcceptChangesDuringFill = false;
  1041. try {
  1042. BeginCase("Execute Fill - check return rows count");
  1043. int i = dbDA.Fill(ds);
  1044. Compare(i ,ExpectedRows );
  1045. }
  1046. catch(Exception ex) {exp = ex;}
  1047. finally {EndCase(exp); exp = null;}
  1048. bool blnAcceptChanges = false;
  1049. foreach (DataRow dr in ds.Tables[0].Rows) {
  1050. if (dr.RowState != DataRowState.Added ) {
  1051. blnAcceptChanges = true;
  1052. break;
  1053. }
  1054. }
  1055. try {
  1056. BeginCase("AcceptChangesDuringFill - false");
  1057. Compare(blnAcceptChanges ,false );
  1058. }
  1059. catch(Exception ex) {exp = ex;}
  1060. finally {EndCase(exp); exp = null;}
  1061. ds.Reset();
  1062. dbDA.AcceptChangesDuringFill = true;
  1063. dbDA.Fill(ds);
  1064. blnAcceptChanges = false;
  1065. foreach (DataRow dr in ds.Tables[0].Rows) {
  1066. if (dr.RowState != DataRowState.Unchanged ) {
  1067. blnAcceptChanges = true;
  1068. break;
  1069. }
  1070. }
  1071. try {
  1072. BeginCase("AcceptChangesDuringFill - true");
  1073. Compare(blnAcceptChanges ,false );
  1074. }
  1075. catch(Exception ex) {exp = ex;}
  1076. finally {EndCase(exp); exp = null;}
  1077. //close connection
  1078. if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
  1079. ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
  1080. }
  1081. protected void DataAdapter_ContinueUpdateOnError(Sys.Data.Common.DbDataAdapter dbDA) {
  1082. /*
  1083. !!!!!! Not working (TestName "ContinueUpdateOnError - true, check value 2")!!!!!
  1084. If ContinueUpdateOnError is set to true, no exception is thrown when an error occurs during the update of a row.
  1085. The update of the row is skipped and the error information is placed in the RowError property of the row in error.
  1086. The DataAdapter continues to update subsequent rows.
  1087. If ContinueUpdateOnError is set to false, an exception is thrown when an error occurs during the update of a row.
  1088. */
  1089. Exception exp = null;
  1090. IDbDataAdapter Ida = (IDbDataAdapter)dbDA;
  1091. IDbCommand ICmd = Ida.SelectCommand;
  1092. IDbConnection IConn = ICmd.Connection;
  1093. IConn.ConnectionString = MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString;
  1094. PrepareDataForTesting( MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  1095. IConn.Open();
  1096. //get the total rows count
  1097. ICmd.CommandText = "SELECT Count(*) FROM Customers where CustomerID in ('GH100','GH200','GH300','GH400','GH500','GH600','GH700')";
  1098. int ExpectedRows = Sys.Convert.ToInt32(ICmd.ExecuteScalar());
  1099. try {
  1100. BeginCase("Check that Expected rows count > 0");
  1101. Compare(ExpectedRows > 0 ,true);
  1102. }
  1103. catch(Exception ex) {exp = ex;}
  1104. finally {EndCase(exp); exp = null;}
  1105. ICmd.CommandText = "SELECT CustomerID, CompanyName, City, Country, Phone FROM Customers where CustomerID in ('GH100','GH200','GH300','GH400','GH500','GH600','GH700')";
  1106. DataSet dsMem = new DataSet(); //Disconected dataset
  1107. DataSet dsDB = new DataSet(); //DataBase data
  1108. dbDA.AcceptChangesDuringFill = true;
  1109. //get data from DB
  1110. try {
  1111. BeginCase("Execute Fill - check return rows count");
  1112. int i = dbDA.Fill(dsMem);
  1113. Compare(i ,ExpectedRows );
  1114. }
  1115. catch(Exception ex) {exp = ex;}
  1116. finally {EndCase(exp); exp = null;}
  1117. //update data with invalid information (Max. length for Phone is 24)
  1118. // 123456789012345678901234
  1119. string newValue1 = "Very Long String That Will Raise An Error Yep!";
  1120. string oldValue1 = dsMem.Tables[0].Rows[3]["Phone"].ToString();
  1121. string oldValue2 = dsMem.Tables[0].Rows[4]["Phone"].ToString();
  1122. string newValue2 = "03-1234";
  1123. dsMem.Tables[0].Rows[3]["Phone"] = newValue1;
  1124. dsMem.Tables[0].Rows[4]["Phone"] = newValue2;
  1125. dbDA.ContinueUpdateOnError = true;
  1126. //will not throw exception
  1127. try {
  1128. BeginCase("ContinueUpdateOnError - true, check exception");
  1129. try {
  1130. dbDA.Update(dsMem);
  1131. }
  1132. catch(Exception ex){exp = ex;}
  1133. Compare(exp == null,true);
  1134. exp = null;
  1135. }
  1136. catch(Exception ex) {exp = ex;}
  1137. finally {EndCase(exp); exp = null;}
  1138. dbDA.Fill(dsDB); //get data from DB to check the update operation
  1139. try {
  1140. BeginCase("ContinueUpdateOnError - true, check RowError");
  1141. Compare(dsMem.Tables[0].Rows[3].RowError.Length > 0 , true);
  1142. }
  1143. catch(Exception ex) {exp = ex;}
  1144. finally {EndCase(exp); exp = null;}
  1145. try {
  1146. BeginCase("ContinueUpdateOnError - true, check value 1");
  1147. Compare(dsDB.Tables[0].Rows[3]["Phone"] , oldValue1);
  1148. }
  1149. catch(Exception ex) {exp = ex;}
  1150. finally {EndCase(exp); exp = null;}
  1151. /* - Test excluded, it is not working in .NET too!
  1152. //should continue the update
  1153. try
  1154. {
  1155. BeginCase("ContinueUpdateOnError - true, check value 2");
  1156. Compare(dsDB.Tables[0].Rows[4]["Phone"] , newValue2); //--------- NOT WORKING !!! -----------
  1157. }
  1158. catch(Exception ex) {exp = ex;}
  1159. finally {EndCase(exp); exp = null;}
  1160. */
  1161. dsMem.Reset();
  1162. dsDB.Reset();
  1163. dbDA.Fill(dsMem);
  1164. dsMem.Tables[0].Rows[3]["Phone"] = newValue1 ;
  1165. dsMem.Tables[0].Rows[4]["Phone"] = newValue2;
  1166. dbDA.ContinueUpdateOnError = false;
  1167. try {
  1168. BeginCase("ContinueUpdateOnError - false, check exception");
  1169. try {
  1170. dbDA.Update(dsMem);
  1171. }
  1172. catch(Exception ex){exp = ex;}
  1173. Compare(exp == null,false);
  1174. exp = null;
  1175. }
  1176. catch(Exception ex) {exp = ex;}
  1177. finally {EndCase(exp); exp = null;}
  1178. dbDA.Fill(dsDB); //get data from DB to check the update operation
  1179. try {
  1180. BeginCase("ContinueUpdateOnError - false,check RowError");
  1181. Compare(dsMem.Tables[0].Rows[3].RowError.Length > 0 ,true);
  1182. }
  1183. catch(Exception ex) {exp = ex;}
  1184. finally {EndCase(exp); exp = null;}
  1185. try {
  1186. BeginCase("ContinueUpdateOnError - false,check value 1");
  1187. Compare(dsDB.Tables[0].Rows[3]["Phone"] , oldValue1 );
  1188. }
  1189. catch(Exception ex) {exp = ex;}
  1190. finally {EndCase(exp); exp = null;}
  1191. try {
  1192. BeginCase("ContinueUpdateOnError - false,check value 2");
  1193. Compare(dsDB.Tables[0].Rows[4]["Phone"] , oldValue2 );
  1194. }
  1195. catch(Exception ex) {exp = ex;}
  1196. finally {EndCase(exp); exp = null;}
  1197. //close connection
  1198. if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
  1199. ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
  1200. }
  1201. protected void DataAdapter_MissingMappingAction(Sys.Data.Common.DbDataAdapter dbDA) {
  1202. Exception exp = null;
  1203. IDbDataAdapter Ida = (IDbDataAdapter)dbDA;
  1204. IDbCommand ICmd = Ida.SelectCommand;
  1205. IDbConnection IConn = ICmd.Connection;
  1206. IConn.ConnectionString = MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString;
  1207. // IConn.Open();
  1208. //get the total rows count
  1209. PrepareDataForTesting( MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  1210. IConn.Open();
  1211. ICmd.CommandText = "SELECT Count(*) FROM Customers where CustomerID in ('GH100','GH200','GH300','GH400','GH500','GH600','GH700')";
  1212. int ExpectedRows = Sys.Convert.ToInt32(ICmd.ExecuteScalar());
  1213. try {
  1214. BeginCase("Check that Expected rows count > 0");
  1215. Compare(ExpectedRows > 0 ,true);
  1216. }
  1217. catch(Exception ex) {exp = ex;}
  1218. finally {EndCase(exp); exp = null;}
  1219. ICmd.CommandText = "SELECT CustomerID, CompanyName, City, Country, Phone FROM Customers where CustomerID in ('GH100','GH200','GH300','GH400','GH500','GH600','GH700')";
  1220. //init dataset
  1221. DataSet ds = new DataSet();
  1222. try {
  1223. BeginCase("Execute Fill - check return rows count");
  1224. int i = dbDA.Fill(ds);
  1225. Compare(i ,ExpectedRows );
  1226. }
  1227. catch(Exception ex) {exp = ex;}
  1228. finally {EndCase(exp); exp = null;}
  1229. //make dataset schema mismatch with DB
  1230. ds.Tables[0].Columns.Remove("Country");
  1231. ds.Tables[0].Clear();
  1232. //--- Default value ---
  1233. try {
  1234. BeginCase("MissingMappingAction Default value");
  1235. Compare(dbDA.MissingMappingAction , MissingMappingAction.Passthrough);
  1236. }
  1237. catch(Exception ex) {exp = ex;}
  1238. finally {EndCase(exp); exp = null;}
  1239. //--- MissingMappingAction.Error ---
  1240. ds.Tables[0].Clear();
  1241. dbDA.MissingMappingAction = MissingMappingAction.Error ;
  1242. Exception ExMissingMappingAction = null;
  1243. try {
  1244. BeginCase("MissingMappingAction.Error");
  1245. try {
  1246. dbDA.Fill(ds);
  1247. }
  1248. catch (InvalidOperationException e) {
  1249. ExMissingMappingAction = e;
  1250. }
  1251. Compare(ExMissingMappingAction.GetType() ,typeof(InvalidOperationException));
  1252. }
  1253. catch(Exception ex) {exp = ex;}
  1254. finally {EndCase(exp); exp = null;}
  1255. try {
  1256. BeginCase("MissingMappingAction.Error, Row.Count = 0");
  1257. Compare(ds.Tables[0].Rows.Count , 0 );
  1258. }
  1259. catch(Exception ex) {exp = ex;}
  1260. finally {EndCase(exp); exp = null;}
  1261. try {
  1262. BeginCase("MissingMappingAction.Error, Column");
  1263. Compare(ds.Tables[0].Columns.IndexOf("Country") , -1 );
  1264. }
  1265. catch(Exception ex) {exp = ex;}
  1266. finally {EndCase(exp); exp = null;}
  1267. //--- MissingMappingAction.Ignore ---
  1268. ds.Tables[0].Clear();
  1269. dbDA.MissingMappingAction = MissingMappingAction.Ignore ;
  1270. ExMissingMappingAction = null;
  1271. try {
  1272. BeginCase("MissingMappingAction.Ignore");
  1273. try {
  1274. dbDA.Fill(ds);
  1275. }
  1276. catch (InvalidOperationException e) {
  1277. ExMissingMappingAction = e;
  1278. }
  1279. Compare(ExMissingMappingAction ,null);
  1280. }
  1281. catch(Exception ex) {exp = ex;}
  1282. finally {EndCase(exp); exp = null;}
  1283. try {
  1284. BeginCase("MissingMappingAction.Ignore, Row.Count = 0");
  1285. Compare(ds.Tables[0].Rows.Count ,0);
  1286. }
  1287. catch(Exception ex) {exp = ex;}
  1288. finally {EndCase(exp); exp = null;}
  1289. try {
  1290. BeginCase("MissingMappingAction.Ignore, Column");
  1291. Compare(ds.Tables[0].Columns.IndexOf("Country") , -1 );
  1292. }
  1293. catch(Exception ex) {exp = ex;}
  1294. finally {EndCase(exp); exp = null;}
  1295. //--- MissingMappingAction.Passthrough ---
  1296. ds.Tables[0].Clear();
  1297. dbDA.MissingMappingAction = MissingMappingAction.Passthrough ;
  1298. ExMissingMappingAction = null;
  1299. try {
  1300. BeginCase("MissingMappingAction.Passthrough");
  1301. try {
  1302. dbDA.Fill(ds);
  1303. }
  1304. catch (InvalidOperationException e) {
  1305. ExMissingMappingAction = e;
  1306. }
  1307. Compare(ExMissingMappingAction ,null);
  1308. }
  1309. catch(Exception ex) {exp = ex;}
  1310. finally {EndCase(exp); exp = null;}
  1311. try {
  1312. BeginCase("MissingMappingAction.Passthrough, Row.Count > 0");
  1313. Compare(ds.Tables[0].Rows.Count >= 0 ,true );
  1314. }
  1315. catch(Exception ex) {exp = ex;}
  1316. finally {EndCase(exp); exp = null;}
  1317. try {
  1318. BeginCase("MissingMappingAction.Passthrough, Column");
  1319. Compare(ds.Tables[0].Columns.IndexOf("Country") >= 0 ,true );
  1320. }
  1321. catch(Exception ex) {exp = ex;}
  1322. finally {EndCase(exp); exp = null;}
  1323. //close connection
  1324. if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
  1325. ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
  1326. }
  1327. protected void DataAdapter_MissingSchemaAction(Sys.Data.Common.DbDataAdapter dbDA) {
  1328. Exception exp = null;
  1329. IDbDataAdapter Ida = (IDbDataAdapter)dbDA;
  1330. IDbCommand ICmd = Ida.SelectCommand;
  1331. IDbConnection IConn = ICmd.Connection;
  1332. IConn.ConnectionString = MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString;
  1333. // IConn.Open();
  1334. PrepareDataForTesting( MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  1335. IConn.Open();
  1336. //get the total rows count
  1337. ICmd.CommandText = "SELECT Count(*) FROM Customers where CustomerID in ('GH100','GH200','GH300','GH400','GH500','GH600','GH700')";
  1338. int ExpectedRows = Sys.Convert.ToInt32(ICmd.ExecuteScalar());
  1339. try {
  1340. BeginCase("Check that Expected rows count > 0");
  1341. Compare(ExpectedRows > 0 ,true);
  1342. }
  1343. catch(Exception ex) {exp = ex;}
  1344. finally {EndCase(exp); exp = null;}
  1345. ICmd.CommandText = "SELECT CustomerID, CompanyName, City, Country, Phone FROM Customers where CustomerID in ('GH100','GH200','GH300','GH400','GH500','GH600','GH700')";
  1346. //get db type
  1347. DBType = ConnectedDataProvider.GetDbType(((IDbDataAdapter)dbDA).SelectCommand.Connection.ConnectionString);
  1348. //init dataset
  1349. DataSet ds = new DataSet();
  1350. try {
  1351. BeginCase("Execute Fill - check return rows count");
  1352. int i = dbDA.Fill(ds);
  1353. Compare(i ,ExpectedRows );
  1354. }
  1355. catch(Exception ex) {exp = ex;}
  1356. finally {EndCase(exp); exp = null;}
  1357. //make dataset schema mismatch with DB
  1358. ds.Tables[0].Columns.Remove("Country");
  1359. ds.Tables[0].Clear();
  1360. //--- Default value ---
  1361. try {
  1362. BeginCase("MissingSchemaAction Default value");
  1363. Compare(dbDA.MissingSchemaAction, MissingSchemaAction.Add);
  1364. }
  1365. catch(Exception ex) {exp = ex;}
  1366. finally {EndCase(exp); exp = null;}
  1367. //--- MissingSchemaAction.Error ---
  1368. ds.Tables[0].Clear();
  1369. dbDA.MissingSchemaAction = MissingSchemaAction.Error ;
  1370. Exception ExMissingSchemaAction = null;
  1371. try {
  1372. BeginCase("MissingSchemaAction.Error");
  1373. try {
  1374. dbDA.Fill(ds);
  1375. }
  1376. catch (InvalidOperationException e) {
  1377. ExMissingSchemaAction = e;
  1378. }
  1379. Compare(ExMissingSchemaAction.GetType() ,typeof(InvalidOperationException));
  1380. }
  1381. catch(Exception ex) {exp = ex;}
  1382. finally {EndCase(exp); exp = null;}
  1383. try {
  1384. BeginCase("MissingSchemaAction.Error, Row.Count = 0");
  1385. Compare(ds.Tables[0].Rows.Count , 0 );
  1386. }
  1387. catch(Exception ex) {exp = ex;}
  1388. finally {EndCase(exp); exp = null;}
  1389. try {
  1390. BeginCase("MissingSchemaAction.Error, Column");
  1391. Compare(ds.Tables[0].Columns.IndexOf("Country") , -1 );
  1392. }
  1393. catch(Exception ex) {exp = ex;}
  1394. finally {EndCase(exp); exp = null;}
  1395. //--- MissingSchemaAction.Ignore ---
  1396. try {
  1397. //catch any exception that might occure
  1398. BeginCase("MissingSchemaAction.Ignore - invoke");
  1399. ds.Tables[0].Clear();
  1400. dbDA.MissingSchemaAction = MissingSchemaAction.Ignore ;
  1401. ExMissingSchemaAction = null;
  1402. dbDA.Fill(ds);
  1403. Compare(true ,true);
  1404. }
  1405. catch(Exception ex) {exp = ex;}
  1406. finally {EndCase(exp); exp = null;}
  1407. try {
  1408. BeginCase("MissingSchemaAction.Ignore, Row.Count = 0");
  1409. Compare(ds.Tables[0].Rows.Count > 0 ,true);
  1410. }
  1411. catch(Exception ex) {exp = ex;}
  1412. finally {EndCase(exp); exp = null;}
  1413. try {
  1414. BeginCase("MissingSchemaAction.Ignore, Column");
  1415. Compare(ds.Tables[0].Columns.IndexOf("Country") , -1 );
  1416. }
  1417. catch(Exception ex) {exp = ex;}
  1418. finally {EndCase(exp); exp = null;}
  1419. #if !KNOWN_BUG //BUG_NUM:1951
  1420. try {
  1421. BeginCase("MissingSchemaAction.Ignore, PrimaryKey");
  1422. Compare(ds.Tables[0].PrimaryKey.Length == 0 ,true );
  1423. }
  1424. catch(Exception ex) {exp = ex;}
  1425. finally {EndCase(exp); exp = null;}
  1426. #endif
  1427. //--- MissingSchemaAction.Add ---
  1428. try {
  1429. //catch any exception that might occure
  1430. BeginCase("MissingSchemaAction.Add - invoke");
  1431. ds.Tables[0].Clear();
  1432. dbDA.MissingSchemaAction = MissingSchemaAction.Add ;
  1433. ExMissingSchemaAction = null;
  1434. dbDA.Fill(ds);
  1435. Compare(true ,true);
  1436. }
  1437. catch(Exception ex) {exp = ex;}
  1438. finally {EndCase(exp); exp = null;}
  1439. try {
  1440. BeginCase("MissingSchemaAction.Add, Row.Count > 0");
  1441. Compare(ds.Tables[0].Rows.Count >= 0 ,true );
  1442. }
  1443. catch(Exception ex) {exp = ex;}
  1444. finally {EndCase(exp); exp = null;}
  1445. try {
  1446. BeginCase("MissingSchemaAction.Add, Column");
  1447. Compare(ds.Tables[0].Columns.IndexOf("Country") >= 0 ,true );
  1448. }
  1449. catch(Exception ex) {exp = ex;}
  1450. finally {EndCase(exp); exp = null;}
  1451. #if !KNOWN_BUG //BUG_NUM:1952
  1452. //DB2 don't return primary key
  1453. if (DBType != DataBaseServer.DB2) {
  1454. try {
  1455. BeginCase("MissingSchemaAction.AddWithKey, PrimaryKey");
  1456. Compare(ds.Tables[0].PrimaryKey.Length ,0);
  1457. }
  1458. catch(Exception ex) {exp = ex;}
  1459. finally {EndCase(exp); exp = null;}
  1460. }
  1461. //--- MissingSchemaAction.AddWithKey ---
  1462. try {
  1463. //catch any exception that might occure
  1464. BeginCase("MissingSchemaAction.AddWithKey - invoke");
  1465. ds.Tables[0].Clear();
  1466. ds.Tables[0].Columns.Remove("Country");
  1467. dbDA.MissingSchemaAction = MissingSchemaAction.AddWithKey ;
  1468. ExMissingSchemaAction = null;
  1469. dbDA.Fill(ds);
  1470. Compare(true ,true);
  1471. }
  1472. catch(Exception ex) {exp = ex;}
  1473. finally {EndCase(exp); exp = null;}
  1474. try {
  1475. BeginCase("MissingSchemaAction.AddWithKey, Row.Count > 0");
  1476. Compare(ds.Tables[0].Rows.Count >= 0 ,true );
  1477. }
  1478. catch(Exception ex) {exp = ex;}
  1479. finally {EndCase(exp); exp = null;}
  1480. try {
  1481. BeginCase("MissingSchemaAction.AddWithKey, Column");
  1482. Compare(ds.Tables[0].Columns.IndexOf("Country") >= 0 ,true );
  1483. }
  1484. catch(Exception ex) {exp = ex;}
  1485. finally {EndCase(exp); exp = null;}
  1486. //DB2 don't return primary key
  1487. if (DBType != DataBaseServer.DB2 &&
  1488. DBType != DataBaseServer.Oracle) {
  1489. try {
  1490. BeginCase("MissingSchemaAction.AddWithKey, PrimaryKey");
  1491. Compare(ds.Tables[0].PrimaryKey.Length > 0 ,true );
  1492. }
  1493. catch(Exception ex) {exp = ex;}
  1494. finally {EndCase(exp); exp = null;}
  1495. }
  1496. #endif
  1497. //close connection
  1498. if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
  1499. ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
  1500. }
  1501. #endregion
  1502. public override void BeginTest(string testName) {
  1503. base.BeginTest (testName);
  1504. Log(String.Format("DataBase Type is {0}", ConnectedDataProvider.GetDbType()));
  1505. }
  1506. }
  1507. }