ADONetTesterClass.cs 66 KB

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