| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865 |
- // Authors:
- // Rafael Mizrahi <[email protected]>
- // Erez Lotan <[email protected]>
- // Oren Gurfinkel <[email protected]>
- // Ofer Borstein
- //
- // Copyright (c) 2004 Mainsoft Co.
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Data;
- using System.Data.OleDb ;
- using System.IO;
- using System.Collections;
- using MonoTests.System.Data.Utils.Data;
- using Sys = System;
- namespace MonoTests.System.Data.Utils {
- public class ADONetTesterClass : GHTBase {
- #region " Base Constructors "
- protected ADONetTesterClass(Sys.IO.TextWriter Logger, bool LogOnSuccess) : base(Logger,LogOnSuccess){}
- protected ADONetTesterClass(bool LogOnSuccess):base(Console.Out, LogOnSuccess){}
- protected ADONetTesterClass():base(Console.Out, false){}
- #endregion
- private MonoTests.System.Data.Utils.DataBaseServer DBType ;
- #region "----------- Build Update Commands --------------"
- protected void OleDbDataAdapter_BuildUpdateCommands(ref Sys.Data.OleDb.OleDbDataAdapter oleDBda) {
- Sys.Data.OleDb.OleDbConnection Conn = oleDBda.SelectCommand.Connection;
- oleDBda.DeleteCommand = new Sys.Data.OleDb.OleDbCommand();
- oleDBda.InsertCommand = new Sys.Data.OleDb.OleDbCommand();
- oleDBda.UpdateCommand = new Sys.Data.OleDb.OleDbCommand();
- oleDBda.DeleteCommand.Connection = Conn;
- oleDBda.InsertCommand.Connection = Conn;
- oleDBda.UpdateCommand.Connection = Conn;
- oleDBda.DeleteCommand.Parameters.Add(new Sys.Data.OleDb.OleDbParameter("EmployeeID",Sys.Data.OleDb.OleDbType.Integer)) ;
- oleDBda.DeleteCommand.Parameters["EmployeeID"].SourceVersion = DataRowVersion.Original;
- oleDBda.DeleteCommand.Parameters["EmployeeID"].SourceColumn = "EmployeeID";
- oleDBda.InsertCommand.Parameters.Add(new Sys.Data.OleDb.OleDbParameter("EmployeeID",Sys.Data.OleDb.OleDbType.Integer));
- oleDBda.InsertCommand.Parameters.Add(new Sys.Data.OleDb.OleDbParameter("LastName",Sys.Data.OleDb.OleDbType.VarWChar ,20));
- oleDBda.InsertCommand.Parameters.Add(new Sys.Data.OleDb.OleDbParameter("FirstName",Sys.Data.OleDb.OleDbType.VarWChar,10));
- oleDBda.InsertCommand.Parameters["EmployeeID"].SourceColumn = "EmployeeID";
- oleDBda.InsertCommand.Parameters["LastName"].SourceColumn = "LastName";
- oleDBda.InsertCommand.Parameters["FirstName"].SourceColumn = "FirstName";
- oleDBda.UpdateCommand.Parameters.Add(new Sys.Data.OleDb.OleDbParameter("Title",Sys.Data.OleDb.OleDbType.VarWChar,30));
- oleDBda.UpdateCommand.Parameters.Add(new Sys.Data.OleDb.OleDbParameter("EmployeeID",Sys.Data.OleDb.OleDbType.Integer));
- oleDBda.UpdateCommand.Parameters["EmployeeID"].SourceColumn = "EmployeeID";
- oleDBda.UpdateCommand.Parameters["Title"].SourceColumn = "Title";
- //for OleDB, ODBC
- string deleteSQL = "DELETE FROM Employees WHERE EmployeeID = ?";
- string insertSQL = "INSERT INTO Employees (EmployeeID, LastName, FirstName) VALUES (?, ?, ?)";
- string updateSQL = "UPDATE Employees SET Title = ? WHERE EmployeeID = ?";
- oleDBda.DeleteCommand.CommandText = deleteSQL;
- oleDBda.InsertCommand.CommandText = insertSQL;
- oleDBda.UpdateCommand.CommandText = updateSQL;
- }
- protected void SqlDataAdapter_BuildUpdateCommands(ref Sys.Data.SqlClient.SqlDataAdapter Sqlda) {
- Sys.Data.SqlClient.SqlConnection Conn = Sqlda.SelectCommand.Connection;
- Sqlda.DeleteCommand = new Sys.Data.SqlClient.SqlCommand();
- Sqlda.InsertCommand = new Sys.Data.SqlClient.SqlCommand();
- Sqlda.UpdateCommand = new Sys.Data.SqlClient.SqlCommand();
- Sqlda.DeleteCommand.Connection = Conn;
- Sqlda.InsertCommand.Connection = Conn;
- Sqlda.UpdateCommand.Connection = Conn;
- Sqlda.DeleteCommand.Parameters.Add(new Sys.Data.SqlClient.SqlParameter("@EmployeeID",DbType.Int32)) ;
- Sqlda.DeleteCommand.Parameters["@EmployeeID"].SourceVersion = DataRowVersion.Original;
- Sqlda.DeleteCommand.Parameters["@EmployeeID"].SourceColumn = "EmployeeID";
- Sqlda.InsertCommand.Parameters.Add(new Sys.Data.SqlClient.SqlParameter("@EmployeeID",DbType.Int32));
- Sqlda.InsertCommand.Parameters.Add(new Sys.Data.SqlClient.SqlParameter("@LastName",Sys.Data.SqlDbType.VarChar ,20));
- Sqlda.InsertCommand.Parameters.Add(new Sys.Data.SqlClient.SqlParameter("@FirstName",Sys.Data.SqlDbType.VarChar ,10));
- Sqlda.InsertCommand.Parameters["@EmployeeID"].SourceColumn = "EmployeeID";
- Sqlda.InsertCommand.Parameters["@LastName"].SourceColumn = "LastName";
- Sqlda.InsertCommand.Parameters["@FirstName"].SourceColumn = "FirstName";
- Sqlda.UpdateCommand.Parameters.Add(new Sys.Data.SqlClient.SqlParameter("@Title",Sys.Data.SqlDbType.VarChar,30));
- Sqlda.UpdateCommand.Parameters.Add(new Sys.Data.SqlClient.SqlParameter("@EmployeeID",DbType.Int32));
- Sqlda.UpdateCommand.Parameters["@EmployeeID"].SourceColumn = "EmployeeID";
- Sqlda.UpdateCommand.Parameters["@Title"].SourceColumn = "Title";
- //for Sql Client
- string deleteSql = "DELETE FROM Employees WHERE EmployeeID = @EmployeeID";
- string insertSql = "INSERT INTO Employees (EmployeeID, LastName, FirstName) VALUES (@EmployeeID, @LastName, @FirstName)";
- string updateSql = "UPDATE Employees SET Title = @Title WHERE EmployeeID = @EmployeeID";
- Sqlda.DeleteCommand.CommandText = deleteSql;
- Sqlda.InsertCommand.CommandText = insertSql;
- Sqlda.UpdateCommand.CommandText = updateSql;
- }
- #endregion
- #region "----------- Sys.Data.Common.DBDataAdapter --------------"
- #region " DBDataAdapter - Fill / Fill Schema "
- protected void DbDataAdapter_Fill_Ds(Sys.Data.Common.DbDataAdapter dbDA) {
- DataSet dsResult, dsExpected ;
- int ExpectedRowsCount,ResultRowsCount;
- DataSet ds = new DataSet();
- ExpectedRowsCount = ReadDBData_Fill(dbDA,ref ds,false);
- // create expected dataset to compare result to
- dsExpected = ds.Copy();
- //make some changes, the fill method will overides those changes with data from DB.
- foreach (DataRow dr in ds.Tables[0].Select())
- dr["Country"] = "NeverNeverLand";
- ds.Tables[0].Columns.Remove("HomePhone"); //remove column, this column will be addedd during the fill process
- //ds.Tables.Remove(ds.Tables[1]); //remove the table, this table will be addedd during the fill process
- ds.AcceptChanges();
-
- // create source dataset to be filled
- dsResult = ds.Copy();
-
- //execute fill
- ResultRowsCount = dbDA.Fill(dsResult);
-
- CompareResults_Fill(dsResult,dsExpected);
- //close connection
- if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
- ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
- }
- protected void DbDataAdapter_Fill_Dt(Sys.Data.Common.DbDataAdapter dbDA) {
- DataSet dsResult, dsExpected ;
- int ExpectedRowsCount,ResultRowsCount;
- DataSet ds = new DataSet();
- ExpectedRowsCount = ReadDBData_Fill(dbDA,ref ds, false);
- //ds.Tables.Remove(ds.Tables[1]); //remove the table, fill only one table
- // create expected dataset to compare result to
- dsExpected = ds.Copy();
- //make some changes, the fill method will overides those changes with data from DB.
- foreach (DataRow dr in ds.Tables[0].Select())
- dr["Country"] = "NeverNeverLand";
- ds.Tables[0].Columns.Remove("HomePhone"); //remove column, this column will be addedd during the fill process
- ds.AcceptChanges();
-
- // create source dataset to be filled
- dsResult = ds.Copy();
-
- //execute fill
- ResultRowsCount = dbDA.Fill(dsResult.Tables["Table"]);
- CompareResults_Fill(dsResult,dsExpected);
- //close connection
- if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
- ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
- }
- protected void DbDataAdapter_Fill_Ds_Str(Sys.Data.Common.DbDataAdapter dbDA) {
- DataSet dsResult, dsExpected ;
- int ExpectedRowsCount,ResultRowsCount;
- DataSet ds = new DataSet();
- ExpectedRowsCount = ReadDBData_Fill(dbDA,ref ds, false);
- // create expected dataset to compare result to
- dsExpected = ds.Copy();
- //make some changes, the fill method will overides those changes with data from DB.
- foreach (DataRow dr in ds.Tables[0].Select())
- dr["Country"] = "NeverNeverLand";
- ds.Tables[0].Columns.Remove("HomePhone"); //remove column, this column will be addedd during the fill process
- //ds.Tables.Remove(ds.Tables[1]); //remove the table, this table will be addedd during the fill process
- ds.AcceptChanges();
-
- // create source dataset to be filled
- dsResult = ds.Copy();
-
- //execute fill
- ResultRowsCount = dbDA.Fill(dsResult,dsResult.Tables[0].TableName );
- CompareResults_Fill(dsResult,dsExpected);
- //close connection
- if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
- ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
- }
- protected void DbDataAdapter_Fill_Ds_Int_Int_Str(Sys.Data.Common.DbDataAdapter dbDA) {
- DataSet dsResult, dsExpected ;
- int ExpectedRowsCount,ResultRowsCount;
- DataSet ds = new DataSet();
- ExpectedRowsCount = ReadDBData_Fill(dbDA,ref ds, false);
- // create expected dataset to compare result to
- dsExpected = ds.Copy();
- //make some changes, the fill method will overides those changes with data from DB.
- foreach (DataRow dr in ds.Tables[0].Select())
- dr["Country"] = "NeverNeverLand";
- ds.Tables[0].Columns.Remove("HomePhone"); //remove column, this column will be addedd during the fill process
- //ds.Tables.Remove(ds.Tables[1]); //remove the table, this table will be addedd during the fill process
- ds.AcceptChanges();
-
- // create source dataset to be filled
- dsResult = ds.Copy();
-
- //execute fill
- ResultRowsCount = dbDA.Fill(dsResult,0,0,dsResult.Tables[0].TableName);
- CompareResults_Fill(dsResult,dsExpected);
-
- dsResult = ds.Copy();
- //modify expected dataset to match the expected result
- for (int i=0; i < dsExpected.Tables[0].Rows.Count ; i++) {
- if (i < 5 || i > 14) {
- dsExpected.Tables[0].Rows[i]["Country"] = "NeverNeverLand";
- dsExpected.Tables[0].Rows[i]["HomePhone"] = DBNull.Value;
- }
- }
- ResultRowsCount = dbDA.Fill(dsResult,5,10,dsResult.Tables[0].TableName);
- CompareResults_Fill(dsResult,dsExpected);
- //close connection
- if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
- ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
- }
- protected void DbDataAdapter_FillSchema_Ds_SchemaType(Sys.Data.Common.DbDataAdapter dbDA) {
- DataSet dsResult = new DataSet();
- DataSet dsExpected = new DataSet();
-
- // create expected dataset to compare result to
- ReadDBData_Fill(dbDA,ref dsExpected ,true);
- // Note: When handling batch SQL statements that return multiple results,
- // the implementation of FillSchema for the .NET Framework Data Provider for OLEDB
- // retrieves schema information for only the first result.
- // To retrieve schema information for multiple results, use Fill with the MissingSchemaAction set to AddWithKey
- // if (dbDA.GetType() == typeof(Sys.Data.OleDb.OleDbDataAdapter))
- // dsExpected.Tables.Remove(dsExpected.Tables[1]);
- //execute FillSchema
- //dsResult = dsExpected.Copy();
- DataTable[] dtArr = dbDA.FillSchema(dsResult,SchemaType.Mapped );
- //************ Fix .Net bug? (FillSchema method add AutoIncrement=true) *******************
- dsResult.Tables[0].Columns["EmployeeID"].AutoIncrement = false;
-
- CompareResults_Fill(dsResult,dsExpected);
- Exception exp = null;
- try {
- BeginCase("Check return value - Table[0]");
- Compare(dtArr[0],dsResult.Tables[0] );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- // if (dbDA.GetType() != typeof(Sys.Data.OleDb.OleDbDataAdapter))
- // try
- // {
- // BeginCase("Check return value - Table[1]");
- // Compare(dtArr[1],dsResult.Tables[1]);
- // }
- // catch(Exception ex) {exp = ex;}
- // finally {EndCase(exp); exp = null;}
-
- //close connection
- if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
- ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
- }
- protected void DbDataAdapter_FillSchema_Dt_SchemaType(Sys.Data.Common.DbDataAdapter dbDA) {
- DataSet dsResult = new DataSet();
- DataSet dsExpected = new DataSet();
-
- // create expected dataset to compare result to
- ReadDBData_Fill(dbDA,ref dsExpected ,true);
- //dsExpected.Tables.Remove(dsExpected.Tables[1]);
- //execute FillSchema
- dsResult.Tables.Add("Table");
- DataTable dt = dbDA.FillSchema(dsResult.Tables[0],SchemaType.Mapped );
- //************ Fix .Net bug? (FillSchema method add AutoIncrement=true) *******************
- dsResult.Tables[0].Columns["EmployeeID"].AutoIncrement = false;
- CompareResults_Fill(dsResult,dsExpected);
- Exception exp = null;
- try {
- BeginCase("Check return value - Table[0]");
- Compare(dt,dsResult.Tables[0] );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- //close connection
- if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
- ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
- }
- protected void DbDataAdapter_FillSchema_Ds_SchemaType_Str(Sys.Data.Common.DbDataAdapter dbDA) {
- DataSet dsResult = new DataSet();
- DataSet dsExpected = new DataSet();
-
- // create expected dataset to compare result to
- ReadDBData_Fill(dbDA,ref dsExpected ,true);
- //dsExpected.Tables.Remove(dsExpected.Tables[1]);
- //execute FillSchema
- dsResult.Tables.Add("Table");
- DataTable[] dtArr = dbDA.FillSchema(dsResult,SchemaType.Mapped,dsResult.Tables[0].TableName);
- //************ Fix .Net bug? (FillSchema method add AutoIncrement=true) *******************
- dsResult.Tables[0].Columns["EmployeeID"].AutoIncrement = false;
- CompareResults_Fill(dsResult,dsExpected);
- Exception exp = null;
- try {
- BeginCase("Check return value - Table[0]");
- Compare(dtArr[0],dsResult.Tables[0] );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- //close connection
- if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
- ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
- }
- private int ReadDBData_Fill(Sys.Data.Common.DbDataAdapter dbDA, ref DataSet ds, bool ReadSchemaOnly) {
- int ExpectedRowsCount = 0;
- IDbDataAdapter Ida = (IDbDataAdapter)dbDA;
- IDbCommand ICmd = Ida.SelectCommand;
- IDbConnection IConn = ICmd.Connection;
- IDataReader Idr;
- IConn.ConnectionString = MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString;
- ICmd.CommandText = "SELECT EmployeeID, LastName, FirstName, Title, Address, City, Region, Country, Extension, HomePhone FROM Employees " ;
- IConn.Open();
- //get db type
- DBType = ConnectedDataProvider.GetDbType(((IDbDataAdapter)dbDA).SelectCommand.Connection.ConnectionString);
- // Execute data Reader - Get Expected results
- Idr = ICmd.ExecuteReader();
- // create temp dataset to insert results
- ExpectedRowsCount = DataReaderFill_Fill(ref ds,ref Idr,ReadSchemaOnly);
- Idr.Close();
- return ExpectedRowsCount;
- }
- private void CompareResults_Fill(DataSet dsResult,DataSet dsExpected ) {
- Exception exp = null;
- // try
- // {
- // BeginCase("Compare Rows count");
- // // ??????? Fill return count for first table only ??????
- // Compare(ExpectedRowsCount ,ResultRowsCount );
- // }
- // catch(Exception ex) {exp = ex;}
- // finally {EndCase(exp); exp = null;}
- try {
- BeginCase("Compare data");
- Compare(dsResult.GetXml() ,dsExpected.GetXml());
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- try {
- BeginCase("Compare schema");
- Compare(dsResult.GetXmlSchema() ,dsExpected.GetXmlSchema());
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- }
- private int DataReaderFill_Fill(ref DataSet dsExpected, ref IDataReader Idr,bool ReadSchemaOnly) {
- bool blnNextResults;
- int RowsAffected = 0;
- object[] objArr = null;
- DataTable SchemaTable = null;
- do {
- SchemaTable = Idr.GetSchemaTable();
- //add new table with the right amount of columns, the first table must be named "Table"
- if (dsExpected.Tables.Count == 0)
- dsExpected.Tables.Add(new DataTable("Table"));
- else
- dsExpected.Tables.Add();
- for (int i = 0 ; i < Idr.FieldCount; i++) {
- dsExpected.Tables[dsExpected.Tables.Count-1].Columns.Add(new DataColumn(Idr.GetName(i),Idr.GetFieldType(i)));
- if (ReadSchemaOnly) { // add schema info
- dsExpected.Tables[dsExpected.Tables.Count-1].Columns[i].AllowDBNull = (bool)SchemaTable.Rows[i]["AllowDBNull"];
- dsExpected.Tables[dsExpected.Tables.Count-1].Columns[i].AutoIncrement = (bool)SchemaTable.Rows[i]["IsAutoIncrement"];
- dsExpected.Tables[dsExpected.Tables.Count-1].Columns[i].ReadOnly = (bool)SchemaTable.Rows[i]["IsReadOnly"];
- dsExpected.Tables[dsExpected.Tables.Count-1].Columns[i].Unique = (bool)SchemaTable.Rows[i]["IsUnique"];
- if (dsExpected.Tables[dsExpected.Tables.Count-1].Columns[i].DataType == typeof(string))
- dsExpected.Tables[dsExpected.Tables.Count-1].Columns[i].MaxLength = (int)SchemaTable.Rows[i]["ColumnSize"];
- }
- }
- if (!ReadSchemaOnly) {
- //array that holds the current rows values
- objArr = new object[Idr.FieldCount];
- //fill the new table
- while (Idr.Read()) {
- Idr.GetValues(objArr);
- //update existing row, if no row is found - add it as new row
- dsExpected.Tables[dsExpected.Tables.Count-1].LoadDataRow(objArr,false);
- RowsAffected++;
- }
- }
- //get next record set
- blnNextResults = Idr.NextResult();
- }
- while (blnNextResults);
- // add primary key, fill method will update existing rows instead of insert new ones
- dsExpected.Tables[0].PrimaryKey = new DataColumn[] {dsExpected.Tables[0].Columns["EmployeeID"]};
- //if (ReadSchemaOnly) dsExpected.Tables[1].PrimaryKey = new DataColumn[] {dsExpected.Tables[1].Columns["CustomerID"]};
- dsExpected.AcceptChanges();
- return RowsAffected;
-
- }
- #endregion
- #region " DBDataAdapter - FillError "
- private bool blnReadDBData_Fill = false;
- protected void DbDataAdapter_FillError(Sys.Data.Common.DbDataAdapter dbDA) {
- Exception exp = null;
- IDbDataAdapter Ida = (IDbDataAdapter)dbDA;
- IDbCommand ICmd = Ida.SelectCommand;
- IDbConnection IConn = ICmd.Connection;
- IConn.ConnectionString = MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString;
- ICmd.CommandText = "SELECT CustomerID, CompanyName, City, Country, Phone FROM Customers ";
- IConn.Open();
- DataSet ds = new DataSet();
- ds.Tables.Add(new DataTable("Customers"));
- ds.Tables[0].Columns.Add("CustomerID",typeof(byte));
-
- //check FillError event
- dbDA.FillError += new FillErrorEventHandler(dbDA_FillError);
- blnReadDBData_Fill = false;
- try {
- BeginCase("FillError");
- try {
- dbDA.Fill(ds,"Customers");
- }
- catch (Exception ){};
- Compare(blnReadDBData_Fill ,true );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- dbDA.FillError -= new FillErrorEventHandler(dbDA_FillError);
- //close connection
- if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
- ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
- }
- private void dbDA_FillError(object sender, FillErrorEventArgs args) {
- blnReadDBData_Fill = true;
- args.Continue = false;
- }
- #endregion
- #region " DBDataAdapter - Update "
- protected void DbDataAdapter_Update_Ds(Sys.Data.Common.DbDataAdapter dbDA) {
- int NumberOfAffectedRows = 0;
- Exception exp = null;
- // --------- get data from DB -----------------
- DataSet ds = PrepareDBData_Update(dbDA);
- // --------- prepare dataset for update method -----------------
- DataSet dsDB1 = ds.Copy();
-
- // --------- prepare dataset for DBConcurrencyException -----------------
- DataSet dsDB2 = ds.Copy();
-
- //update expecteed dataset
- dsDB2.Tables[0].Rows.Add(new object[] {9994,"Ofer", "Borshtein", "Delete"});
- dsDB2.Tables[0].Rows.Add(new object[] {9995,"Ofer", "Borshtein", "Update"});
- dsDB2.Tables[0].Rows.Find(9996).Delete();
- dsDB2.AcceptChanges();
- //make changes to the DataBase (through the dataset)
- dsDB1.Tables[0].Rows.Add(new object[] {9991,"Ofer","Borshtein","Insert"});
- dsDB1.Tables[0].Rows.Find(9992).Delete();
- dsDB1.Tables[0].Rows.Find(9993)["Title"] = "Jack the ripper";
- //execute update to db
- NumberOfAffectedRows = dbDA.Update(dsDB1);
-
- try {
- BeginCase("Number Of Affected Rows");
- Compare(NumberOfAffectedRows ,3 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
-
- //get result from db in order to check them
- DataSet dsExpected = new DataSet(); //ds.Reset();
- dbDA.Fill(dsExpected);
- dsExpected.Tables[0].PrimaryKey = new DataColumn[] {dsExpected.Tables[0].Columns["EmployeeID"]};
- CompareResults_Update(dsDB1,dsDB2,ref dbDA);
- CompareResults_Update_Ds_Exception(dsDB2,ref dbDA);
- //Create rows which not exists in the DB but exists in the DS with row state = deleted
- //this will cause the Update to fail.
- dsDB1.Tables[0].Rows.Add(new object[] {9997,"Ofer", "Borshtein", "Delete"});
- dsDB1.Tables[0].Rows.Add(new object[] {9998,"Ofer", "Borshtein", "Delete"});
- dsDB1.AcceptChanges();
- dsDB1.Tables[0].Rows.Find(9997).Delete();
- dsDB1.Tables[0].Rows.Find(9998).Delete();
- //Check Sys.Data.DBConcurrencyException
- //The exception that is thrown by the DataAdapter during the update operation if the number of rows affected equals zero.
- try {
- BeginCase("Check DBConcurrencyException");
- try {
- NumberOfAffectedRows = dbDA.Update(dsDB1);
- }
- catch (DBConcurrencyException ex) {exp=ex;}
- Compare(exp.GetType(),typeof(DBConcurrencyException) );
- exp = null;
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- //close connection
- if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
- ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
- }
- private void CompareResults_Update_Ds_Exception(DataSet dsResultException,ref Sys.Data.Common.DbDataAdapter dbDA) {
- int NumberOfAffectedRows = 0;
- Exception exp = null;
- Exception e = null;
-
- // --------- check for DBConcurrencyException /UniqueConstraint -----------------
- // call AcceptChanges after each exception check in order to make sure that we check only the the current row
- try {
- BeginCase("DBConcurrencyException - Delete");
- dsResultException.Tables[0].Rows.Find(9994).Delete();
- //no row with row version delete exists - records affected = 0
- NumberOfAffectedRows = -1;
- try {NumberOfAffectedRows = dbDA.Update(dsResultException);}
- catch (DBConcurrencyException dbExp){e = dbExp;}
- Compare(e.GetType(),typeof(DBConcurrencyException));
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null; e = null;}
- try {
- BeginCase("Number Of Affected Rows - Delete Exception");
- Compare(NumberOfAffectedRows ,-1 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- dsResultException.AcceptChanges();
- try {
- BeginCase("DBConcurrencyException - Update");
- dsResultException.Tables[0].Rows.Find(9995)["Title"] = "Jack the ripper";
- //no row with row version Update exists - records affected = 0
- NumberOfAffectedRows = -1;
- try {NumberOfAffectedRows = dbDA.Update(dsResultException);}
- catch (DBConcurrencyException dbExp){e = dbExp;}
- Compare(e.GetType(),typeof(DBConcurrencyException));
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null; e = null;}
- try {
- BeginCase("Number Of Affected Rows - Update Exception");
- Compare(NumberOfAffectedRows ,-1 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- dsResultException.AcceptChanges();
- try {
- BeginCase("DBConcurrencyException - Insert");
- dsResultException.Tables[0].Rows.Add(new object[] {9996,"Ofer","Borshtein","Insert"});
- //no row with row version Insert exists - records affected = 0
- NumberOfAffectedRows = -1;
- try {NumberOfAffectedRows = dbDA.Update(dsResultException);}
- catch (Exception dbExp){e = dbExp;} //throw Sys.Exception
- Compare(e != null ,true);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null; e = null;}
- try {
- BeginCase("Number Of Affected Rows - Insert Exception");
- Compare(NumberOfAffectedRows ,-1 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- dsResultException.AcceptChanges();
- }
-
- protected void DbDataAdapter_Update_Dt(Sys.Data.Common.DbDataAdapter dbDA) {
- int NumberOfAffectedRows = 0;
- Exception exp = null;
- // --------- get data from DB -----------------
- DataSet ds = PrepareDBData_Update(dbDA);
- // --------- prepare dataset for update method -----------------
- DataSet dsDB1 = ds.Copy();
-
- // --------- prepare dataset for DBConcurrencyException -----------------
- DataSet dsDB2 = ds.Copy();
- //update dataset
- dsDB2.Tables[0].Rows.Add(new object[] {9994,"Ofer", "Borshtein", "Delete"});
- dsDB2.Tables[0].Rows.Add(new object[] {9995,"Ofer", "Borshtein", "Update"});
- dsDB2.Tables[0].Rows.Find(9996).Delete();
- dsDB2.AcceptChanges();
-
- dsDB1.Tables[0].Rows.Add(new object[] {9991,"Ofer","Borshtein","Insert"});
- dsDB1.Tables[0].Rows.Find(9992).Delete();
- dsDB1.Tables[0].Rows.Find(9993)["Title"] = "Jack the ripper";
- //execute update to db
- NumberOfAffectedRows = dbDA.Update(dsDB1.Tables[0]);
-
- try {
- BeginCase("Number Of Affected Rows");
- Compare(NumberOfAffectedRows ,3 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
-
- //get result from db in order to check them
- DataSet dsExpected = new DataSet(); //ds.Reset();
- dbDA.Fill(dsExpected);
- dsExpected.Tables[0].PrimaryKey = new DataColumn[] {dsExpected.Tables[0].Columns["EmployeeID"]};
- CompareResults_Update(dsDB1,dsDB2,ref dbDA);
- CompareResults_Update_Dt_Exception(dsDB2,ref dbDA);
- //Create rows which not exists in the DB but exists in the DS with row state = deleted
- //this will cause the Update to fail.
- dsDB1.Tables[0].Rows.Add(new object[] {9997,"Ofer", "Borshtein", "Delete"});
- dsDB1.Tables[0].Rows.Add(new object[] {9998,"Ofer", "Borshtein", "Delete"});
- dsDB1.AcceptChanges();
- dsDB1.Tables[0].Rows.Find(9997).Delete();
- dsDB1.Tables[0].Rows.Find(9998).Delete();
- //Check Sys.Data.DBConcurrencyException
- //The exception that is thrown by the DataAdapter during the update operation if the number of rows affected equals zero.
- try {
- BeginCase("Check DBConcurrencyException");
- try {
- NumberOfAffectedRows = dbDA.Update(dsDB1.Tables[0]);
- }
- catch (DBConcurrencyException ex) {exp=ex;}
- Compare(exp.GetType(),typeof(DBConcurrencyException) );
- exp = null;
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- //close connection
- if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
- ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
- }
- private void CompareResults_Update_Dt_Exception(DataSet dsResultException,ref Sys.Data.Common.DbDataAdapter dbDA) {
- int NumberOfAffectedRows = 0;
- Exception exp = null;
- Exception e = null;
- // --------- check for DBConcurrencyException /UniqueConstraint -----------------
- // call AcceptChanges after each exception check in order to make sure that we check only the the current row
- try {
- BeginCase("DBConcurrencyException - Delete");
- dsResultException.Tables[0].Rows.Find(9994).Delete();
- //no row with row version delete exists - records affected = 0
- NumberOfAffectedRows = -1;
- try {NumberOfAffectedRows = dbDA.Update(dsResultException.Tables[0]);}
- catch (DBConcurrencyException dbExp){e = dbExp;}
- Compare(e.GetType(),typeof(DBConcurrencyException));
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null; e = null;}
- try {
- BeginCase("Number Of Affected Rows - Delete Exception");
- Compare(NumberOfAffectedRows ,-1 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- dsResultException.AcceptChanges();
- try {
- BeginCase("DBConcurrencyException - Update");
- dsResultException.Tables[0].Rows.Find(9995)["Title"] = "Jack the ripper";
- //no row with row version Update exists - records affected = 0
- NumberOfAffectedRows = -1;
- try {NumberOfAffectedRows = dbDA.Update(dsResultException.Tables[0]);}
- catch (DBConcurrencyException dbExp){e = dbExp;}
- Compare(e.GetType(),typeof(DBConcurrencyException));
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null; e = null;}
- try {
- BeginCase("Number Of Affected Rows - Update Exception");
- Compare(NumberOfAffectedRows ,-1 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- dsResultException.AcceptChanges();
- try {
- BeginCase("DBConcurrencyException - Insert");
- dsResultException.Tables[0].Rows.Add(new object[] {9996,"Ofer","Borshtein","Insert"});
- //no row with row version Insert exists - records affected = 0
- NumberOfAffectedRows = -1;
- try {NumberOfAffectedRows = dbDA.Update(dsResultException.Tables[0]);}
- catch (Exception dbExp){e = dbExp;} //throw Sys.Exception
- Compare(e != null ,true);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null; e = null;}
- try {
- BeginCase("Number Of Affected Rows - Insert Exception");
- Compare(NumberOfAffectedRows ,-1 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- dsResultException.AcceptChanges();
- }
- protected void DbDataAdapter_Update_Dr(Sys.Data.Common.DbDataAdapter dbDA) {
- int NumberOfAffectedRows = 0;
- Exception exp = null;
- // --------- get data from DB -----------------
- DataSet ds = PrepareDBData_Update(dbDA);
- // --------- prepare dataset for update method -----------------
- DataSet dsDB1 = ds.Copy();
-
- // --------- prepare dataset for DBConcurrencyException -----------------
- DataSet dsDB2 = ds.Copy();
- //update dataset
- dsDB2.Tables[0].Rows.Add(new object[] {9994,"Ofer", "Borshtein", "Delete"});
- dsDB2.Tables[0].Rows.Add(new object[] {9995,"Ofer", "Borshtein", "Update"});
- dsDB2.Tables[0].Rows.Find(9996).Delete();
- dsDB2.AcceptChanges();
-
- dsDB1.Tables[0].Rows.Add(new object[] {9991,"Ofer","Borshtein","Insert"});
- dsDB1.Tables[0].Rows.Find(9992).Delete();
- dsDB1.Tables[0].Rows.Find(9993)["Title"] = "Jack the ripper";
- //execute update to db
-
- DataRow[] drArr = new DataRow[dsDB1.Tables[0].Rows.Count] ;
- dsDB1.Tables[0].Rows.CopyTo(drArr,0);
- NumberOfAffectedRows = dbDA.Update(drArr);
-
- try {
- BeginCase("Number Of Affected Rows");
- Compare(NumberOfAffectedRows ,3 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
-
- //get result from db in order to check them
- DataSet dsExpected = new DataSet(); //ds.Reset();
- dbDA.Fill(dsExpected);
- dsExpected.Tables[0].PrimaryKey = new DataColumn[] {dsExpected.Tables[0].Columns["EmployeeID"]};
- CompareResults_Update(dsDB1,dsDB2,ref dbDA);
- CompareResults_Update_Dr_Exception(dsDB2,ref dbDA);
- //Create rows which not exists in the DB but exists in the DS with row state = deleted
- //this will cause the Update to fail.
- dsDB1.Tables[0].Rows.Add(new object[] {9997,"Ofer", "Borshtein", "Delete"});
- dsDB1.Tables[0].Rows.Add(new object[] {9998,"Ofer", "Borshtein", "Delete"});
- dsDB1.AcceptChanges();
- dsDB1.Tables[0].Rows.Find(9997).Delete();
- dsDB1.Tables[0].Rows.Find(9998)[1] = "Updated!";
-
- drArr = new DataRow[dsDB1.Tables[0].Rows.Count];
- dsDB1.Tables[0].Rows.CopyTo(drArr,0);
- //Check Sys.Data.DBConcurrencyException
- //The exception that is thrown by the DataAdapter during the update operation if the number of rows affected equals zero.
- try {
- BeginCase("Check DBConcurrencyException");
- try {
- NumberOfAffectedRows = dbDA.Update(drArr);
- }
- catch (DBConcurrencyException ex) {exp=ex;}
- Compare(exp.GetType(),typeof(DBConcurrencyException) );
- exp = null;
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- //close connection
- if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
- ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
- }
- private void CompareResults_Update_Dr_Exception(DataSet dsResultException,ref Sys.Data.Common.DbDataAdapter dbDA) {
- int NumberOfAffectedRows = 0;
- Exception exp = null;
- Exception e = null;
-
- DataRow[] drArr = new DataRow[dsResultException.Tables[0].Rows.Count];
-
- // --------- check for DBConcurrencyException /UniqueConstraint -----------------
- // call AcceptChanges after each exception check in order to make sure that we check only the the current row
- try {
- BeginCase("DBConcurrencyException - Delete");
- dsResultException.Tables[0].Rows.Find(9994).Delete();
- dsResultException.Tables[0].Rows.CopyTo(drArr,0);
- //no row with row version delete exists - records affected = 0
- NumberOfAffectedRows = -1;
- try {NumberOfAffectedRows = dbDA.Update(drArr);}
- catch (DBConcurrencyException dbExp){e = dbExp;}
- Compare(e.GetType(),typeof(DBConcurrencyException));
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null; e = null;}
- try {
- BeginCase("Number Of Affected Rows - Delete Exception");
- Compare(NumberOfAffectedRows ,-1 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- dsResultException.AcceptChanges();
- try {
- BeginCase("DBConcurrencyException - Update");
- dsResultException.Tables[0].Rows.Find(9995)["Title"] = "Jack the ripper";
- dsResultException.Tables[0].Rows.CopyTo(drArr,0);
- //no row with row version Update exists - records affected = 0
- NumberOfAffectedRows = -1;
- try {NumberOfAffectedRows = dbDA.Update(drArr);}
- catch (DBConcurrencyException dbExp){e = dbExp;}
- Compare(e.GetType(),typeof(DBConcurrencyException));
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null; e = null;}
- try {
- BeginCase("Number Of Affected Rows - Update Exception");
- Compare(NumberOfAffectedRows ,-1 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- dsResultException.AcceptChanges();
- try {
- BeginCase("DBConcurrencyException - Insert");
- dsResultException.Tables[0].Rows.Add(new object[] {9996,"Ofer","Borshtein","Insert"});
- dsResultException.Tables[0].Rows.CopyTo(drArr,0);
- //no row with row version Insert exists - records affected = 0
- NumberOfAffectedRows = -1;
- try {NumberOfAffectedRows = dbDA.Update(drArr);}
- catch (Exception dbExp){e = dbExp;} //throw Sys.Exception
- Compare(e != null ,true);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null; e = null;}
- try {
- BeginCase("Number Of Affected Rows - Insert Exception");
- Compare(NumberOfAffectedRows ,-1 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- dsResultException.AcceptChanges();
- }
- protected void DbDataAdapter_Update_Ds_Str(Sys.Data.Common.DbDataAdapter dbDA) {
- int NumberOfAffectedRows = 0;
- Exception exp = null;
- // --------- get data from DB -----------------
- DataSet ds = PrepareDBData_Update(dbDA);
- // --------- prepare dataset for update method -----------------
- DataSet dsDB1 = ds.Copy();
-
- // --------- prepare dataset for DBConcurrencyException -----------------
- DataSet dsDB2 = ds.Copy();
-
- //update dataset
- dsDB2.Tables[0].Rows.Add(new object[] {9994,"Ofer", "Borshtein", "Delete"});
- dsDB2.Tables[0].Rows.Add(new object[] {9995,"Ofer", "Borshtein", "Update"});
- dsDB2.Tables[0].Rows.Find(9996).Delete();
- dsDB2.AcceptChanges();
-
- dsDB1.Tables[0].Rows.Add(new object[] {9991,"Ofer","Borshtein","Insert"});
- dsDB1.Tables[0].Rows.Find(9992).Delete();
- dsDB1.Tables[0].Rows.Find(9993)["Title"] = "Jack the ripper";
- //execute update to db
- NumberOfAffectedRows = dbDA.Update(dsDB1,dsDB1.Tables[0].TableName);
-
- try {
- BeginCase("Number Of Affected Rows");
- Compare(NumberOfAffectedRows ,3 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
-
- //get result from db in order to check them
- DataSet dsExpected = new DataSet(); //ds.Reset();
- dbDA.Fill(dsExpected);
- dsExpected.Tables[0].PrimaryKey = new DataColumn[] {dsExpected.Tables[0].Columns["EmployeeID"]};
- CompareResults_Update(dsDB1,dsDB2,ref dbDA);
- CompareResults_Update_Ds_Str_Exception(dsDB2,ref dbDA);
- //Create rows which not exists in the DB but exists in the DS with row state = deleted
- //this will cause the Update to fail.
- dsDB1.Tables[0].Rows.Add(new object[] {9997,"Ofer", "Borshtein", "Delete"});
- dsDB1.Tables[0].Rows.Add(new object[] {9998,"Ofer", "Borshtein", "Delete"});
- dsDB1.AcceptChanges();
- dsDB1.Tables[0].Rows.Find(9997).Delete();
- dsDB1.Tables[0].Rows.Find(9998).Delete();
- //Check Sys.Data.DBConcurrencyException
- //The exception that is thrown by the DataAdapter during the update operation if the number of rows affected equals zero.
- try {
- BeginCase("Check DBConcurrencyException");
- try {
- NumberOfAffectedRows = dbDA.Update(dsDB1,dsDB1.Tables[0].TableName);
- }
- catch (DBConcurrencyException ex) {exp=ex;}
- Compare(exp.GetType(),typeof(DBConcurrencyException) );
- exp = null;
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- //close connection
- if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
- ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
- }
- private void CompareResults_Update_Ds_Str_Exception(DataSet dsResultException,ref Sys.Data.Common.DbDataAdapter dbDA) {
- int NumberOfAffectedRows = 0;
- Exception exp = null;
- Exception e = null;
-
- // --------- check for DBConcurrencyException /UniqueConstraint -----------------
- // call AcceptChanges after each exception check in order to make sure that we check only the the current row
- try {
- BeginCase("DBConcurrencyException - Delete");
- dsResultException.Tables[0].Rows.Find(9994).Delete();
- //no row with row version delete exists - records affected = 0
- NumberOfAffectedRows = -1;
- try {NumberOfAffectedRows = dbDA.Update(dsResultException,dsResultException.Tables[0].TableName);}
- catch (DBConcurrencyException dbExp){e = dbExp;}
- Compare(e.GetType(),typeof(DBConcurrencyException));
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null; e = null;}
- try {
- BeginCase("Number Of Affected Rows - Delete Exception");
- Compare(NumberOfAffectedRows ,-1 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- dsResultException.AcceptChanges();
- try {
- BeginCase("DBConcurrencyException - Update");
- dsResultException.Tables[0].Rows.Find(9995)["Title"] = "Jack the ripper";
- //no row with row version Update exists - records affected = 0
- NumberOfAffectedRows = -1;
- try {NumberOfAffectedRows = dbDA.Update(dsResultException,dsResultException.Tables[0].TableName);}
- catch (DBConcurrencyException dbExp){e = dbExp;}
- Compare(e.GetType(),typeof(DBConcurrencyException));
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null; e = null;}
- try {
- BeginCase("Number Of Affected Rows - Update Exception");
- Compare(NumberOfAffectedRows ,-1 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- dsResultException.AcceptChanges();
- try {
- BeginCase("DBConcurrencyException - Insert");
- dsResultException.Tables[0].Rows.Add(new object[] {9996,"Ofer","Borshtein","Insert"});
- //no row with row version Insert exists - records affected = 0
- NumberOfAffectedRows = -1;
- try {NumberOfAffectedRows = dbDA.Update(dsResultException,dsResultException.Tables[0].TableName);}
- catch (Exception dbExp){e = dbExp;} //throw Sys.Exception
- Compare(e != null ,true);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null; e = null;}
- try {
- BeginCase("Number Of Affected Rows - Insert Exception");
- Compare(NumberOfAffectedRows ,-1 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- dsResultException.AcceptChanges();
- }
- /*
- *
- * insert/update the database with data that will be used in testings
- *
- */
- protected void PrepareDataForTesting(string ConnectionString) {
- int iExists = 0;
- try {
- //create database specific date value
- string strBirthDateValue = "";
- switch (ConnectedDataProvider.GetDbType(ConnectionString)) {
- case DataBaseServer.DB2:
- strBirthDateValue = "'1988-05-31-15.33.44'";
- break;
- case DataBaseServer.Oracle:
- strBirthDateValue = "to_date('1988-05-31 15:33:44', 'yyyy-mm-dd HH24:mi:ss')";
- break;
- case DataBaseServer.PostgreSQL:
- strBirthDateValue = "to_timestamp('1988-05-31 15:33:44', 'yyyy-mm-dd HH24:mi:ss')";
- break;
- case DataBaseServer.SQLServer:
- case DataBaseServer.Sybase:
- strBirthDateValue = "'1988-May-31 15:33:44'";
- break;
- }
- OleDbConnection con = new OleDbConnection(ConnectionString);
- try {
- con.Open();
- }
- catch (Exception ex) {
- throw new Exception("PrepareDataForTesting failed trying to connect to DB using Connection string:" + ConnectionString + "\nException:" + ex.ToString(),ex);
- }
- OleDbCommand cmd = new OleDbCommand("",con);
- //update / insert to table Employees
- for (int i = 100; i <= 700; i +=100) {
- cmd.CommandText = "select 1 from Employees where EmployeeID = " + i.ToString();
- iExists = Sys.Convert.ToInt32(cmd.ExecuteScalar());
- if (iExists != 1) {
- cmd.CommandText = "insert into Employees (EmployeeID, LastName, FirstName, Title, BirthDate) " +
- " Values (" + i.ToString() +
- ",'Last" + i.ToString()
- + "','First" + i.ToString()
- + "', null, "
- + strBirthDateValue + ")";
- cmd.ExecuteNonQuery();
- }
- else {
- cmd.CommandText = "update Employees set "
- + " LastName = 'Last" + i.ToString()
- + "', FirstName = 'First" + i.ToString()
- + "', Title = null, BirthDate = "
- + strBirthDateValue
- + " where EmployeeID = " + i.ToString() ;
- Log(cmd.CommandText);
- cmd.ExecuteNonQuery();
- }
- }
- //update / insert to table Customers
- for (int i = 100; i <= 700; i +=100) {
- cmd.CommandText = "select 1 from Customers where CustomerID = 'GH" + i.ToString() + "'";
- iExists = Sys.Convert.ToInt32(cmd.ExecuteScalar());
- if (iExists != 1) {
- cmd.CommandText = "insert into Customers (CustomerID , CompanyName, Phone) Values ('GH" + i.ToString() + "','Company" + i.ToString() + "','00-" + i.ToString() + i.ToString() + "')";
- cmd.ExecuteNonQuery();
- }
- else {
- cmd.CommandText = "update Customers set CompanyName = 'Company" + i.ToString() + "', Phone = '00-" + i.ToString() + i.ToString() + "' where CustomerID = 'GH" + i.ToString() + "'";
- cmd.ExecuteNonQuery();
- }
- }
- cmd.CommandText = "select 1 from Customers where CustomerID = 'GH200'";
- iExists = Sys.Convert.ToInt32(cmd.ExecuteScalar());
- if (iExists != 1) {
- cmd.CommandText = "insert into Customers (CustomerID , CompanyName) Values ('GH200','Company200')";
- cmd.ExecuteNonQuery();
- }
- else {
- cmd.CommandText = "update Customers set CompanyName = 'Company200' where CustomerID = 'GH200'";
- cmd.ExecuteNonQuery();
- }
- con.Close();
- }
- catch (Exception ex) {
- throw new Exception("PrepareDataForTesting failed with exception:" + ex.ToString(),ex);
- }
- }
- /*
- * used to insert data to the database in order to check DataAdapter Update metods
- */
- protected DataSet PrepareDBData_Update(Sys.Data.Common.DbDataAdapter dbDA) {
- return PrepareDBData_Update(dbDA,false);
- }
- protected DataSet PrepareDBData_Update(Sys.Data.Common.DbDataAdapter dbDA,bool sqlConnectionString) {
- IDbDataAdapter Ida = (IDbDataAdapter)dbDA;
- IDbCommand ICmd = Ida.SelectCommand;
- ICmd.CommandText = "SELECT EmployeeID, LastName, FirstName, Title FROM Employees WHERE EmployeeID in (9991,9992,9993,9994,9995,9996)";
- IDbConnection IConn = ICmd.Connection;
- if (!sqlConnectionString) {
- IConn.ConnectionString = MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString;
- }
- IConn.Open();
-
- //Insert rows to be updated (insert,delete,update)
- IDbCommand cmd;
- if (sqlConnectionString) {
- cmd = new Sys.Data.SqlClient.SqlCommand();
- cmd.Connection = (Sys.Data.SqlClient.SqlConnection)IConn;
- }
- else {
- cmd = new Sys.Data.OleDb.OleDbCommand();
- cmd.Connection = (Sys.Data.OleDb.OleDbConnection)IConn;
- }
- //run execute after each command because DB2 doesn't support multiple commands
- cmd.CommandText = "DELETE FROM Employees WHERE EmployeeID in (9991,9992,9993,9994,9995,9996,9997,9998)";
- cmd.ExecuteNonQuery();
- //only for SQL Server
- DataBaseServer DBType = ConnectedDataProvider.GetDbType(IConn.ConnectionString);
- cmd.CommandText = "INSERT INTO Employees (EmployeeID, LastName, FirstName, Title) VALUES(9992, 'Ofer', 'Borshtein', 'delete')";
- //if (DBType == DataBaseServer.SQLServer) cmd.CommandText = "SET IDENTITY_INSERT Employees ON;" + cmd.CommandText;
- cmd.ExecuteNonQuery();
- cmd.CommandText = "INSERT INTO Employees (EmployeeID, LastName, FirstName, Title) VALUES(9993, 'Ofer', 'Borshtein', 'Update')";
- //if (DBType == DataBaseServer.SQLServer) cmd.CommandText = "SET IDENTITY_INSERT Employees ON;" + cmd.CommandText;
- cmd.ExecuteNonQuery();
- cmd.CommandText = "INSERT INTO Employees (EmployeeID, LastName, FirstName, Title) VALUES(9996, 'Ofer', 'Borshtein', 'Exp')";
- //if (DBType == DataBaseServer.SQLServer) cmd.CommandText = "SET IDENTITY_INSERT Employees ON;" + cmd.CommandText;
- cmd.ExecuteNonQuery();
- //cmd.CommandText += "INSERT INTO Employees (EmployeeID, LastName, FirstName, Title) VALUES(9991, 'Ofer', 'Borshtein', 'Insert'); ";
- //cmd.CommandText += "INSERT INTO Employees (EmployeeID, LastName, FirstName, Title) VALUES(9994, 'Ofer', 'Borshtein', 'Exp'); ";
- //cmd.CommandText += "INSERT INTO Employees (EmployeeID, LastName, FirstName, Title) VALUES(9995, 'Ofer', 'Borshtein', 'Exp'); ";
- //cmd.CommandText += "INSERT INTO Employees (EmployeeID, LastName, FirstName, Title) VALUES(9997, 'Ofer', 'Borshtein', 'delete'); ";
- //cmd.CommandText += "INSERT INTO Employees (EmployeeID, LastName, FirstName, Title) VALUES(9998, 'Ofer', 'Borshtein', 'delete'); ";
- DataSet ds = new DataSet();
- dbDA.Fill(ds);
- ds.Tables[0].PrimaryKey = new DataColumn[] {ds.Tables[0].Columns["EmployeeID"]};
- return ds;
- }
- private void CompareResults_Update(DataSet dsResult,DataSet dsResultException,ref Sys.Data.Common.DbDataAdapter dbDA) {
- Exception exp = null;
-
- //----------------- Compare dsDB with dsExcepted -----------------
- try {
- BeginCase("Insert Row ");
- Compare(dsResult.Tables[0].Rows.Find(9991) == null ,false );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- try {
- BeginCase("Update Row ");
- Compare(dsResult.Tables[0].Rows.Find(9993)["Title"],"Jack the ripper");
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
-
- try {
- BeginCase("Delete Row ");
- Compare(dsResult.Tables[0].Rows.Find(9992) ,null);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- }
- #endregion
- protected void DBDataAdapter_DefaultSourceTableName() {
- Exception exp = null;
- try {
- BeginCase("DefaultSourceTableName");
- Compare(Sys.Data.Common.DbDataAdapter.DefaultSourceTableName , "Table");
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- }
- #endregion
- #region "----------- Sys.Data.Common.DataAdapter --------------"
- protected void DataAdapter_AcceptChangesDuringFill(Sys.Data.Common.DbDataAdapter dbDA) {
- Exception exp = null;
- IDbDataAdapter Ida = (IDbDataAdapter)dbDA;
- IDbCommand ICmd = Ida.SelectCommand;
- IDbConnection IConn = ICmd.Connection;
- IConn.ConnectionString = MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString;
- IConn.Open();
- PrepareDataForTesting( MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
- //get the total rows count
- ICmd.CommandText = "SELECT Count(*) FROM Customers where CustomerID in ('GH100','GH200','GH300','GH400','GH500','GH600','GH700')";
- int ExpectedRows = Sys.Convert.ToInt32(ICmd.ExecuteScalar());
- try {
- BeginCase("Check that Expected rows count > 0");
- Compare(ExpectedRows > 0 ,true);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- ICmd.CommandText = "SELECT CustomerID, CompanyName, City, Country, Phone FROM Customers where CustomerID in ('GH100','GH200','GH300','GH400','GH500','GH600','GH700')";
- DataSet ds = new DataSet();
- dbDA.AcceptChangesDuringFill = false;
-
- try {
- BeginCase("Execute Fill - check return rows count");
- int i = dbDA.Fill(ds);
- Compare(i ,ExpectedRows );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
-
- bool blnAcceptChanges = false;
- foreach (DataRow dr in ds.Tables[0].Rows) {
- if (dr.RowState != DataRowState.Added ) {
- blnAcceptChanges = true;
- break;
- }
- }
- try {
- BeginCase("AcceptChangesDuringFill - false");
- Compare(blnAcceptChanges ,false );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- ds.Reset();
- dbDA.AcceptChangesDuringFill = true;
- dbDA.Fill(ds);
- blnAcceptChanges = false;
- foreach (DataRow dr in ds.Tables[0].Rows) {
- if (dr.RowState != DataRowState.Unchanged ) {
- blnAcceptChanges = true;
- break;
- }
- }
- try {
- BeginCase("AcceptChangesDuringFill - true");
- Compare(blnAcceptChanges ,false );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- //close connection
- if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
- ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
- }
- protected void DataAdapter_ContinueUpdateOnError(Sys.Data.Common.DbDataAdapter dbDA) {
- /*
- !!!!!! Not working (TestName "ContinueUpdateOnError - true, check value 2")!!!!!
- If ContinueUpdateOnError is set to true, no exception is thrown when an error occurs during the update of a row.
- The update of the row is skipped and the error information is placed in the RowError property of the row in error.
- The DataAdapter continues to update subsequent rows.
- If ContinueUpdateOnError is set to false, an exception is thrown when an error occurs during the update of a row.
- */
- Exception exp = null;
-
- IDbDataAdapter Ida = (IDbDataAdapter)dbDA;
- IDbCommand ICmd = Ida.SelectCommand;
- IDbConnection IConn = ICmd.Connection;
- IConn.ConnectionString = MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString;
- IConn.Open();
- PrepareDataForTesting( MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
- //get the total rows count
- ICmd.CommandText = "SELECT Count(*) FROM Customers where CustomerID in ('GH100','GH200','GH300','GH400','GH500','GH600','GH700')";
- int ExpectedRows = Sys.Convert.ToInt32(ICmd.ExecuteScalar());
- try {
- BeginCase("Check that Expected rows count > 0");
- Compare(ExpectedRows > 0 ,true);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- ICmd.CommandText = "SELECT CustomerID, CompanyName, City, Country, Phone FROM Customers where CustomerID in ('GH100','GH200','GH300','GH400','GH500','GH600','GH700')";
- DataSet dsMem = new DataSet(); //Disconected dataset
- DataSet dsDB = new DataSet(); //DataBase data
- dbDA.AcceptChangesDuringFill = true;
- //get data from DB
- try {
- BeginCase("Execute Fill - check return rows count");
- int i = dbDA.Fill(dsMem);
- Compare(i ,ExpectedRows );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
-
- //update data with invalid information (Max. length for Phone is 24)
- // 123456789012345678901234
- string newValue1 = "Very Long String That Will Raise An Error Yep!";
- string oldValue1 = dsMem.Tables[0].Rows[3]["Phone"].ToString();
- string oldValue2 = dsMem.Tables[0].Rows[4]["Phone"].ToString();
- string newValue2 = "03-1234";
- dsMem.Tables[0].Rows[3]["Phone"] = newValue1;
- dsMem.Tables[0].Rows[4]["Phone"] = newValue2;
- dbDA.ContinueUpdateOnError = true;
-
- //will not throw exception
- try {
- BeginCase("ContinueUpdateOnError - true, check exception");
- try {
- dbDA.Update(dsMem);
- }
- catch(Exception ex){exp = ex;}
- Compare(exp == null,true);
- exp = null;
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- dbDA.Fill(dsDB); //get data from DB to check the update operation
- try {
- BeginCase("ContinueUpdateOnError - true, check RowError");
- Compare(dsMem.Tables[0].Rows[3].RowError.Length > 0 , true);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- try {
- BeginCase("ContinueUpdateOnError - true, check value 1");
- Compare(dsDB.Tables[0].Rows[3]["Phone"] , oldValue1);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- /* - Test excluded, it is not working in .NET too!
- //should continue the update
- try
- {
- BeginCase("ContinueUpdateOnError - true, check value 2");
- Compare(dsDB.Tables[0].Rows[4]["Phone"] , newValue2); //--------- NOT WORKING !!! -----------
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- */
- dsMem.Reset();
- dsDB.Reset();
- dbDA.Fill(dsMem);
- dsMem.Tables[0].Rows[3]["Phone"] = newValue1 ;
- dsMem.Tables[0].Rows[4]["Phone"] = newValue2;
- dbDA.ContinueUpdateOnError = false;
-
- try {
- BeginCase("ContinueUpdateOnError - false, check exception");
- try {
- dbDA.Update(dsMem);
- }
- catch(Exception ex){exp = ex;}
- Compare(exp == null,false);
- exp = null;
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- dbDA.Fill(dsDB); //get data from DB to check the update operation
- try {
- BeginCase("ContinueUpdateOnError - false,check RowError");
- Compare(dsMem.Tables[0].Rows[3].RowError.Length > 0 ,true);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- try {
- BeginCase("ContinueUpdateOnError - false,check value 1");
- Compare(dsDB.Tables[0].Rows[3]["Phone"] , oldValue1 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- try {
- BeginCase("ContinueUpdateOnError - false,check value 2");
- Compare(dsDB.Tables[0].Rows[4]["Phone"] , oldValue2 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- //close connection
- if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
- ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
- }
- protected void DataAdapter_MissingMappingAction(Sys.Data.Common.DbDataAdapter dbDA) {
- Exception exp = null;
- IDbDataAdapter Ida = (IDbDataAdapter)dbDA;
- IDbCommand ICmd = Ida.SelectCommand;
- IDbConnection IConn = ICmd.Connection;
- IConn.ConnectionString = MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString;
- IConn.Open();
- //get the total rows count
- PrepareDataForTesting( MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
- ICmd.CommandText = "SELECT Count(*) FROM Customers where CustomerID in ('GH100','GH200','GH300','GH400','GH500','GH600','GH700')";
- int ExpectedRows = Sys.Convert.ToInt32(ICmd.ExecuteScalar());
- try {
- BeginCase("Check that Expected rows count > 0");
- Compare(ExpectedRows > 0 ,true);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- ICmd.CommandText = "SELECT CustomerID, CompanyName, City, Country, Phone FROM Customers where CustomerID in ('GH100','GH200','GH300','GH400','GH500','GH600','GH700')";
- //init dataset
- DataSet ds = new DataSet();
- try {
- BeginCase("Execute Fill - check return rows count");
- int i = dbDA.Fill(ds);
- Compare(i ,ExpectedRows );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
-
- //make dataset schema mismatch with DB
- ds.Tables[0].Columns.Remove("Country");
- ds.Tables[0].Clear();
-
- //--- Default value ---
-
- try {
- BeginCase("MissingMappingAction Default value");
- Compare(dbDA.MissingMappingAction , MissingMappingAction.Passthrough);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
-
- //--- MissingMappingAction.Error ---
- ds.Tables[0].Clear();
- dbDA.MissingMappingAction = MissingMappingAction.Error ;
- Exception ExMissingMappingAction = null;
- try {
- BeginCase("MissingMappingAction.Error");
- try {
- dbDA.Fill(ds);
- }
- catch (InvalidOperationException e) {
- ExMissingMappingAction = e;
- }
- Compare(ExMissingMappingAction.GetType() ,typeof(InvalidOperationException));
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- try {
- BeginCase("MissingMappingAction.Error, Row.Count = 0");
- Compare(ds.Tables[0].Rows.Count , 0 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- try {
- BeginCase("MissingMappingAction.Error, Column");
- Compare(ds.Tables[0].Columns.IndexOf("Country") , -1 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
-
- //--- MissingMappingAction.Ignore ---
- ds.Tables[0].Clear();
- dbDA.MissingMappingAction = MissingMappingAction.Ignore ;
- ExMissingMappingAction = null;
- try {
- BeginCase("MissingMappingAction.Ignore");
- try {
- dbDA.Fill(ds);
- }
- catch (InvalidOperationException e) {
- ExMissingMappingAction = e;
- }
- Compare(ExMissingMappingAction ,null);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- try {
- BeginCase("MissingMappingAction.Ignore, Row.Count = 0");
- Compare(ds.Tables[0].Rows.Count ,0);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- try {
- BeginCase("MissingMappingAction.Ignore, Column");
- Compare(ds.Tables[0].Columns.IndexOf("Country") , -1 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- //--- MissingMappingAction.Passthrough ---
- ds.Tables[0].Clear();
- dbDA.MissingMappingAction = MissingMappingAction.Passthrough ;
- ExMissingMappingAction = null;
- try {
- BeginCase("MissingMappingAction.Passthrough");
- try {
- dbDA.Fill(ds);
- }
- catch (InvalidOperationException e) {
- ExMissingMappingAction = e;
- }
- Compare(ExMissingMappingAction ,null);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- try {
- BeginCase("MissingMappingAction.Passthrough, Row.Count > 0");
- Compare(ds.Tables[0].Rows.Count >= 0 ,true );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- try {
- BeginCase("MissingMappingAction.Passthrough, Column");
- Compare(ds.Tables[0].Columns.IndexOf("Country") >= 0 ,true );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- //close connection
- if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
- ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
- }
- protected void DataAdapter_MissingSchemaAction(Sys.Data.Common.DbDataAdapter dbDA) {
- Exception exp = null;
- IDbDataAdapter Ida = (IDbDataAdapter)dbDA;
- IDbCommand ICmd = Ida.SelectCommand;
- IDbConnection IConn = ICmd.Connection;
- IConn.ConnectionString = MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString;
- IConn.Open();
- PrepareDataForTesting( MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
- //get the total rows count
- ICmd.CommandText = "SELECT Count(*) FROM Customers where CustomerID in ('GH100','GH200','GH300','GH400','GH500','GH600','GH700')";
- int ExpectedRows = Sys.Convert.ToInt32(ICmd.ExecuteScalar());
- try {
- BeginCase("Check that Expected rows count > 0");
- Compare(ExpectedRows > 0 ,true);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- ICmd.CommandText = "SELECT CustomerID, CompanyName, City, Country, Phone FROM Customers where CustomerID in ('GH100','GH200','GH300','GH400','GH500','GH600','GH700')";
- //get db type
- DBType = ConnectedDataProvider.GetDbType(((IDbDataAdapter)dbDA).SelectCommand.Connection.ConnectionString);
- //init dataset
- DataSet ds = new DataSet();
- try {
- BeginCase("Execute Fill - check return rows count");
- int i = dbDA.Fill(ds);
- Compare(i ,ExpectedRows );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- //make dataset schema mismatch with DB
- ds.Tables[0].Columns.Remove("Country");
- ds.Tables[0].Clear();
-
- //--- Default value ---
-
- try {
- BeginCase("MissingSchemaAction Default value");
- Compare(dbDA.MissingSchemaAction, MissingSchemaAction.Add);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
-
- //--- MissingSchemaAction.Error ---
- ds.Tables[0].Clear();
- dbDA.MissingSchemaAction = MissingSchemaAction.Error ;
- Exception ExMissingSchemaAction = null;
- try {
- BeginCase("MissingSchemaAction.Error");
- try {
- dbDA.Fill(ds);
- }
- catch (InvalidOperationException e) {
- ExMissingSchemaAction = e;
- }
- Compare(ExMissingSchemaAction.GetType() ,typeof(InvalidOperationException));
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- try {
- BeginCase("MissingSchemaAction.Error, Row.Count = 0");
- Compare(ds.Tables[0].Rows.Count , 0 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- try {
- BeginCase("MissingSchemaAction.Error, Column");
- Compare(ds.Tables[0].Columns.IndexOf("Country") , -1 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
-
- //--- MissingSchemaAction.Ignore ---
- try {
- //catch any exception that might occure
- BeginCase("MissingSchemaAction.Ignore - invoke");
- ds.Tables[0].Clear();
- dbDA.MissingSchemaAction = MissingSchemaAction.Ignore ;
- ExMissingSchemaAction = null;
- dbDA.Fill(ds);
- Compare(true ,true);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- try {
- BeginCase("MissingSchemaAction.Ignore, Row.Count = 0");
- Compare(ds.Tables[0].Rows.Count > 0 ,true);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- try {
- BeginCase("MissingSchemaAction.Ignore, Column");
- Compare(ds.Tables[0].Columns.IndexOf("Country") , -1 );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- #if !KNOWN_BUG //BUG_NUM:1951
- try {
- BeginCase("MissingSchemaAction.Ignore, PrimaryKey");
- Compare(ds.Tables[0].PrimaryKey.Length == 0 ,true );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- #endif
- //--- MissingSchemaAction.Add ---
- try {
- //catch any exception that might occure
- BeginCase("MissingSchemaAction.Add - invoke");
- ds.Tables[0].Clear();
- dbDA.MissingSchemaAction = MissingSchemaAction.Add ;
- ExMissingSchemaAction = null;
- dbDA.Fill(ds);
- Compare(true ,true);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- try {
- BeginCase("MissingSchemaAction.Add, Row.Count > 0");
- Compare(ds.Tables[0].Rows.Count >= 0 ,true );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- try {
- BeginCase("MissingSchemaAction.Add, Column");
- Compare(ds.Tables[0].Columns.IndexOf("Country") >= 0 ,true );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- #if !KNOWN_BUG //BUG_NUM:1952
- //DB2 don't return primary key
- if (DBType != DataBaseServer.DB2) {
- try {
- BeginCase("MissingSchemaAction.AddWithKey, PrimaryKey");
- Compare(ds.Tables[0].PrimaryKey.Length ,0);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- }
- //--- MissingSchemaAction.AddWithKey ---
- try {
- //catch any exception that might occure
- BeginCase("MissingSchemaAction.AddWithKey - invoke");
- ds.Tables[0].Clear();
- ds.Tables[0].Columns.Remove("Country");
- dbDA.MissingSchemaAction = MissingSchemaAction.AddWithKey ;
- ExMissingSchemaAction = null;
- dbDA.Fill(ds);
- Compare(true ,true);
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- try {
- BeginCase("MissingSchemaAction.AddWithKey, Row.Count > 0");
- Compare(ds.Tables[0].Rows.Count >= 0 ,true );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- try {
- BeginCase("MissingSchemaAction.AddWithKey, Column");
- Compare(ds.Tables[0].Columns.IndexOf("Country") >= 0 ,true );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- //DB2 don't return primary key
- if (DBType != DataBaseServer.DB2 &&
- DBType != DataBaseServer.Oracle) {
- try {
- BeginCase("MissingSchemaAction.AddWithKey, PrimaryKey");
- Compare(ds.Tables[0].PrimaryKey.Length > 0 ,true );
- }
- catch(Exception ex) {exp = ex;}
- finally {EndCase(exp); exp = null;}
- }
- #endif
- //close connection
- if ( ((IDbDataAdapter)dbDA).SelectCommand.Connection.State != ConnectionState.Closed )
- ((IDbDataAdapter)dbDA).SelectCommand.Connection.Close();
- }
- #endregion
- public override void BeginTest(string testName) {
- base.BeginTest (testName);
- Log(String.Format("DataBase Type is {0}", ConnectedDataProvider.GetDbType()));
- }
- }
- }
|