2 // Rafael Mizrahi <rafim@mainsoft.com>
3 // Erez Lotan <erezl@mainsoft.com>
4 // Oren Gurfinkel <oreng@mainsoft.com>
6 // Veerapuram Varadhan <vvaradhan@novell.com>
8 // Copyright (c) 2004 Mainsoft Co.
9 // Copyright (c) 2009 Novell Inc.
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using NUnit.Framework;
36 using MonoTests.System.Data.Utils;
38 using System.Runtime.Serialization;
39 using System.Runtime.Serialization.Formatters.Binary;
40 using System.Globalization;
42 namespace MonoTests_System.Data
45 public class DataSetTest2
47 private DataSet m_ds = null;
48 private bool EventRaised = false;
50 [Test] public void AcceptChanges()
52 DataSet ds = new DataSet();
53 DataTable dtP = DataProvider.CreateParentDataTable();
54 DataTable dtC = DataProvider.CreateChildDataTable();
57 ds.Relations.Add(new DataRelation("myRelation",dtP.Columns[0],dtC.Columns[0]));
60 dtP.Rows[0][0] = "70";
62 dtP.Rows.Add(new object[] {9,"string1","string2"});
66 Assert.AreEqual(null, dtP.GetChanges(), "DS1");
69 dtP.Columns[0].ReadOnly = true;
70 // check ReadOnlyException
74 Assert.Fail("DS2: Indexer Failed to throw ReadOnlyException");
76 catch (ReadOnlyException) {}
77 catch (AssertionException exc) {throw exc;}
80 Assert.Fail("DS3: Indexer. Wrong exception type. Got:" + exc);
83 // check invoke AcceptChanges
87 [Test] public void CaseSensitive()
89 DataSet ds = new DataSet();
90 DataTable dt = new DataTable();
92 // CaseSensitive - default value (false)
93 Assert.AreEqual(false , ds.CaseSensitive , "DS4");
95 ds.CaseSensitive = true;
97 // CaseSensitive - get
98 Assert.AreEqual(true , ds.CaseSensitive , "DS5");
100 //add a datatable to a dataset
102 // DataTable CaseSensitive from DataSet - true
103 Assert.AreEqual(true , dt.CaseSensitive , "DS6");
106 ds.CaseSensitive = false;
107 dt = new DataTable();
110 // DataTable CaseSensitive from DataSet - false
111 Assert.AreEqual(false, dt.CaseSensitive , "DS7");
113 //change DataSet CaseSensitive and check DataTables in it
115 ds.CaseSensitive = false;
116 dt = new DataTable();
119 // Change DataSet CaseSensitive - check Table - true
120 ds.CaseSensitive = true;
121 Assert.AreEqual(true, dt.CaseSensitive , "DS8");
123 // Change DataSet CaseSensitive - check Table - false
124 ds.CaseSensitive = false;
125 Assert.AreEqual(false, dt.CaseSensitive , "DS9");
127 //Add new table to DataSet with CaseSensitive,check the table case after adding it to DataSet
129 ds.CaseSensitive = true;
130 dt = new DataTable();
131 dt.CaseSensitive = false;
134 // DataTable get case sensitive from DataSet - false
135 Assert.AreEqual(false, dt.CaseSensitive , "DS10");
138 ds.CaseSensitive = false;
139 dt = new DataTable();
140 dt.CaseSensitive = true;
143 // DataTable get case sensitive from DataSet - true
144 Assert.AreEqual(true, dt.CaseSensitive , "DS11");
146 //Add new table to DataSet and change the DataTable CaseSensitive
148 ds.CaseSensitive = true;
149 dt = new DataTable();
152 // Add new table to DataSet and change the DataTable CaseSensitive - false
153 dt.CaseSensitive = false;
154 Assert.AreEqual(false, dt.CaseSensitive , "DS12");
157 ds.CaseSensitive = false;
158 dt = new DataTable();
161 // Add new table to DataSet and change the DataTable CaseSensitive - true
162 dt.CaseSensitive = true;
163 Assert.AreEqual(true, dt.CaseSensitive , "DS13");
165 //Add DataTable to Dataset, Change DataSet CaseSensitive, check DataTable
167 ds.CaseSensitive = true;
168 dt = new DataTable();
169 dt.CaseSensitive = true;
172 // Add DataTable to Dataset, Change DataSet CaseSensitive, check DataTable - true
173 ds.CaseSensitive = false;
174 Assert.AreEqual(true, dt.CaseSensitive , "DS14");
177 [Test] public void Clear()
179 DataSet ds = new DataSet();
180 ds.Tables.Add(DataProvider.CreateParentDataTable());
181 ds.Tables[0].Rows.Add(new object[] {9,"",""});
185 Assert.AreEqual(0, ds.Tables[0].Rows.Count , "DS15");
188 [Test] public void Clear_WithNoDataWithConstraint()
190 // Test dataset with no data and with constraint
191 DataSet ds = new DataSet();
192 ds.Tables.Add(DataProvider.CreateParentDataTable());
193 ds.Tables.Add(DataProvider.CreateChildDataTable());
194 ds.Tables[0].Rows.Clear();
195 ds.Tables[1].Rows.Clear();
197 ds.Tables[0].Constraints.Add("test",ds.Tables[1].Columns[0],ds.Tables[0].Columns[0]);
201 [Test] public void Clone()
203 DataSet ds = new DataSet(), dsTarget = null;
204 ds.Tables.Add(DataProvider.CreateParentDataTable());
205 ds.Tables.Add(DataProvider.CreateChildDataTable());
206 ds.Relations.Add(new DataRelation("myRelation",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
207 ds.Tables[0].Rows.Add(new object[] {9,"",""});
208 ds.Tables[1].Columns[2].ReadOnly = true;
209 ds.Tables[0].PrimaryKey = new DataColumn[] {ds.Tables[0].Columns[0],ds.Tables[0].Columns[1]};
211 //copy schema only, no data
214 dsTarget = ds.Clone();
215 //Assert.AreEqual(ds.GetXmlSchema(), dsTarget.GetXmlSchema() , "DS16");
216 //use my function because GetXmlSchema not implemented in java
217 Assert.AreEqual(DataProvider.GetDSSchema(ds), DataProvider.GetDSSchema(dsTarget), "DS17");
220 Assert.AreEqual(false, dsTarget.GetXml() == ds.GetXml(), "DS18");
223 [Test] public void Copy()
225 DataSet ds = new DataSet(), dsTarget = null;
226 ds.Tables.Add(DataProvider.CreateParentDataTable());
227 ds.Tables.Add(DataProvider.CreateChildDataTable());
228 ds.Relations.Add(new DataRelation("myRelation",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
229 ds.Tables[0].Rows.Add(new object[] {9,"",""});
230 ds.Tables[1].Columns[2].ReadOnly = true;
231 ds.Tables[0].PrimaryKey = new DataColumn[] {ds.Tables[0].Columns[0],ds.Tables[0].Columns[1]};
233 //copy data and schema
236 dsTarget = ds.Copy();
237 //Assert.AreEqual(ds.GetXmlSchema(), dsTarget.GetXmlSchema() , "DS19");
238 //using my function because GetXmlSchema in not implemented in java
239 Assert.AreEqual(DataProvider.GetDSSchema(ds), DataProvider.GetDSSchema (dsTarget) , "DS20");
242 Assert.AreEqual(true, dsTarget.GetXml() == ds.GetXml(), "DS21");
245 [Test] public void DataSetName()
247 DataSet ds = new DataSet();
249 // DataSetName - default value
250 Assert.AreEqual("NewDataSet" , ds.DataSetName , "DS22");
252 ds.DataSetName = "NewName";
255 Assert.AreEqual("NewName" , ds.DataSetName , "DS23");
258 [Test] public void EnforceConstraints()
260 DataSet ds = new DataSet();
262 // EnforceConstraints - default value (true)
263 Assert.AreEqual(true, ds.EnforceConstraints , "DS24");
265 ds.EnforceConstraints = false;
267 // EnforceConstraints - get
268 Assert.AreEqual(false, ds.EnforceConstraints , "DS25");
272 public void EnforceConstraints_CheckPrimaryConstraint ()
274 DataSet ds = new DataSet();
275 ds.Tables.Add ("table");
276 ds.Tables [0].Columns.Add ("col");
277 ds.Tables [0].PrimaryKey = new DataColumn[] {ds.Tables [0].Columns [0]};
278 ds.EnforceConstraints = false;
279 ds.Tables [0].Rows.Add (new object[] {null});
281 ds.EnforceConstraints = true;
283 } catch (ConstraintException e) {
284 // Never premise English.
285 //Assert.AreEqual ("Failed to enable constraints. One or more rows contain values " +
286 // "violating non-null, unique, or foreign-key constraints.", e.Message, "#2");
291 public void EnforceConstraints_NonNullCols ()
293 DataSet ds = new DataSet();
294 ds.Tables.Add ("table");
295 ds.Tables [0].Columns.Add ("col");
296 ds.Tables [0].Columns [0].AllowDBNull = false;
298 ds.EnforceConstraints = false;
299 ds.Tables [0].Rows.Add (new object[] {null});
301 ds.EnforceConstraints = true;
303 } catch (ConstraintException e) {
304 // Never premise English.
305 //Assert.AreEqual ("Failed to enable constraints. One or more rows contain values " +
306 // "violating non-null, unique, or foreign-key constraints.", e.Message, "#2");
310 [Test] public void GetChanges()
312 DataSet ds = new DataSet();
313 ds.Tables.Add(DataProvider.CreateParentDataTable());
316 Assert.AreEqual(null , ds.GetChanges(), "DS26");
318 DataRow dr = ds.Tables[0].NewRow();
320 ds.Tables[0].Rows.Add(dr);
323 Assert.AreEqual(true , ds.GetChanges()!=null, "DS27");
326 Assert.AreEqual(dr.ItemArray, ds.GetChanges().Tables[0].Rows[0].ItemArray , "DS28");
329 [Test] public void GetChanges_ByDataRowState()
331 DataSet ds = new DataSet();
332 object[] arrAdded,arrDeleted,arrModified,arrUnchanged;
333 //object[] arrDetached;
336 ds.Tables.Add(DataProvider.CreateParentDataTable());
339 Assert.AreEqual(null , ds.GetChanges(), "DS29");
343 // can't check detached
344 // dr = ds.Tables[0].Rows[0];
345 // arrDetached = dr.ItemArray;
347 // ds.Tables[0].AcceptChanges();
349 dr= ds.Tables[0].Rows[1];
350 arrDeleted = dr.ItemArray;
353 dr = ds.Tables[0].Rows[2];
355 arrModified = dr.ItemArray;
357 dr = ds.Tables[0].Select("","",DataViewRowState.Unchanged)[0];
358 arrUnchanged = dr.ItemArray;
360 dr = ds.Tables[0].NewRow();
362 ds.Tables[0].Rows.Add(dr);
363 arrAdded = dr.ItemArray;
366 Assert.AreEqual(arrAdded, ds.GetChanges(DataRowState.Added).Tables[0].Rows[0].ItemArray , "DS30");
368 // GetChanges Deleted
369 dr = ds.GetChanges(DataRowState.Deleted).Tables[0].Rows[0];
370 object[] tmp = new object[] {dr[0,DataRowVersion.Original],dr[1,DataRowVersion.Original],dr[2,DataRowVersion.Original],dr[3,DataRowVersion.Original],dr[4,DataRowVersion.Original],dr[5,DataRowVersion.Original]};
371 Assert.AreEqual(arrDeleted, tmp, "DS31");
374 // // GetChanges Detached
375 // dr = ds.GetChanges(DataRowState.Detached).Tables[0].Rows[0];
376 // object[] tmp = new object[] {dr[0,DataRowVersion.Original],dr[1,DataRowVersion.Original],dr[2,DataRowVersion.Original]};
377 // Assert.AreEqual(arrDetached, tmp, "DS32");
379 // GetChanges Modified
380 Assert.AreEqual(arrModified, ds.GetChanges(DataRowState.Modified).Tables[0].Rows[0].ItemArray , "DS33");
382 // GetChanges Unchanged
383 Assert.AreEqual(arrUnchanged, ds.GetChanges(DataRowState.Unchanged).Tables[0].Rows[0].ItemArray , "DS34");
386 [Test] public void BeginInitTest ()
388 DataSet ds = new DataSet ();
390 DataTable table1 = new DataTable ("table1");
391 DataTable table2 = new DataTable ("table2");
393 DataColumn col1 = new DataColumn ("col1", typeof (int));
394 DataColumn col2 = new DataColumn ("col2", typeof (int));
395 table1.Columns.Add (col1);
396 table2.Columns.Add (col2);
398 UniqueConstraint pkey = new UniqueConstraint ("pk", new string[] {"col1"}, true);
399 ForeignKeyConstraint fkey = new ForeignKeyConstraint ("fk", "table1", new String[] {"col1"},
400 new String[] {"col2"}, AcceptRejectRule.Cascade,
401 Rule.Cascade, Rule.Cascade);
402 DataRelation relation = new DataRelation ("rel", "table1", "table2", new String[] {"col1"},
403 new String[] {"col2"}, false);
408 ds.Tables.AddRange (new DataTable[] {table1, table2});
409 ds.Relations.AddRange (new DataRelation[] {relation});
411 table1.Constraints.AddRange (new Constraint[] {pkey});
412 table2.Constraints.AddRange (new Constraint[] {fkey});
414 // The tables/relations shud not get added to the DataSet yet
415 Assert.AreEqual (0, ds.Tables.Count, "#1");
416 Assert.AreEqual (0, ds.Relations.Count, "#2");
417 Assert.AreEqual (0, table1.Constraints.Count, "#3");
418 Assert.AreEqual (0, table2.Constraints.Count, "#4");
421 Assert.AreEqual (2, ds.Tables.Count, "#5");
422 Assert.AreEqual (1, ds.Relations.Count, "#6");
423 Assert.AreEqual (1, ds.Tables [0].Constraints.Count, "#7");
424 Assert.AreEqual (1, ds.Tables [1].Constraints.Count, "#8");
426 // Table shud still be in BeginInit ..
427 DataColumn col3 = new DataColumn ("col2");
428 UniqueConstraint uc = new UniqueConstraint ("uc", new string[] {"col2"}, false);
430 table1.Columns.AddRange (new DataColumn[] {col3});
431 table1.Constraints.AddRange (new Constraint[] {uc});
433 Assert.AreEqual (1, table1.Columns.Count, "#9");
434 Assert.AreEqual (1, table1.Constraints.Count, "#10");
437 Assert.AreEqual (2, table1.Columns.Count, "#11");
438 Assert.AreEqual (2, table1.Columns.Count, "#12");
441 [Test] public void GetXml()
443 DataSet ds = new DataSet();
444 ds.Namespace = "namespace"; //if we don't add namespace the test will fail because GH (by design) always add namespace
445 DataTable dt = DataProvider.CreateParentDataTable();
447 dt.Rows.Add(new object[] {1,"Value1","Value2"});
448 dt.Rows.Add(new object[] {2,"Value3","Value4"});
449 dt.Rows.Add(new object[] {3,"Value5","Value5"});
451 System.Text.StringBuilder resultXML = new System.Text.StringBuilder();
453 resultXML.Append("<" + ds.DataSetName + "xmlns=\"namespace\">");
455 resultXML.Append("<Parent>");
456 resultXML.Append("<ParentId>1</ParentId>");
457 resultXML.Append("<String1>Value1</String1>");
458 resultXML.Append("<String2>Value2</String2>");
459 resultXML.Append("</Parent>");
461 resultXML.Append("<Parent>");
462 resultXML.Append("<ParentId>2</ParentId>");
463 resultXML.Append("<String1>Value3</String1>");
464 resultXML.Append("<String2>Value4</String2>");
465 resultXML.Append("</Parent>");
467 resultXML.Append("<Parent>");
468 resultXML.Append("<ParentId>3</ParentId>");
469 resultXML.Append("<String1>Value5</String1>");
470 resultXML.Append("<String2>Value5</String2>");
471 resultXML.Append("</Parent>");
473 resultXML.Append("</" + ds.DataSetName + ">");
476 string strXML = ds.GetXml();
477 strXML = strXML.Replace(" ","");
478 strXML = strXML.Replace("\t","");
479 strXML = strXML.Replace("\n","");
480 strXML = strXML.Replace("\r","");
483 Assert.AreEqual(resultXML.ToString() , strXML , "DS35");
486 [Test] public void HasChanges()
488 DataSet ds = new DataSet();
489 ds.Tables.Add(DataProvider.CreateParentDataTable());
492 Assert.AreEqual(false , ds.HasChanges(), "DS36");
494 DataRow dr = ds.Tables[0].NewRow();
496 ds.Tables[0].Rows.Add(dr);
499 Assert.AreEqual(true , ds.HasChanges(), "DS37");
502 [Test] public void HasChanges_ByDataRowState()
504 DataSet ds = new DataSet();
507 ds.Tables.Add(DataProvider.CreateParentDataTable());
510 Assert.AreEqual(false , ds.HasChanges(), "DS38");
514 dr= ds.Tables[0].Rows[1];
517 dr = ds.Tables[0].Rows[2];
520 dr = ds.Tables[0].Select("","",DataViewRowState.Unchanged)[0];
522 dr = ds.Tables[0].NewRow();
524 ds.Tables[0].Rows.Add(dr);
527 Assert.AreEqual(true , ds.HasChanges(DataRowState.Added), "DS39");
529 // HasChanges Deleted
530 Assert.AreEqual(true , ds.HasChanges(DataRowState.Deleted) , "DS40");
532 // HasChanges Modified
533 Assert.AreEqual(true, ds.HasChanges(DataRowState.Modified), "DS41");
535 // HasChanges Unchanged
536 Assert.AreEqual(true, ds.HasChanges(DataRowState.Unchanged), "DS42");
539 [Test] public void HasErrors()
541 DataSet ds = new DataSet();
542 ds.Tables.Add(DataProvider.CreateParentDataTable());
544 // HasErrors - default
545 Assert.AreEqual(false , ds.HasErrors , "DS43");
547 ds.Tables[0].Rows[0].RowError = "ErrDesc";
550 Assert.AreEqual(true , ds.HasErrors , "DS44");
553 #region test namespaces
555 [Test] public void InferXmlSchema_BasicXml()
557 StringBuilder sb = new StringBuilder();
558 sb.Append("<NewDataSet xmlns:od='urn:schemas-microsoft-com:officedata'>");
559 sb.Append("<Categories>");
560 sb.Append("<CategoryID od:adotype='3'>1</CategoryID>");
561 sb.Append("<CategoryName od:maxLength='15' od:adotype='130'>Beverages</CategoryName>");
562 sb.Append("<Description od:adotype='203'>Soft drinks and teas</Description>");
563 sb.Append("</Categories>");
564 sb.Append("<Products>");
565 sb.Append("<ProductID od:adotype='20'>1</ProductID>");
566 sb.Append("<ReorderLevel od:adotype='3'>10</ReorderLevel>");
567 sb.Append("<Discontinued od:adotype='11'>0</Discontinued>");
568 sb.Append("</Products>");
569 sb.Append("</NewDataSet>");
571 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
573 DataSet ds = new DataSet();
574 // ds.ReadXml(myStream);
575 ds.InferXmlSchema(myStream, new string[] {"urn:schemas-microsoft-com:officedata"});
576 Assert.AreEqual(2, ds.Tables.Count, "DS45");
577 Assert.AreEqual("CategoryID", ds.Tables[0].Columns[0].ColumnName, "DS46");
578 Assert.AreEqual("CategoryName", ds.Tables[0].Columns[1].ColumnName, "DS47");
579 Assert.AreEqual("Description", ds.Tables[0].Columns[2].ColumnName, "DS48");
581 Assert.AreEqual("ProductID", ds.Tables[1].Columns[0].ColumnName, "DS49");
582 Assert.AreEqual("ReorderLevel", ds.Tables[1].Columns[1].ColumnName, "DS50");
583 Assert.AreEqual("Discontinued", ds.Tables[1].Columns[2].ColumnName, "DS51");
586 [Test] public void InferXmlSchema_WithoutIgnoreNameSpaces()
588 StringBuilder sb = new StringBuilder();
589 sb.Append("<NewDataSet xmlns:od='urn:schemas-microsoft-com:officedata'>");
590 sb.Append("<Categories>");
591 sb.Append("<CategoryID od:adotype='3'>1</CategoryID>");
592 sb.Append("<CategoryName od:maxLength='15' od:adotype='130'>Beverages</CategoryName>");
593 sb.Append("<Description od:adotype='203'>Soft drinks and teas</Description>");
594 sb.Append("</Categories>");
595 sb.Append("<Products>");
596 sb.Append("<ProductID od:adotype='20'>1</ProductID>");
597 sb.Append("<ReorderLevel od:adotype='3'>10</ReorderLevel>");
598 sb.Append("<Discontinued od:adotype='11'>0</Discontinued>");
599 sb.Append("</Products>");
600 sb.Append("</NewDataSet>");
602 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
604 DataSet ds = new DataSet();
605 //ds.ReadXml(myStream);
606 ds.InferXmlSchema(myStream,new string[] {"urn:schemas-microsoft-com:officedata1"});
607 Assert.AreEqual(8, ds.Tables.Count, "DS52");
610 [Test] public void InferXmlSchema_IgnoreNameSpace()
612 StringBuilder sb = new StringBuilder();
613 sb.Append("<NewDataSet xmlns:od='urn:schemas-microsoft-com:officedata'>");
614 sb.Append("<Categories>");
615 sb.Append("<CategoryID od:adotype='3'>1</CategoryID>");
616 sb.Append("<CategoryName od:maxLength='15' adotype='130'>Beverages</CategoryName>");
617 sb.Append("<Description od:adotype='203'>Soft drinks and teas</Description>");
618 sb.Append("</Categories>");
619 sb.Append("<Products>");
620 sb.Append("<ProductID od:adotype='20'>1</ProductID>");
621 sb.Append("<ReorderLevel od:adotype='3'>10</ReorderLevel>");
622 sb.Append("<Discontinued od:adotype='11'>0</Discontinued>");
623 sb.Append("</Products>");
624 sb.Append("</NewDataSet>");
626 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
628 DataSet ds = new DataSet();
629 // ds.ReadXml(myStream);
630 ds.InferXmlSchema(myStream, new string[] {"urn:schemas-microsoft-com:officedata"});
631 Assert.AreEqual(3, ds.Tables.Count, "DS53");
633 Assert.AreEqual(3, ds.Tables[0].Columns.Count, "DS54");
634 Assert.AreEqual("CategoryID", ds.Tables[0].Columns["CategoryID"].ColumnName, "DS55");
635 Assert.AreEqual("Categories_Id", ds.Tables[0].Columns["Categories_Id"].ColumnName, "DS56");//Hidden
636 Assert.AreEqual("Description", ds.Tables[0].Columns["Description"].ColumnName, "DS57");
638 Assert.AreEqual(3, ds.Tables[1].Columns.Count, "DS58");
639 Assert.AreEqual("adotype", ds.Tables[1].Columns["adotype"].ColumnName, "DS59");
640 Assert.AreEqual("CategoryName_Text", ds.Tables[1].Columns["CategoryName_Text"].ColumnName, "DS60");
641 Assert.AreEqual("Categories_Id", ds.Tables[1].Columns["Categories_Id"].ColumnName, "DS61");//Hidden
643 Assert.AreEqual(3, ds.Tables[2].Columns.Count, "DS62");
644 Assert.AreEqual("ProductID", ds.Tables[2].Columns["ProductID"].ColumnName, "DS63");
645 Assert.AreEqual("ReorderLevel", ds.Tables[2].Columns["ReorderLevel"].ColumnName, "DS64");
646 Assert.AreEqual("Discontinued", ds.Tables[2].Columns["Discontinued"].ColumnName, "DS65");
649 [Test] public void InferXmlSchema_IgnoreNameSpaces() //Ignoring 2 namespaces
651 StringBuilder sb = new StringBuilder();
652 sb.Append("<h:html xmlns:xdc='http://www.xml.com/books' xmlns:h='http://www.w3.org/HTML/1998/html4'>");
653 sb.Append("<h:head><h:title>Book Review</h:title></h:head>");
654 sb.Append("<h:body>");
655 sb.Append("<xdc:bookreview>");
656 sb.Append("<xdc:title h:attrib1='1' xdc:attrib2='2' >XML: A Primer</xdc:title>");
657 sb.Append("</xdc:bookreview>");
658 sb.Append("</h:body>");
659 sb.Append("</h:html>");
661 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
662 DataSet tempDs = new DataSet();
663 tempDs.ReadXml(myStream);
664 myStream.Seek(0,SeekOrigin.Begin);
665 DataSet ds = new DataSet();
666 ds.InferXmlSchema(myStream, new string[] {"http://www.xml.com/books","http://www.w3.org/HTML/1998/html4"});
667 //Assert.AreEqual(8, ds.Tables.Count, "DS66");
669 // string str1 = tempDs.GetXmlSchema(); //DataProvider.GetDSSchema(tempDs);
670 // string str2 = ds.GetXmlSchema(); //DataProvider.GetDSSchema(ds);
671 Assert.AreEqual(3, ds.Tables.Count, "DS67");
672 Assert.AreEqual("bookreview", ds.Tables[2].TableName, "DS68");
673 Assert.AreEqual(2, ds.Tables[2].Columns.Count, "DS69");
677 #region inferingTables
678 [Test] public void InferXmlSchema_inferingTables1()
680 //Acroding to the msdn documantaion :
681 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
682 //Elements that have attributes specified in them will result in inferred tables
685 StringBuilder sb = new StringBuilder();
687 sb.Append("<DocumentElement>");
688 sb.Append("<Element1 attr1='value1'/>");
689 sb.Append("<Element1 attr1='value2'>Text1</Element1>");
690 sb.Append("</DocumentElement>");
691 DataSet ds = new DataSet();
692 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
693 ds.InferXmlSchema(myStream,null);
694 Assert.AreEqual("DocumentElement", ds.DataSetName, "DS70");
695 Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS71");
696 Assert.AreEqual(1, ds.Tables.Count, "DS72");
697 Assert.AreEqual("attr1", ds.Tables[0].Columns["attr1"].ColumnName, "DS73");
698 Assert.AreEqual("Element1_Text", ds.Tables[0].Columns["Element1_Text"].ColumnName, "DS74");
701 [Test] public void InferXmlSchema_inferingTables2()
703 //Acroding to the msdn documantaion :
704 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
705 //Elements that have child elements will result in inferred tables
708 StringBuilder sb = new StringBuilder();
710 sb.Append("<DocumentElement>");
711 sb.Append("<Element1>");
712 sb.Append("<ChildElement1>Text1</ChildElement1>");
713 sb.Append("</Element1>");
714 sb.Append("</DocumentElement>");
715 DataSet ds = new DataSet();
716 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
717 ds.InferXmlSchema(myStream,null);
718 Assert.AreEqual("DocumentElement", ds.DataSetName, "DS75");
719 Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS76");
720 Assert.AreEqual(1, ds.Tables.Count, "DS77");
721 Assert.AreEqual("ChildElement1", ds.Tables[0].Columns["ChildElement1"].ColumnName, "DS78");
724 [Test] public void InferXmlSchema_inferingTables3()
726 //Acroding to the msdn documantaion :
727 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
728 //The document, or root, element will result in an inferred table if it has attributes
729 //or child elements that will be inferred as columns.
730 //If the document element has no attributes and no child elements that would be inferred as columns, the element will be inferred as a DataSet
733 StringBuilder sb = new StringBuilder();
735 sb.Append("<DocumentElement>");
736 sb.Append("<Element1>Text1</Element1>");
737 sb.Append("<Element2>Text2</Element2>");
738 sb.Append("</DocumentElement>");
739 DataSet ds = new DataSet();
740 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
741 ds.InferXmlSchema(myStream,null);
742 Assert.AreEqual("NewDataSet", ds.DataSetName, "DS79");
743 Assert.AreEqual("DocumentElement", ds.Tables[0].TableName, "DS80");
744 Assert.AreEqual(1, ds.Tables.Count, "DS81");
745 Assert.AreEqual("Element1", ds.Tables[0].Columns["Element1"].ColumnName, "DS82");
746 Assert.AreEqual("Element2", ds.Tables[0].Columns["Element2"].ColumnName, "DS83");
749 [Test] public void InferXmlSchema_inferingTables4()
751 //Acroding to the msdn documantaion :
752 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
753 //The document, or root, element will result in an inferred table if it has attributes
754 //or child elements that will be inferred as columns.
755 //If the document element has no attributes and no child elements that would be inferred as columns, the element will be inferred as a DataSet
758 StringBuilder sb = new StringBuilder();
760 sb.Append("<DocumentElement>");
761 sb.Append("<Element1 attr1='value1' attr2='value2'/>");
762 sb.Append("</DocumentElement>");
763 DataSet ds = new DataSet();
764 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
765 ds.InferXmlSchema(myStream,null);
766 Assert.AreEqual("DocumentElement", ds.DataSetName, "DS84");
767 Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS85");
768 Assert.AreEqual(1, ds.Tables.Count, "DS86");
769 Assert.AreEqual("attr1", ds.Tables[0].Columns["attr1"].ColumnName, "DS87");
770 Assert.AreEqual("attr2", ds.Tables[0].Columns["attr2"].ColumnName, "DS88");
774 public void InferXmlSchema_inferingTables5()
776 //Acroding to the msdn documantaion :
777 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
778 //Elements that repeat will result in a single inferred table
781 StringBuilder sb = new StringBuilder();
783 sb.Append("<DocumentElement>");
784 sb.Append("<Element1>Text1</Element1>");
785 sb.Append("<Element1>Text2</Element1>");
786 sb.Append("</DocumentElement>");
787 DataSet ds = new DataSet();
788 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
789 ds.InferXmlSchema(myStream,null);
790 Assert.AreEqual("DocumentElement", ds.DataSetName, "DS89");
791 Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS90");
792 Assert.AreEqual(1, ds.Tables.Count, "DS91");
793 Assert.AreEqual("Element1_Text", ds.Tables[0].Columns["Element1_Text"].ColumnName, "DS92");
797 #region inferringColumns
798 [Test] public void InferXmlSchema_inferringColumns1()
800 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringcolumns.htm
802 StringBuilder sb = new StringBuilder();
804 sb.Append("<DocumentElement>");
805 sb.Append("<Element1 attr1='value1' attr2='value2'/>");
806 sb.Append("</DocumentElement>");
807 DataSet ds = new DataSet();
808 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
809 ds.InferXmlSchema(myStream,null);
810 Assert.AreEqual("DocumentElement", ds.DataSetName, "DS93");
811 Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS94");
812 Assert.AreEqual(1, ds.Tables.Count, "DS95");
813 Assert.AreEqual("attr1", ds.Tables[0].Columns["attr1"].ColumnName, "DS96");
814 Assert.AreEqual("attr2", ds.Tables[0].Columns["attr2"].ColumnName, "DS97");
815 Assert.AreEqual(MappingType.Attribute, ds.Tables[0].Columns["attr1"].ColumnMapping , "DS98");
816 Assert.AreEqual(MappingType.Attribute, ds.Tables[0].Columns["attr2"].ColumnMapping , "DS99");
817 Assert.AreEqual(typeof(string), ds.Tables[0].Columns["attr1"].DataType , "DS100");
818 Assert.AreEqual(typeof(string), ds.Tables[0].Columns["attr2"].DataType , "DS101");
821 [Test] public void InferXmlSchema_inferringColumns2()
823 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringcolumns.htm
824 //If an element has no child elements or attributes, it will be inferred as a column.
825 //The ColumnMapping property of the column will be set to MappingType.Element.
826 //The text for child elements is stored in a row in the table
829 StringBuilder sb = new StringBuilder();
831 sb.Append("<DocumentElement>");
832 sb.Append("<Element1>");
833 sb.Append("<ChildElement1>Text1</ChildElement1>");
834 sb.Append("<ChildElement2>Text2</ChildElement2>");
835 sb.Append("</Element1>");
836 sb.Append("</DocumentElement>");
837 DataSet ds = new DataSet();
838 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
839 ds.InferXmlSchema(myStream,null);
840 Assert.AreEqual("DocumentElement", ds.DataSetName, "DS102");
841 Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS103");
842 Assert.AreEqual(1, ds.Tables.Count, "DS104");
843 Assert.AreEqual("ChildElement1", ds.Tables[0].Columns["ChildElement1"].ColumnName, "DS105");
844 Assert.AreEqual("ChildElement2", ds.Tables[0].Columns["ChildElement2"].ColumnName, "DS106");
845 Assert.AreEqual(MappingType.Element, ds.Tables[0].Columns["ChildElement1"].ColumnMapping , "DS107");
846 Assert.AreEqual(MappingType.Element, ds.Tables[0].Columns["ChildElement2"].ColumnMapping , "DS108");
847 Assert.AreEqual(typeof(string), ds.Tables[0].Columns["ChildElement1"].DataType , "DS109");
848 Assert.AreEqual(typeof(string), ds.Tables[0].Columns["ChildElement2"].DataType , "DS110");
853 #region Inferring Relationships
855 [Test] public void InferXmlSchema_inferringRelationships1()
857 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringrelationships.htm
859 // inferringRelationships1
860 StringBuilder sb = new StringBuilder();
862 sb.Append("<DocumentElement>");
863 sb.Append("<Element1>");
864 sb.Append("<ChildElement1 attr1='value1' attr2='value2'/>");
865 sb.Append("<ChildElement2>Text2</ChildElement2>");
866 sb.Append("</Element1>");
867 sb.Append("</DocumentElement>");
868 DataSet ds = new DataSet();
869 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
870 ds.InferXmlSchema(myStream,null);
871 Assert.AreEqual("DocumentElement", ds.DataSetName, "DS111");
872 Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS112");
873 Assert.AreEqual("ChildElement1", ds.Tables[1].TableName, "DS113");
874 Assert.AreEqual(2, ds.Tables.Count, "DS114");
876 Assert.AreEqual("Element1_Id", ds.Tables["Element1"].Columns["Element1_Id"].ColumnName, "DS115");
877 Assert.AreEqual(MappingType.Hidden, ds.Tables["Element1"].Columns["Element1_Id"].ColumnMapping , "DS116");
878 Assert.AreEqual(typeof(Int32), ds.Tables["Element1"].Columns["Element1_Id"].DataType , "DS117");
880 Assert.AreEqual("ChildElement2", ds.Tables["Element1"].Columns["ChildElement2"].ColumnName, "DS118");
881 Assert.AreEqual(MappingType.Element, ds.Tables["Element1"].Columns["ChildElement2"].ColumnMapping , "DS119");
882 Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["ChildElement2"].DataType , "DS120");
884 Assert.AreEqual("attr1", ds.Tables["ChildElement1"].Columns["attr1"].ColumnName, "DS121");
885 Assert.AreEqual(MappingType.Attribute, ds.Tables["ChildElement1"].Columns["attr1"].ColumnMapping , "DS122");
886 Assert.AreEqual(typeof(string), ds.Tables["ChildElement1"].Columns["attr1"].DataType , "DS123");
888 Assert.AreEqual("attr2", ds.Tables["ChildElement1"].Columns["attr2"].ColumnName, "DS124");
889 Assert.AreEqual(MappingType.Attribute, ds.Tables["ChildElement1"].Columns["attr2"].ColumnMapping , "DS125");
890 Assert.AreEqual(typeof(string), ds.Tables["ChildElement1"].Columns["attr2"].DataType , "DS126");
892 Assert.AreEqual("Element1_Id", ds.Tables["ChildElement1"].Columns["Element1_Id"].ColumnName, "DS127");
893 Assert.AreEqual(MappingType.Hidden, ds.Tables["ChildElement1"].Columns["Element1_Id"].ColumnMapping , "DS128");
894 Assert.AreEqual(typeof(Int32), ds.Tables["ChildElement1"].Columns["Element1_Id"].DataType , "DS129");
896 //Checking dataRelation :
897 Assert.AreEqual("Element1", ds.Relations["Element1_ChildElement1"].ParentTable.TableName, "DS130");
898 Assert.AreEqual("Element1_Id", ds.Relations["Element1_ChildElement1"].ParentColumns[0].ColumnName, "DS131");
899 Assert.AreEqual("ChildElement1", ds.Relations["Element1_ChildElement1"].ChildTable.TableName, "DS132");
900 Assert.AreEqual("Element1_Id", ds.Relations["Element1_ChildElement1"].ChildColumns[0].ColumnName, "DS133");
901 Assert.AreEqual(true, ds.Relations["Element1_ChildElement1"].Nested, "DS134");
903 //Checking ForeignKeyConstraint
905 ForeignKeyConstraint con = (ForeignKeyConstraint)ds.Tables["ChildElement1"].Constraints["Element1_ChildElement1"];
907 Assert.AreEqual("Element1_Id", con.Columns[0].ColumnName, "DS135");
908 Assert.AreEqual(Rule.Cascade, con.DeleteRule, "DS136");
909 Assert.AreEqual(AcceptRejectRule.None, con.AcceptRejectRule, "DS137");
910 Assert.AreEqual("Element1", con.RelatedTable.TableName, "DS138");
911 Assert.AreEqual("ChildElement1", con.Table.TableName, "DS139");
916 #region Inferring Element Text
918 [Test] public void InferXmlSchema_elementText1()
920 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringelementtext.htm
923 StringBuilder sb = new StringBuilder();
925 sb.Append("<DocumentElement>");
926 sb.Append("<Element1 attr1='value1'>Text1</Element1>");
927 sb.Append("</DocumentElement>");
928 DataSet ds = new DataSet();
929 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
930 ds.InferXmlSchema(myStream,null);
932 Assert.AreEqual("DocumentElement", ds.DataSetName, "DS140");
933 Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS141");
934 Assert.AreEqual(1, ds.Tables.Count, "DS142");
936 Assert.AreEqual("attr1", ds.Tables["Element1"].Columns["attr1"].ColumnName, "DS143");
937 Assert.AreEqual(MappingType.Attribute, ds.Tables["Element1"].Columns["attr1"].ColumnMapping , "DS144");
938 Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["attr1"].DataType , "DS145");
940 Assert.AreEqual("Element1_Text", ds.Tables["Element1"].Columns["Element1_Text"].ColumnName, "DS146");
941 Assert.AreEqual(MappingType.SimpleContent, ds.Tables["Element1"].Columns["Element1_Text"].ColumnMapping , "DS147");
942 Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["Element1_Text"].DataType , "DS148");
945 [Test] public void InferXmlSchema_elementText2()
947 //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringelementtext.htm
950 StringBuilder sb = new StringBuilder();
952 sb.Append("<DocumentElement>");
953 sb.Append("<Element1>");
955 sb.Append("<ChildElement1>Text2</ChildElement1>");
957 sb.Append("</Element1>");
958 sb.Append("</DocumentElement>");
959 DataSet ds = new DataSet();
960 MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
961 ds.InferXmlSchema(myStream,null);
963 Assert.AreEqual("DocumentElement", ds.DataSetName, "DS149");
964 Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS150");
965 Assert.AreEqual(1, ds.Tables.Count, "DS151");
967 Assert.AreEqual("ChildElement1", ds.Tables["Element1"].Columns["ChildElement1"].ColumnName, "DS152");
968 Assert.AreEqual(MappingType.Element, ds.Tables["Element1"].Columns["ChildElement1"].ColumnMapping , "DS153");
969 Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["ChildElement1"].DataType , "DS154");
970 Assert.AreEqual(1, ds.Tables["Element1"].Columns.Count, "DS155");
975 [Test] public void Locale()
977 DataSet ds = new DataSet("MyDataSet");
978 System.Globalization.CultureInfo culInfo = System.Globalization.CultureInfo.CurrentCulture ;
980 // Checking Locale default from system
981 Assert.AreEqual(culInfo, ds.Locale , "DS156");
983 // Checking Locale get/set
984 culInfo = new System.Globalization.CultureInfo("fr"); // = french
985 ds.Locale = culInfo ;
986 Assert.AreEqual(culInfo , ds.Locale , "DS157");
990 [SetCulture ("cs-CZ")]
991 public void DataSetSpecificCulture ()
993 var ds = new DataSet() ;
994 ds.Locale = CultureInfo.GetCultureInfo (1033);
995 var dt = ds.Tables.Add ("machine");
996 dt.Locale = ds.Locale;
997 Assert.AreSame (dt, ds.Tables["MACHINE"]);
1000 [Test] public void MergeFailed()
1002 EventRaised = false;
1004 ds1 = new DataSet();
1005 ds1.Tables.Add(DataProvider.CreateParentDataTable());
1006 //add primary key to the FIRST column
1007 ds1.Tables[0].PrimaryKey = new DataColumn[] {ds1.Tables[0].Columns[0]};
1009 //create target dataset which is a copy of the source
1013 //add primary key to the SECOND columnn
1014 ds2.Tables[0].PrimaryKey = new DataColumn[] {ds2.Tables[0].Columns[1]};
1015 //add a new row that already exists in the source dataset
1016 //ds2.Tables[0].Rows.Add(ds1.Tables[0].Rows[0].ItemArray);
1018 //enforce constraints
1019 ds2.EnforceConstraints = true;
1020 ds1.EnforceConstraints = true;
1022 // Add MergeFailed event handler for the table.
1023 ds2.MergeFailed += new MergeFailedEventHandler( Merge_Failed );
1025 ds2.Merge(ds1); //will raise MergeFailed event
1027 // MergeFailed event
1028 Assert.AreEqual(true , EventRaised , "DS158");
1030 private void Merge_Failed( object sender, MergeFailedEventArgs e )
1035 [Test] public void Merge_ByDataRowsNoPreserveIgnoreMissingSchema()
1037 DataTable dt = DataProvider.CreateParentDataTable();
1038 dt.TableName = "Table1";
1039 dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
1041 //create target dataset (copy of source dataset)
1042 DataSet dsTarget = new DataSet();
1043 dsTarget.Tables.Add(dt.Copy());
1045 DataRow[] drArr = new DataRow[3];
1047 string OldValue = dt.Select("ParentId=1")[0][1].ToString();
1048 drArr[0] = dt.Select("ParentId=1")[0];
1049 drArr[0][1] = "NewValue";
1051 drArr[1] = dt.Select("ParentId=2")[0];
1054 drArr[2] = dt.NewRow();
1055 drArr[2].ItemArray = new object[] {99 ,"NewRowValue1", "NewRowValue2"};
1056 dt.Rows.Add(drArr[2]);
1058 dsTarget.Merge(drArr,false,MissingSchemaAction.Ignore );
1061 Assert.AreEqual("NewValue", dsTarget.Tables["Table1"].Select("ParentId=1")[0][1] , "DS159");
1064 Assert.AreEqual(1, dsTarget.Tables["Table1"].Select("ParentId=99").Length , "DS160");
1066 // Merge deleted row
1067 Assert.AreEqual(0, dsTarget.Tables["Table1"].Select("ParentId=2").Length , "DS161");
1070 [Test] public void Merge_ByDataRowsPreserveMissingSchema()
1072 DataTable dt = DataProvider.CreateParentDataTable();
1073 dt.TableName = "Table1";
1074 dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
1076 //create target dataset (copy of source dataset)
1077 DataSet dsTarget = new DataSet();
1078 dsTarget.Tables.Add(dt.Copy());
1080 //add new column (for checking MissingSchemaAction)
1081 DataColumn dc = new DataColumn("NewColumn",typeof(float));
1084 DataRow[] drArr = new DataRow[3];
1087 string OldValue = dt.Select("ParentId=1")[0][1].ToString();
1088 drArr[0] = dt.Select("ParentId=1")[0];
1089 drArr[0][1] = "NewValue";
1091 drArr[1] = dt.Select("ParentId=2")[0];
1094 drArr[2] = dt.NewRow();
1095 drArr[2].ItemArray = new object[] {99 ,"NewRowValue1", "NewRowValue2",null};
1096 dt.Rows.Add(drArr[2]);
1098 DataSet dsTarget1 = null;
1100 #region "Merge(drArr,true,MissingSchemaAction.Ignore )"
1101 dsTarget1 = dsTarget.Copy();
1102 dsTarget1.Merge(drArr,true,MissingSchemaAction.Ignore );
1103 // Merge true,Ignore - Column
1104 Assert.AreEqual(false, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS162");
1106 // Merge true,Ignore - changed values
1107 Assert.AreEqual(OldValue, dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS163");
1109 // Merge true,Ignore - added values
1110 Assert.AreEqual(1 , dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS164");
1112 // Merge true,Ignore - deleted row
1113 Assert.AreEqual(true, dsTarget1.Tables["Table1"].Select("ParentId=2").Length > 0, "DS165");
1116 #region "Merge(drArr,false,MissingSchemaAction.Ignore )"
1117 dsTarget1 = dsTarget.Copy();
1118 dsTarget1.Merge(drArr,false,MissingSchemaAction.Ignore );
1119 // Merge true,Ignore - Column
1120 Assert.AreEqual(false, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS166");
1122 // Merge true,Ignore - changed values
1123 Assert.AreEqual("NewValue", dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS167");
1125 // Merge true,Ignore - added values
1126 Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS168");
1128 // Merge true,Ignore - deleted row
1129 Assert.AreEqual(0, dsTarget1.Tables["Table1"].Select("ParentId=2").Length , "DS169");
1132 #region "Merge(drArr,true,MissingSchemaAction.Add )"
1133 dsTarget1 = dsTarget.Copy();
1134 dsTarget1.Merge(drArr,true,MissingSchemaAction.Add );
1135 // Merge true,Ignore - Column
1136 Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS170");
1138 // Merge true,Ignore - changed values
1139 Assert.AreEqual(OldValue, dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS171");
1141 // Merge true,Ignore - added values
1142 Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS172");
1144 // Merge true,Ignore - deleted row
1145 Assert.AreEqual(true, dsTarget1.Tables["Table1"].Select("ParentId=2").Length >0, "DS173");
1148 #region "Merge(drArr,false,MissingSchemaAction.Add )"
1149 dsTarget1 = dsTarget.Copy();
1150 dsTarget1.Merge(drArr,false,MissingSchemaAction.Add );
1151 // Merge true,Ignore - Column
1152 Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS174");
1154 // Merge true,Ignore - changed values
1155 Assert.AreEqual("NewValue", dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS175");
1157 // Merge true,Ignore - added values
1158 Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS176");
1160 // Merge true,Ignore - deleted row
1161 Assert.AreEqual(0, dsTarget1.Tables["Table1"].Select("ParentId=2").Length , "DS177");
1164 #region "Merge(drArr,false/true,MissingSchemaAction.Error )"
1165 // dsTarget1 = dsTarget.Copy();
1166 // // Merge true,Error - Column
1168 // dsTarget1.Merge(drArr,true,MissingSchemaAction.Error );
1169 // Assert.Fail("DS178: Merge Failed to throw InvalidOperationException");
1171 // catch (InvalidOperationException) {}
1172 // catch (AssertionException exc) {throw exc;}
1173 // catch (Exception exc)
1175 // Assert.Fail("DS179: Merge. Wrong exception type. Got:" + exc);
1178 // // Merge false,Error - Column
1180 // dsTarget1.Merge(drArr,false,MissingSchemaAction.Error );
1181 // Assert.Fail("DS180: Merge Failed to throw InvalidOperationException");
1183 // catch (InvalidOperationException) {}
1184 // catch (AssertionException exc) {throw exc;}
1185 // catch (Exception exc)
1187 // Assert.Fail("DS181: Merge. Wrong exception type. Got:" + exc);
1192 [Test] public void Merge_ByDataSet()
1194 //create source dataset
1195 DataSet ds = new DataSet();
1196 DataTable dt = DataProvider.CreateParentDataTable();
1197 dt.TableName = "Table1";
1198 ds.Tables.Add(dt.Copy());
1199 dt.TableName = "Table2";
1201 dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
1202 ds.Tables.Add(dt.Copy());
1204 //create target dataset (copy of source dataset)
1205 DataSet dsTarget = ds.Copy();
1206 int iTable1RowsCount = dsTarget.Tables["Table1"].Rows.Count;
1208 //Source - add another table, don't exists on the target dataset
1209 ds.Tables.Add(new DataTable("SomeTable"));
1210 ds.Tables["SomeTable"].Columns.Add("Id");
1211 ds.Tables["SomeTable"].Rows.Add(new object[] {777});
1213 //Target - add another table, don't exists on the source dataset
1214 dsTarget.Tables.Add(new DataTable("SmallTable"));
1215 dsTarget.Tables["SmallTable"].Columns.Add("Id");
1216 dsTarget.Tables["SmallTable"].Rows.Add(new object[] {777});
1218 //update existing row
1219 ds.Tables["Table2"].Select("ParentId=1")[0][1] = "OldValue1";
1221 object[] arrAddedRow = new object[] {99,"NewValue1","NewValue2",new DateTime(0),0.5,true};
1222 ds.Tables["Table2"].Rows.Add(arrAddedRow);
1223 //delete existing rows
1224 foreach (DataRow dr in ds.Tables["Table2"].Select("ParentId=2"))
1229 // Merge - changed values
1231 Assert.AreEqual("OldValue1", dsTarget.Tables["Table2"].Select("ParentId=1")[0][1] , "DS182");
1233 // Merge - added values
1234 Assert.AreEqual(arrAddedRow, dsTarget.Tables["Table2"].Select("ParentId=99")[0].ItemArray , "DS183");
1236 // Merge - deleted row
1237 Assert.AreEqual(0, dsTarget.Tables["Table2"].Select("ParentId=2").Length , "DS184");
1239 //Table1 rows count should be double (no primary key)
1240 // Merge - Unchanged table 1
1241 Assert.AreEqual(iTable1RowsCount * 2, dsTarget.Tables["Table1"].Rows.Count , "DS185");
1243 //SmallTable rows count should be the same
1244 // Merge - Unchanged table 2
1245 Assert.AreEqual(1, dsTarget.Tables["SmallTable"].Rows.Count , "DS186");
1247 //SomeTable - new table
1248 // Merge - new table
1249 Assert.AreEqual(true, dsTarget.Tables["SomeTable"] != null, "DS187");
1252 [Test] public void Merge_ByDataSetPreserve()
1254 //create source dataset
1255 DataSet ds = new DataSet();
1256 DataTable dt = DataProvider.CreateParentDataTable();
1257 dt.TableName = "Table1";
1258 ds.Tables.Add(dt.Copy());
1259 dt.TableName = "Table2";
1261 dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
1262 ds.Tables.Add(dt.Copy());
1264 //create target dataset (copy of source dataset)
1265 DataSet dsTarget1 = ds.Copy();
1266 DataSet dsTarget2 = ds.Copy();
1267 int iTable1RowsCount = dsTarget1.Tables["Table1"].Rows.Count;
1269 //update existing row
1270 string oldValue = ds.Tables["Table2"].Select("ParentId=1")[0][1].ToString();
1271 ds.Tables["Table2"].Select("ParentId=1")[0][1] = "NewValue";
1273 object[] arrAddedRow = new object[] {99,"NewValue1","NewValue2",new DateTime(0),0.5,true};
1274 ds.Tables["Table2"].Rows.Add(arrAddedRow);
1275 //delete existing rows
1276 int iDeleteLength = dsTarget1.Tables["Table2"].Select("ParentId=2").Length;
1277 foreach (DataRow dr in ds.Tables["Table2"].Select("ParentId=2"))
1282 #region "Merge(ds,true)"
1283 //only new added rows are merged (preserveChanges = true)
1284 dsTarget1.Merge(ds,true);
1285 // Merge - changed values
1286 Assert.AreEqual(oldValue, dsTarget1.Tables["Table2"].Select("ParentId=1")[0][1] , "DS188");
1288 // Merge - added values
1289 Assert.AreEqual(arrAddedRow, dsTarget1.Tables["Table2"].Select("ParentId=99")[0].ItemArray , "DS189");
1291 // Merge - deleted row
1292 Assert.AreEqual(iDeleteLength, dsTarget1.Tables["Table2"].Select("ParentId=2").Length , "DS190");
1295 #region "Merge(ds,false)"
1296 //all changes are merged (preserveChanges = false)
1297 dsTarget2.Merge(ds,false);
1298 // Merge - changed values
1299 Assert.AreEqual("NewValue", dsTarget2.Tables["Table2"].Select("ParentId=1")[0][1] , "DS191");
1301 // Merge - added values
1302 Assert.AreEqual(arrAddedRow, dsTarget2.Tables["Table2"].Select("ParentId=99")[0].ItemArray , "DS192");
1304 // Merge - deleted row
1305 Assert.AreEqual(0, dsTarget2.Tables["Table2"].Select("ParentId=2").Length , "DS193");
1309 [Test] public void Merge_ByDataSetPreserveMissingSchemaAction()
1311 //create source dataset
1312 DataSet ds = new DataSet();
1314 DataTable dt = DataProvider.CreateParentDataTable();
1315 dt.TableName = "Table1";
1317 dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
1319 //add table to dataset
1320 ds.Tables.Add(dt.Copy());
1324 //create target dataset (copy of source dataset)
1325 DataSet dsTarget = ds.Copy();
1327 //add new column (for checking MissingSchemaAction)
1328 DataColumn dc = new DataColumn("NewColumn",typeof(float));
1329 //make the column to be primary key
1332 //add new table (for checking MissingSchemaAction)
1333 ds.Tables.Add(new DataTable("NewTable"));
1334 ds.Tables["NewTable"].Columns.Add("NewColumn1",typeof(int));
1335 ds.Tables["NewTable"].Columns.Add("NewColumn2",typeof(long));
1336 ds.Tables["NewTable"].Rows.Add(new object[] {1,2});
1337 ds.Tables["NewTable"].Rows.Add(new object[] {3,4});
1338 ds.Tables["NewTable"].PrimaryKey = new DataColumn[] {ds.Tables["NewTable"].Columns["NewColumn1"]};
1340 #region "ds,false,MissingSchemaAction.Add)"
1341 DataSet dsTarget1 = dsTarget.Copy();
1342 dsTarget1.Merge(ds,false,MissingSchemaAction.Add);
1343 // Merge MissingSchemaAction.Add - Column
1344 Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS194");
1346 // Merge MissingSchemaAction.Add - Table
1347 Assert.AreEqual(true, dsTarget1.Tables.Contains("NewTable"), "DS195");
1349 //failed, should be success by MSDN Library documentation
1350 // // Merge MissingSchemaAction.Add - PrimaryKey
1351 // Assert.AreEqual(0, dsTarget1.Tables["NewTable"].PrimaryKey.Length, "DS196");
1354 #region "ds,false,MissingSchemaAction.AddWithKey)"
1355 //MissingSchemaAction.Add,MissingSchemaAction.AddWithKey - behave the same, checked only Add
1357 // DataSet dsTarget2 = dsTarget.Copy();
1358 // dsTarget2.Merge(ds,false,MissingSchemaAction.AddWithKey);
1359 // // Merge MissingSchemaAction.AddWithKey - Column
1360 // Assert.AreEqual(true, dsTarget2.Tables["Table1"].Columns.Contains("NewColumn"), "DS197");
1362 // // Merge MissingSchemaAction.AddWithKey - Table
1363 // Assert.AreEqual(true, dsTarget2.Tables.Contains("NewTable"), "DS198");
1365 // // Merge MissingSchemaAction.AddWithKey - PrimaryKey
1366 // Assert.AreEqual(dsTarget2.Tables["NewTable"].Columns["NewColumn1"], dsTarget2.Tables["NewTable"].PrimaryKey[0], "DS199");
1369 #region "ds,false,MissingSchemaAction.Error)"
1370 //Error - throw System.Data.DataException, should throw InvalidOperationException
1371 // DataSet dsTarget3 ;
1372 // // Merge MissingSchemaAction.Error
1373 // dsTarget3 = dsTarget.Copy();
1375 // dsTarget3.Merge(ds,false,MissingSchemaAction.Error);
1376 // Assert.Fail("DS200: Merge Failed to throw InvalidOperationException");
1378 // catch (InvalidOperationException) {}
1379 // catch (AssertionException exc) {throw exc;}
1380 // catch (Exception exc)
1382 // Assert.Fail("DS201: Merge. Wrong exception type. Got:" + exc);
1386 #region "ds,false,MissingSchemaAction.Ignore )"
1387 DataSet dsTarget4 = dsTarget.Copy();
1388 dsTarget4.Merge(ds,false,MissingSchemaAction.Ignore );
1389 // Merge MissingSchemaAction.Ignore - Column
1390 Assert.AreEqual(false, dsTarget4.Tables["Table1"].Columns.Contains("NewColumn"), "DS202");
1392 // Merge MissingSchemaAction.Ignore - Table
1393 Assert.AreEqual(false, dsTarget4.Tables.Contains("NewTable"), "DS203");
1397 [Test] public void Merge_ByComplexDataSet()
1399 //create source dataset
1400 DataSet ds = new DataSet();
1402 ds.Tables.Add(DataProvider.CreateParentDataTable());
1403 ds.Tables.Add(DataProvider.CreateChildDataTable());
1404 ds.Tables["Child"].TableName = "Child2";
1405 ds.Tables.Add(DataProvider.CreateChildDataTable());
1407 // Console.WriteLine(ds.Tables[0].TableName + ds.Tables[1].TableName + ds.Tables[2].TableName);
1408 // Console.WriteLine(ds.Tables[2].Rows.Count.ToString());
1410 //craete a target dataset to the merge operation
1411 DataSet dsTarget = ds.Copy();
1413 //craete a second target dataset to the merge operation
1414 DataSet dsTarget1 = ds.Copy();
1416 //------------------ make some changes in the second target dataset schema --------------------
1418 dsTarget1.Tables["Parent"].PrimaryKey = new DataColumn[] {dsTarget1.Tables["Parent"].Columns["ParentId"]};
1419 dsTarget1.Tables["Child"].PrimaryKey = new DataColumn[] {dsTarget1.Tables["Child"].Columns["ParentId"],dsTarget1.Tables["Child"].Columns["ChildId"]};
1421 //add Foreign Key (different name)
1422 dsTarget1.Tables["Child2"].Constraints.Add("Child2_FK_2",dsTarget1.Tables["Parent"].Columns["ParentId"],dsTarget1.Tables["Child2"].Columns["ParentId"]);
1424 //add relation (different name)
1425 //dsTarget1.Relations.Add("Parent_Child_1",dsTarget1.Tables["Parent"].Columns["ParentId"],dsTarget1.Tables["Child"].Columns["ParentId"]);
1427 //------------------ make some changes in the source dataset schema --------------------
1429 ds.Tables["Parent"].PrimaryKey = new DataColumn[] {ds.Tables["Parent"].Columns["ParentId"]};
1430 ds.Tables["Child"].PrimaryKey = new DataColumn[] {ds.Tables["Child"].Columns["ParentId"],ds.Tables["Child"].Columns["ChildId"]};
1433 ds.Tables["Parent"].Columns["String2"].Unique = true; //will not be merged
1436 ds.Tables["Child2"].Constraints.Add("Child2_FK",ds.Tables["Parent"].Columns["ParentId"],ds.Tables["Child2"].Columns["ParentId"]);
1439 ds.Relations.Add("Parent_Child",ds.Tables["Parent"].Columns["ParentId"],ds.Tables["Child"].Columns["ParentId"]);
1441 //add allow null constraint
1442 ds.Tables["Parent"].Columns["ParentBool"].AllowDBNull = false; //will not be merged
1444 //add Indentity column
1445 ds.Tables["Parent"].Columns.Add("Indentity",typeof(int));
1446 ds.Tables["Parent"].Columns["Indentity"].AutoIncrement = true;
1447 ds.Tables["Parent"].Columns["Indentity"].AutoIncrementStep = 2;
1449 //modify default value
1450 ds.Tables["Child"].Columns["String1"].DefaultValue = "Default"; //will not be merged
1453 ds.Tables["Child"].Columns.Remove("String2"); //will not be merged
1455 //-------------------- begin to check ----------------------------------------------
1456 // merge 1 - make sure the merge method invoked without exceptions
1458 Assert.AreEqual("Success", "Success", "DS204");
1460 CompareResults_1("merge 1",ds,dsTarget);
1463 // merge 2 - make sure the merge method invoked without exceptions
1465 Assert.AreEqual("Success", "Success", "DS205");
1467 CompareResults_1("merge 2",ds,dsTarget);
1469 // merge second dataset - make sure the merge method invoked without exceptions
1470 dsTarget1.Merge(ds);
1471 Assert.AreEqual("Success", "Success", "DS206");
1473 CompareResults_2("merge 3",ds,dsTarget1);
1477 public void Merge_RelationWithoutConstraints ()
1479 DataSet ds = new DataSet ();
1481 DataTable table1 = ds.Tables.Add ("table1");
1482 DataTable table2 = ds.Tables.Add ("table2");
1484 DataColumn pcol = table1.Columns.Add ("col1", typeof (int));
1485 DataColumn ccol = table2.Columns.Add ("col1", typeof (int));
1487 DataSet ds1 = ds.Copy ();
1488 DataRelation rel = ds1.Relations.Add ("rel1", ds1.Tables[0].Columns[0],
1489 ds1.Tables [1].Columns [0], false);
1492 Assert.AreEqual (1, ds.Relations.Count , "#1");
1493 Assert.AreEqual (0, ds.Tables [0].Constraints.Count , "#2");
1494 Assert.AreEqual (0, ds.Tables [1].Constraints.Count , "#3");
1498 public void Merge_DuplicateConstraints ()
1500 DataSet ds = new DataSet ();
1502 DataTable table1 = ds.Tables.Add ("table1");
1503 DataTable table2 = ds.Tables.Add ("table2");
1505 DataColumn pcol = table1.Columns.Add ("col1", typeof (int));
1506 DataColumn ccol = table2.Columns.Add ("col1", typeof (int));
1508 DataSet ds1 = ds.Copy ();
1510 DataRelation rel = ds.Relations.Add ("rel1", pcol, ccol);
1512 ds1.Tables [1].Constraints.Add ("fk", ds1.Tables [0].Columns [0], ds1.Tables [1].Columns [0]);
1514 // No Exceptions shud be thrown
1516 Assert.AreEqual (1, table2.Constraints.Count, "#1 Constraints shudnt be duplicated");
1520 public void Merge_DuplicateConstraints_1 ()
1522 DataSet ds = new DataSet ();
1524 DataTable table1 = ds.Tables.Add ("table1");
1525 DataTable table2 = ds.Tables.Add ("table2");
1527 DataColumn pcol = table1.Columns.Add ("col1", typeof (int));
1528 DataColumn ccol = table2.Columns.Add ("col1", typeof (int));
1529 DataColumn pcol1 = table1.Columns.Add ("col2", typeof (int));
1530 DataColumn ccol1 = table2.Columns.Add ("col2", typeof (int));
1532 DataSet ds1 = ds.Copy ();
1534 table2.Constraints.Add ("fk", pcol, ccol);
1535 ds1.Tables [1].Constraints.Add ("fk", ds1.Tables [0].Columns ["col2"], ds1.Tables [1].Columns ["col2"]);
1537 // No Exceptions shud be thrown
1539 Assert.AreEqual (2, table2.Constraints.Count, "#1 fk constraint shud be merged");
1540 Assert.AreEqual ("Constraint1", table2.Constraints [1].ConstraintName, "#2 constraint name shud be changed");
1544 public void CopyClone_RelationWithoutConstraints ()
1546 DataSet ds = new DataSet ();
1548 DataTable table1 = ds.Tables.Add ("table1");
1549 DataTable table2 = ds.Tables.Add ("table2");
1551 DataColumn pcol = table1.Columns.Add ("col1", typeof (int));
1552 DataColumn ccol = table2.Columns.Add ("col1", typeof (int));
1554 DataRelation rel = ds.Relations.Add ("rel1", pcol, ccol, false);
1556 DataSet ds1 = ds.Copy ();
1557 DataSet ds2 = ds.Clone ();
1559 Assert.AreEqual (1, ds1.Relations.Count, "#1");
1560 Assert.AreEqual (1, ds2.Relations.Count, "#2");
1562 Assert.AreEqual (0, ds1.Tables [0].Constraints.Count, "#3");
1563 Assert.AreEqual (0, ds1.Tables [1].Constraints.Count, "#4");
1565 Assert.AreEqual (0, ds2.Tables [0].Constraints.Count, "#5");
1566 Assert.AreEqual (0, ds2.Tables [1].Constraints.Count, "#6");
1570 public void Merge_ConstraintsFromReadXmlSchema ()
1572 DataSet ds = new DataSet ();
1573 ds.ReadXml ("Test/System.Data/TestMerge1.xml");
1574 DataSet ds2 = new DataSet ();
1575 ds2.Merge (ds, true, MissingSchemaAction.AddWithKey);
1576 DataRelation c = ds2.Tables [0].ChildRelations [0];
1577 Assert.IsNotNull (c.ParentKeyConstraint, "#1");
1578 Assert.IsNotNull (c.ChildKeyConstraint, "#2");
1582 [ExpectedException (typeof (DataException))]
1583 public void Merge_MissingEventHandler ()
1585 DataSet ds = new DataSet ();
1586 DataTable table1 = ds.Tables.Add ("table1");
1588 DataColumn pcol = table1.Columns.Add ("col1", typeof (int));
1589 DataColumn pcol1 = table1.Columns.Add ("col2", typeof (int));
1591 DataSet ds1 = ds.Copy ();
1592 table1.PrimaryKey = new DataColumn[] {pcol};
1593 ds1.Tables [0].PrimaryKey = new DataColumn[] {ds1.Tables [0].Columns [1]};
1595 // Exception shud be raised when handler is not set for MergeFailed Event
1600 [ExpectedException (typeof(DataException))]
1601 public void Merge_MissingColumn ()
1603 DataSet ds = new DataSet ();
1604 DataTable table1 = ds.Tables.Add ("table1");
1605 DataTable table2 = ds.Tables.Add ("table2");
1607 table1.Columns.Add ("col1", typeof (int));
1608 table2.Columns.Add ("col1", typeof (int));
1610 DataSet ds1 = ds.Copy ();
1612 ds1.Tables [0].Columns.Add ("col2");
1614 ds.Merge (ds1, true, MissingSchemaAction.Error);
1618 public void Merge_MissingConstraint ()
1620 DataSet ds = new DataSet ();
1621 DataTable table1 = ds.Tables.Add ("table1");
1622 DataTable table2 = ds.Tables.Add ("table2");
1623 table1.Columns.Add ("col1", typeof (int));
1624 table2.Columns.Add ("col1", typeof (int));
1627 DataSet ds1 = ds.Copy ();
1628 DataSet ds2 = ds.Copy ();
1629 ds2.Tables [0].Constraints.Add ("uc", ds2.Tables [0].Columns [0], false);
1630 ds1.Merge (ds2, true, MissingSchemaAction.Error);
1631 Assert.Fail ("#1 If uniqueconstraint is missing, exception shud be thrown");
1632 }catch (DataException e) {
1636 DataSet ds1 = ds.Copy ();
1637 DataSet ds2 = ds.Copy ();
1638 ds2.Tables [0].Constraints.Add ("fk", ds2.Tables [0].Columns [0], ds2.Tables[1].Columns [0]);
1639 ds1.Tables [0].Constraints.Add ("uc", ds1.Tables [0].Columns [0],false);
1640 ds1.Merge (ds2, true, MissingSchemaAction.Error);
1641 Assert.Fail ("#2 If foreignkeyconstraint is missing, exception shud be thrown");
1642 }catch (DataException e) {
1646 DataSet ds1 = ds.Copy ();
1647 DataSet ds2 = ds.Copy ();
1648 ds2.Relations.Add ("rel", ds2.Tables [0].Columns [0], ds2.Tables[1].Columns [0], false);
1649 ds1.Merge (ds2, true, MissingSchemaAction.Error);
1650 Assert.Fail ("#2 If datarelation is missing, exception shud be thrown");
1651 }catch (ArgumentException e) {
1656 [ExpectedException (typeof (DataException))]
1657 public void Merge_PrimaryKeys_IncorrectOrder ()
1659 DataSet ds = new DataSet ();
1660 DataTable table1 = ds.Tables.Add ("table1");
1661 DataTable table2 = ds.Tables.Add ("table2");
1662 DataColumn pcol = table1.Columns.Add ("col1", typeof (int));
1663 DataColumn pcol1 = table1.Columns.Add ("col2", typeof (int));
1664 DataColumn ccol = table2.Columns.Add ("col1", typeof (int));
1666 DataSet ds1 = ds.Copy ();
1667 table1.PrimaryKey = new DataColumn[] {pcol,pcol1};
1668 ds1.Tables [0].PrimaryKey = new DataColumn [] {ds1.Tables[0].Columns [1], ds1.Tables [0].Columns [0]};
1670 // Though the key columns are the same, if the order is incorrect
1671 // Exception must be raised
1675 void CompareResults_1(string Msg,DataSet ds, DataSet dsTarget)
1677 // check Parent Primary key length
1678 Assert.AreEqual(dsTarget.Tables["Parent"].PrimaryKey.Length , ds.Tables["Parent"].PrimaryKey.Length , "DS207");
1680 // check Child Primary key length
1681 Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey.Length , ds.Tables["Child"].PrimaryKey.Length , "DS208");
1683 // check Parent Primary key columns
1684 Assert.AreEqual(dsTarget.Tables["Parent"].PrimaryKey[0].ColumnName, ds.Tables["Parent"].PrimaryKey[0].ColumnName , "DS209");
1686 // check Child Primary key columns[0]
1687 Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey[0].ColumnName, ds.Tables["Child"].PrimaryKey[0].ColumnName , "DS210");
1689 // check Child Primary key columns[1]
1690 Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey[1].ColumnName, ds.Tables["Child"].PrimaryKey[1].ColumnName , "DS211");
1692 // check Parent Unique columns
1693 Assert.AreEqual(dsTarget.Tables["Parent"].Columns["String2"].Unique, ds.Tables["Parent"].Columns["String2"].Unique , "DS212");
1695 // check Child2 Foreign Key name
1696 Assert.AreEqual(dsTarget.Tables["Child2"].Constraints[0].ConstraintName , ds.Tables["Child2"].Constraints[0].ConstraintName , "DS213");
1698 // check dataset relation count
1699 Assert.AreEqual(dsTarget.Relations.Count , ds.Relations.Count , "DS214");
1701 // check dataset relation - Parent column
1702 Assert.AreEqual(dsTarget.Relations[0].ParentColumns[0].ColumnName , ds.Relations[0].ParentColumns[0].ColumnName , "DS215");
1704 // check dataset relation - Child column
1705 Assert.AreEqual(dsTarget.Relations[0].ChildColumns[0].ColumnName , ds.Relations[0].ChildColumns[0].ColumnName , "DS216");
1707 // check allow null constraint
1708 Assert.AreEqual(true, dsTarget.Tables["Parent"].Columns["ParentBool"].AllowDBNull, "DS217");
1710 // check Indentity column
1711 Assert.AreEqual(dsTarget.Tables["Parent"].Columns.Contains("Indentity"), ds.Tables["Parent"].Columns.Contains("Indentity"), "DS218");
1713 // check Indentity column - AutoIncrementStep
1714 Assert.AreEqual(dsTarget.Tables["Parent"].Columns["Indentity"].AutoIncrementStep, ds.Tables["Parent"].Columns["Indentity"].AutoIncrementStep, "DS219");
1716 // check Indentity column - AutoIncrement
1717 Assert.AreEqual(dsTarget.Tables["Parent"].Columns["Indentity"].AutoIncrement, ds.Tables["Parent"].Columns["Indentity"].AutoIncrement, "DS220");
1719 // check Indentity column - DefaultValue
1720 Assert.AreEqual(true, dsTarget.Tables["Child"].Columns["String1"].DefaultValue == DBNull.Value , "DS221");
1722 // check remove colum
1723 Assert.AreEqual(true, dsTarget.Tables["Child"].Columns.Contains("String2"), "DS222");
1726 void CompareResults_2(string Msg,DataSet ds, DataSet dsTarget)
1728 // check Parent Primary key length
1729 Assert.AreEqual(dsTarget.Tables["Parent"].PrimaryKey.Length , ds.Tables["Parent"].PrimaryKey.Length , "DS223");
1731 // check Child Primary key length
1732 Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey.Length , ds.Tables["Child"].PrimaryKey.Length , "DS224");
1734 // check Parent Primary key columns
1735 Assert.AreEqual(dsTarget.Tables["Parent"].PrimaryKey[0].ColumnName, ds.Tables["Parent"].PrimaryKey[0].ColumnName , "DS225");
1737 // check Child Primary key columns[0]
1738 Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey[0].ColumnName, ds.Tables["Child"].PrimaryKey[0].ColumnName , "DS226");
1740 // check Child Primary key columns[1]
1741 Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey[1].ColumnName, ds.Tables["Child"].PrimaryKey[1].ColumnName , "DS227");
1743 // check Parent Unique columns
1744 Assert.AreEqual(dsTarget.Tables["Parent"].Columns["String2"].Unique, ds.Tables["Parent"].Columns["String2"].Unique , "DS228");
1746 // check Child2 Foreign Key name
1747 Assert.AreEqual("Child2_FK_2" , dsTarget.Tables["Child2"].Constraints[0].ConstraintName, "DS229");
1749 // check dataset relation count
1750 Assert.AreEqual(dsTarget.Relations.Count , ds.Relations.Count , "DS230");
1752 // check dataset relation - Parent column
1753 Assert.AreEqual(dsTarget.Relations[0].ParentColumns[0].ColumnName , ds.Relations[0].ParentColumns[0].ColumnName , "DS231");
1755 // check dataset relation - Child column
1756 Assert.AreEqual(dsTarget.Relations[0].ChildColumns[0].ColumnName , ds.Relations[0].ChildColumns[0].ColumnName , "DS232");
1758 // check allow null constraint
1759 Assert.AreEqual(true, dsTarget.Tables["Parent"].Columns["ParentBool"].AllowDBNull, "DS233");
1761 // check Indentity column
1762 Assert.AreEqual(dsTarget.Tables["Parent"].Columns.Contains("Indentity"), ds.Tables["Parent"].Columns.Contains("Indentity"), "DS234");
1764 // check Indentity column - AutoIncrementStep
1765 Assert.AreEqual(dsTarget.Tables["Parent"].Columns["Indentity"].AutoIncrementStep, ds.Tables["Parent"].Columns["Indentity"].AutoIncrementStep, "DS235");
1767 // check Indentity column - AutoIncrement
1768 Assert.AreEqual(dsTarget.Tables["Parent"].Columns["Indentity"].AutoIncrement, ds.Tables["Parent"].Columns["Indentity"].AutoIncrement, "DS236");
1770 // check Indentity column - DefaultValue
1771 Assert.AreEqual(true, dsTarget.Tables["Child"].Columns["String1"].DefaultValue == DBNull.Value , "DS237");
1773 // check remove colum
1774 Assert.AreEqual(true, dsTarget.Tables["Child"].Columns.Contains("String2"), "DS238");
1775 //TestCase for bug #3168
1776 // Check Relation.Nested value, TestCase for bug #3168
1777 DataSet orig = new DataSet();
1779 DataTable parent = orig.Tables.Add("Parent");
1780 parent.Columns.Add("Id", typeof(int));
1781 parent.Columns.Add("col1", typeof(string));
1782 parent.Rows.Add(new object[] {0, "aaa"});
1784 DataTable child = orig.Tables.Add("Child");
1785 child.Columns.Add("ParentId", typeof(int));
1786 child.Columns.Add("col1", typeof(string));
1787 child.Rows.Add(new object[] {0, "bbb"});
1789 orig.Relations.Add("Parent_Child", parent.Columns["Id"], child.Columns["ParentId"]);
1790 orig.Relations["Parent_Child"].Nested = true;
1792 DataSet merged = new DataSet();
1794 Assert.AreEqual(orig.Relations["Parent_Child"].Nested, merged.Relations["Parent_Child"].Nested, "DS239");
1797 [Test] public void Merge_ByDataTable()
1799 //create source dataset
1800 DataSet ds = new DataSet();
1802 DataTable dt = DataProvider.CreateParentDataTable();
1803 dt.TableName = "Table1";
1804 //add a copy of the datatable to the dataset
1805 ds.Tables.Add(dt.Copy());
1807 dt.TableName = "Table2";
1809 dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
1810 ds.Tables.Add(dt.Copy());
1811 //now the dataset hase two tables
1813 //create target dataset (copy of source dataset)
1814 DataSet dsTarget = ds.Copy();
1816 dt = ds.Tables["Table2"];
1817 //update existing row
1818 dt.Select("ParentId=1")[0][1] = "OldValue1";
1820 object[] arrAddedRow = new object[] {99,"NewValue1","NewValue2",new DateTime(0),0.5,true};
1821 dt.Rows.Add(arrAddedRow);
1822 //delete existing rows
1823 foreach (DataRow dr in dt.Select("ParentId=2"))
1828 // Merge - changed values
1830 Assert.AreEqual("OldValue1", dsTarget.Tables["Table2"].Select("ParentId=1")[0][1] , "DS240");
1832 // Merge - added values
1833 Assert.AreEqual(arrAddedRow, dsTarget.Tables["Table2"].Select("ParentId=99")[0].ItemArray , "DS241");
1835 // Merge - deleted row
1836 Assert.AreEqual(0, dsTarget.Tables["Table2"].Select("ParentId=2").Length , "DS242");
1838 //test case added due to a reported bug from infogate
1839 //when merging a DataTable with TableName=null, GH throw null reference exception.
1841 dt = new DataTable();
1842 dt.Columns.Add("Col1");
1843 dt.Rows.Add(new object[] {1});
1845 // Merge - add a table with no name
1847 Assert.AreEqual(1, ds.Tables.Count, "DS243");
1849 // Merge - add a table with no name - check Rows.Count
1850 Assert.AreEqual(dt.Rows.Count , ds.Tables[0].Rows.Count , "DS244");
1853 [Test] public void Merge_ByDataTablePreserveMissingSchemaAction()
1855 DataTable dt = DataProvider.CreateParentDataTable();
1856 dt.TableName = "Table1";
1857 dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
1859 //create target dataset (copy of source dataset)
1860 DataSet dsTarget = new DataSet();
1861 dsTarget.Tables.Add(dt.Copy());
1863 //add new column (for checking MissingSchemaAction)
1864 DataColumn dc = new DataColumn("NewColumn",typeof(float));
1868 string OldValue = dt.Select("ParentId=1")[0][1].ToString();
1869 dt.Select("ParentId=1")[0][1] = "NewValue";
1871 dt.Select("ParentId=2")[0].Delete();
1873 object[] arrAddedRow = new object[] {99,"NewRowValue1","NewRowValue2",new DateTime(0),0.5,true};
1874 dt.Rows.Add(arrAddedRow);
1876 #region "Merge(dt,true,MissingSchemaAction.Ignore )"
1877 DataSet dsTarget1 = dsTarget.Copy();
1878 dsTarget1.Merge(dt,true,MissingSchemaAction.Ignore );
1879 // Merge true,Ignore - Column
1880 Assert.AreEqual(false, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS245");
1882 // Merge true,Ignore - changed values
1883 Assert.AreEqual(OldValue, dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS246");
1885 // Merge true,Ignore - added values
1886 Assert.AreEqual(arrAddedRow, dsTarget1.Tables["Table1"].Select("ParentId=99")[0].ItemArray , "DS247");
1888 // Merge true,Ignore - deleted row
1889 Assert.AreEqual(true, dsTarget1.Tables["Table1"].Select("ParentId=2").Length > 0, "DS248");
1892 #region "Merge(dt,false,MissingSchemaAction.Ignore )"
1894 dsTarget1 = dsTarget.Copy();
1895 dsTarget1.Merge(dt,false,MissingSchemaAction.Ignore );
1896 // Merge true,Ignore - Column
1897 Assert.AreEqual(false, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS249");
1899 // Merge true,Ignore - changed values
1900 Assert.AreEqual("NewValue", dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS250");
1902 // Merge true,Ignore - added values
1903 Assert.AreEqual(arrAddedRow, dsTarget1.Tables["Table1"].Select("ParentId=99")[0].ItemArray , "DS251");
1905 // Merge true,Ignore - deleted row
1906 Assert.AreEqual(0, dsTarget1.Tables["Table1"].Select("ParentId=2").Length , "DS252");
1909 #region "Merge(dt,true,MissingSchemaAction.Add )"
1910 dsTarget1 = dsTarget.Copy();
1911 dsTarget1.Merge(dt,true,MissingSchemaAction.Add );
1912 // Merge true,Add - Column
1913 Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS253");
1915 // Merge true,Add - changed values
1916 Assert.AreEqual(OldValue, dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS254");
1918 // Merge true,Add - added values
1919 Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS255");
1921 // Merge true,Add - deleted row
1922 Assert.AreEqual(true, dsTarget1.Tables["Table1"].Select("ParentId=2").Length > 0, "DS256");
1925 #region "Merge(dt,false,MissingSchemaAction.Add )"
1926 dsTarget1 = dsTarget.Copy();
1927 dsTarget1.Merge(dt,false,MissingSchemaAction.Add );
1928 // Merge true,Add - Column
1929 Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS257");
1931 // Merge true,Add - changed values
1932 Assert.AreEqual("NewValue", dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS258");
1934 // Merge true,Add - added values
1935 Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS259");
1937 // Merge true,Add - deleted row
1938 Assert.AreEqual(0, dsTarget1.Tables["Table1"].Select("ParentId=2").Length , "DS260");
1941 #region "Merge(dt,false/true,MissingSchemaAction.Error )"
1942 // dsTarget1 = dsTarget.Copy();
1943 // // Merge true,Error - Column
1945 // dsTarget1.Merge(dt,true,MissingSchemaAction.Error );
1946 // Assert.Fail("DS261: Merge Failed to throw InvalidOperationException");
1948 // catch (InvalidOperationException) {}
1949 // catch (AssertionException exc) {throw exc;}
1950 // catch (Exception exc)
1952 // Assert.Fail("DS262: Merge. Wrong exception type. Got:" + exc);
1955 // // Merge false,Error - Column
1957 // dsTarget1.Merge(dt,false,MissingSchemaAction.Error );
1958 // Assert.Fail("DS263: Merge Failed to throw InvalidOperationException");
1960 // catch (InvalidOperationException) {}
1961 // catch (AssertionException exc) {throw exc;}
1962 // catch (Exception exc)
1964 // Assert.Fail("DS264: Merge. Wrong exception type. Got:" + exc);
1969 [Test] public void Namespace()
1971 DataSet ds = new DataSet();
1973 // Checking Namespace default
1974 Assert.AreEqual(String.Empty, ds.Namespace, "DS265");
1976 // Checking Namespace set/get
1977 String s = "MyNamespace";
1979 Assert.AreEqual(s, ds.Namespace, "DS266");
1982 [Test] public void Prefix()
1984 DataSet ds = new DataSet();
1986 // Checking Prefix default
1987 Assert.AreEqual(String.Empty, ds.Prefix , "DS267");
1989 // Checking Prefix set/get
1990 String s = "MyPrefix";
1992 Assert.AreEqual(s, ds.Prefix, "DS268");
1995 [Test] public void ReadXmlSchema_ByStream()
1997 DataSet ds1 = new DataSet();
1998 ds1.Tables.Add(DataProvider.CreateParentDataTable());
1999 ds1.Tables.Add(DataProvider.CreateChildDataTable());
2001 System.IO.MemoryStream ms = new System.IO.MemoryStream();
2002 //write xml schema only
2003 ds1.WriteXmlSchema(ms);
2005 System.IO.MemoryStream ms1 = new System.IO.MemoryStream(ms.GetBuffer());
2007 DataSet ds2 = new DataSet();
2008 ds2.ReadXmlSchema(ms1);
2011 // ReadXmlSchema - Tables count
2012 Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS269");
2014 // ReadXmlSchema - Tables 0 Col count
2015 Assert.AreEqual(ds1.Tables[0].Columns.Count , ds2.Tables[0].Columns.Count , "DS270");
2017 // ReadXmlSchema - Tables 1 Col count
2018 Assert.AreEqual(ds1.Tables[1].Columns.Count , ds2.Tables[1].Columns.Count , "DS271");
2020 //check some colummns types
2021 // ReadXmlSchema - Tables 0 Col type
2022 Assert.AreEqual(ds1.Tables[0].Columns[0].GetType() , ds2.Tables[0].Columns[0].GetType() , "DS272");
2024 // ReadXmlSchema - Tables 1 Col type
2025 Assert.AreEqual(ds1.Tables[1].Columns[3].GetType() , ds2.Tables[1].Columns[3].GetType() , "DS273");
2027 //check that no data exists
2028 // ReadXmlSchema - Table 1 row count
2029 Assert.AreEqual(0, ds2.Tables[0].Rows.Count , "DS274");
2031 // ReadXmlSchema - Table 2 row count
2032 Assert.AreEqual(0, ds2.Tables[1].Rows.Count , "DS275");
2035 [Test] public void ReadXmlSchema_ByFileName()
2037 string sTempFileName = Path.Combine (Path.GetTempPath (), "tmpDataSet_ReadWriteXml_43899.xml");
2039 DataSet ds1 = new DataSet();
2040 ds1.Tables.Add(DataProvider.CreateParentDataTable());
2041 ds1.Tables.Add(DataProvider.CreateChildDataTable());
2043 //write xml file, schema only
2044 ds1.WriteXmlSchema(sTempFileName);
2046 //copy both data and schema
2047 DataSet ds2 = new DataSet();
2049 ds2.ReadXmlSchema(sTempFileName);
2052 // ReadXmlSchema - Tables count
2053 Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS276");
2055 // ReadXmlSchema - Tables 0 Col count
2056 Assert.AreEqual(ds1.Tables[0].Columns.Count , ds2.Tables[0].Columns.Count , "DS277");
2058 // ReadXmlSchema - Tables 1 Col count
2059 Assert.AreEqual(ds1.Tables[1].Columns.Count , ds2.Tables[1].Columns.Count , "DS278");
2061 //check some colummns types
2062 // ReadXmlSchema - Tables 0 Col type
2063 Assert.AreEqual(ds1.Tables[0].Columns[0].GetType() , ds2.Tables[0].Columns[0].GetType() , "DS279");
2065 // ReadXmlSchema - Tables 1 Col type
2066 Assert.AreEqual(ds1.Tables[1].Columns[3].GetType() , ds2.Tables[1].Columns[3].GetType() , "DS280");
2068 //check that no data exists
2069 // ReadXmlSchema - Table 1 row count
2070 Assert.AreEqual(0, ds2.Tables[0].Rows.Count , "DS281");
2072 // ReadXmlSchema - Table 2 row count
2073 Assert.AreEqual(0, ds2.Tables[1].Rows.Count , "DS282");
2075 //try to delete the file
2076 System.IO.File.Delete(sTempFileName);
2079 [Test] public void ReadXmlSchema_ByTextReader()
2081 DataSet ds1 = new DataSet();
2082 ds1.Tables.Add(DataProvider.CreateParentDataTable());
2083 ds1.Tables.Add(DataProvider.CreateChildDataTable());
2085 System.IO.StringWriter sw = new System.IO.StringWriter();
2086 //write xml file, schema only
2087 ds1.WriteXmlSchema(sw);
2089 System.IO.StringReader sr = new System.IO.StringReader(sw.GetStringBuilder().ToString());
2090 //copy both data and schema
2091 DataSet ds2 = new DataSet();
2092 ds2.ReadXmlSchema(sr);
2095 // ReadXmlSchema - Tables count
2096 Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS283");
2098 // ReadXmlSchema - Tables 0 Col count
2099 Assert.AreEqual(ds1.Tables[0].Columns.Count , ds2.Tables[0].Columns.Count , "DS284");
2101 // ReadXmlSchema - Tables 1 Col count
2102 Assert.AreEqual(ds1.Tables[1].Columns.Count , ds2.Tables[1].Columns.Count , "DS285");
2104 //check some colummns types
2105 // ReadXmlSchema - Tables 0 Col type
2106 Assert.AreEqual(ds1.Tables[0].Columns[0].GetType() , ds2.Tables[0].Columns[0].GetType() , "DS286");
2108 // ReadXmlSchema - Tables 1 Col type
2109 Assert.AreEqual(ds1.Tables[1].Columns[3].GetType() , ds2.Tables[1].Columns[3].GetType() , "DS287");
2111 //check that no data exists
2112 // ReadXmlSchema - Table 1 row count
2113 Assert.AreEqual(0, ds2.Tables[0].Rows.Count , "DS288");
2115 // ReadXmlSchema - Table 2 row count
2116 Assert.AreEqual(0, ds2.Tables[1].Rows.Count , "DS289");
2119 [Test] public void ReadXmlSchema_ByXmlReader()
2121 DataSet ds1 = new DataSet();
2122 ds1.Tables.Add(DataProvider.CreateParentDataTable());
2123 ds1.Tables.Add(DataProvider.CreateChildDataTable());
2125 System.IO.StringWriter sw = new System.IO.StringWriter();
2126 System.Xml.XmlTextWriter xmlTW = new System.Xml.XmlTextWriter(sw);
2127 //write xml file, schema only
2128 ds1.WriteXmlSchema(xmlTW);
2131 System.IO.StringReader sr = new System.IO.StringReader(sw.ToString());
2132 System.Xml.XmlTextReader xmlTR = new System.Xml.XmlTextReader(sr);
2134 //copy both data and schema
2135 DataSet ds2 = new DataSet();
2136 ds2.ReadXmlSchema(xmlTR);
2139 // ReadXmlSchema - Tables count
2140 Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS290");
2142 // ReadXmlSchema - Tables 0 Col count
2143 Assert.AreEqual(ds1.Tables[0].Columns.Count , ds2.Tables[0].Columns.Count , "DS291");
2145 // ReadXmlSchema - Tables 1 Col count
2146 Assert.AreEqual(ds1.Tables[1].Columns.Count , ds2.Tables[1].Columns.Count , "DS292");
2148 //check some colummns types
2149 // ReadXmlSchema - Tables 0 Col type
2150 Assert.AreEqual(ds1.Tables[0].Columns[0].GetType() , ds2.Tables[0].Columns[0].GetType() , "DS293");
2152 // ReadXmlSchema - Tables 1 Col type
2153 Assert.AreEqual(ds1.Tables[1].Columns[3].GetType() , ds2.Tables[1].Columns[3].GetType() , "DS294");
2155 //check that no data exists
2156 // ReadXmlSchema - Table 1 row count
2157 Assert.AreEqual(0, ds2.Tables[0].Rows.Count , "DS295");
2159 // ReadXmlSchema - Table 2 row count
2160 Assert.AreEqual(0, ds2.Tables[1].Rows.Count , "DS296");
2164 [Category ("NotWorking")]
2165 public void ReadXml_Strg()
2167 string sTempFileName = "tmpDataSet_ReadWriteXml_43894.xml" ;
2169 DataSet ds1 = new DataSet();
2170 ds1.Tables.Add(DataProvider.CreateParentDataTable());
2171 ds1.Tables.Add(DataProvider.CreateChildDataTable());
2173 //add data to check GH bug of DataSet.ReadXml of empty strings
2174 ds1.Tables[1].Rows.Add(new object[] {7,1,string.Empty,string.Empty,new DateTime(2000,1,1,0,0,0,0),35});
2175 ds1.Tables[1].Rows.Add(new object[] {7,2," "," ",new DateTime(2000,1,1,0,0,0,0),35});
2176 ds1.Tables[1].Rows.Add(new object[] {7,3,"","",new DateTime(2000,1,1,0,0,0,0),35});
2178 //write xml file, data only
2179 ds1.WriteXml(sTempFileName);
2181 //copy both data and schema
2182 DataSet ds2 = ds1.Copy();
2186 ds2.ReadXml(sTempFileName);
2189 // ReadXml - Tables count
2190 Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS297");
2192 // ReadXml - Table 1 row count
2193 Assert.AreEqual(ds2.Tables[0].Rows.Count, ds1.Tables[0].Rows.Count , "DS298");
2195 // ReadXml - Table 2 row count
2196 Assert.AreEqual(ds2.Tables[1].Rows.Count, ds1.Tables[1].Rows.Count , "DS299");
2198 //try to delete the file
2199 System.IO.File.Delete(sTempFileName);
2203 [Category ("NotWorking")]
2204 public void ReadXml_Strm()
2206 DataSet ds1 = new DataSet();
2207 ds1.Tables.Add(DataProvider.CreateParentDataTable());
2208 ds1.Tables.Add(DataProvider.CreateChildDataTable());
2210 //add data to check GH bug of DataSet.ReadXml of empty strings
2211 ds1.Tables[1].Rows.Add(new object[] {7,1,string.Empty,string.Empty,new DateTime(2000,1,1,0,0,0,0),35});
2212 ds1.Tables[1].Rows.Add(new object[] {7,2," "," ",new DateTime(2000,1,1,0,0,0,0),35});
2213 ds1.Tables[1].Rows.Add(new object[] {7,3,"","",new DateTime(2000,1,1,0,0,0,0),35});
2215 System.IO.MemoryStream ms = new System.IO.MemoryStream();
2216 //write xml file, data only
2219 //copy both data and schema
2220 DataSet ds2 = ds1.Copy();
2228 // ReadXml - Tables count
2229 Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS300");
2231 // ReadXml - Table 1 row count
2232 Assert.AreEqual(ds2.Tables[0].Rows.Count, ds1.Tables[0].Rows.Count , "DS301");
2234 // ReadXml - Table 2 row count
2235 Assert.AreEqual(ds2.Tables[1].Rows.Count, ds1.Tables[1].Rows.Count , "DS302");
2240 [Test] public void ReadXml_Strm2()
2242 string input = string.Empty;
2244 System.IO.StringReader sr;
2245 DataSet ds = new DataSet();
2247 input += "<?xml version=\"1.0\"?>";
2248 input += "<Stock name=\"MSFT\">";
2249 input += " <Company name=\"Microsoft Corp.\"/>";
2250 input += " <Price type=\"high\">";
2251 input += " <Value>10.0</Value>";
2252 input += " <Date>01/20/2000</Date>";
2253 input += " </Price>";
2254 input += " <Price type=\"low\">";
2255 input += " <Value>1.0</Value>";
2256 input += " <Date>03/21/2002</Date>";
2257 input += " </Price>";
2258 input += " <Price type=\"current\">";
2259 input += " <Value>3.0</Value>";
2260 input += " <Date>TODAY</Date>";
2261 input += " </Price>";
2262 input += "</Stock>";
2264 sr = new System.IO.StringReader(input);
2269 Assert.AreEqual(2 , ds.Relations.Count , "DS303");
2272 Assert.AreEqual("Stock_Company" , ds.Relations[0].RelationName , "DS304");
2275 Assert.AreEqual("Stock_Price" , ds.Relations[1].RelationName , "DS305");
2278 Assert.AreEqual(3, ds.Tables.Count , "DS306");
2280 // Tables[0] ChildRelations count
2281 Assert.AreEqual(2, ds.Tables[0].ChildRelations.Count , "DS307");
2283 // Tables[0] ChildRelations[0] name
2284 Assert.AreEqual("Stock_Company", ds.Tables[0].ChildRelations[0].RelationName , "DS308");
2286 // Tables[0] ChildRelations[1] name
2287 Assert.AreEqual("Stock_Price", ds.Tables[0].ChildRelations[1].RelationName , "DS309");
2289 // Tables[1] ChildRelations count
2290 Assert.AreEqual(0, ds.Tables[1].ChildRelations.Count , "DS310");
2292 // Tables[2] ChildRelations count
2293 Assert.AreEqual(0, ds.Tables[2].ChildRelations.Count , "DS311");
2295 // Tables[0] ParentRelations count
2296 Assert.AreEqual(0, ds.Tables[0].ParentRelations.Count , "DS312");
2298 // Tables[1] ParentRelations count
2299 Assert.AreEqual(1, ds.Tables[1].ParentRelations.Count , "DS313");
2301 // Tables[1] ParentRelations[0] name
2302 Assert.AreEqual("Stock_Company", ds.Tables[1].ParentRelations[0].RelationName , "DS314");
2304 // Tables[2] ParentRelations count
2305 Assert.AreEqual(1, ds.Tables[2].ParentRelations.Count , "DS315");
2307 // Tables[2] ParentRelations[0] name
2308 Assert.AreEqual("Stock_Price", ds.Tables[2].ParentRelations[0].RelationName , "DS316");
2310 [Test] public void ReadXml_Strm3()
2312 DataSet ds = new DataSet("TestDataSet");
2313 string input = string.Empty;
2314 System.IO.StringReader sr;
2316 input += "<?xml version=\"1.0\" standalone=\"yes\"?>";
2317 input += "<Stocks><Stock name=\"MSFT\"><Company name=\"Microsoft Corp.\" /><Price type=\"high\"><Value>10.0</Value>";
2318 input += "<Date>01/20/2000</Date></Price><Price type=\"low\"><Value>10</Value><Date>03/21/2002</Date></Price>";
2319 input += "<Price type=\"current\"><Value>3.0</Value><Date>TODAY</Date></Price></Stock><Stock name=\"GE\">";
2320 input += "<Company name=\"General Electric\" /><Price type=\"high\"><Value>22.23</Value><Date>02/12/2001</Date></Price>";
2321 input += "<Price type=\"low\"><Value>1.97</Value><Date>04/20/2003</Date></Price><Price type=\"current\"><Value>3.0</Value>";
2322 input += "<Date>TODAY</Date></Price></Stock></Stocks>";
2323 sr = new System.IO.StringReader(input);
2324 ds.EnforceConstraints = false;
2327 //Test that all added columns have "Hidden" mapping type.
2328 // StockTable.Stock_IdCol.ColumnMapping
2329 Assert.AreEqual(MappingType.Hidden, ds.Tables["Stock"].Columns["Stock_Id"].ColumnMapping, "DS317");
2331 // CompanyTable.Stock_IdCol.ColumnMapping
2332 Assert.AreEqual(MappingType.Hidden, ds.Tables["Company"].Columns["Stock_Id"].ColumnMapping, "DS318");
2334 // PriceTable.Stock_IdCol.ColumnMapping
2335 Assert.AreEqual(MappingType.Hidden, ds.Tables["Price"].Columns["Stock_Id"].ColumnMapping, "DS319");
2338 [Test] public void ReadXml_Strm4()
2340 m_ds = new DataSet("Stocks");
2341 string input = string.Empty;
2342 System.IO.StringReader sr;
2344 input += "<?xml version=\"1.0\"?>";
2345 input += "<Stocks>";
2346 input += " <Stock name=\"MSFT\">";
2347 input += " <Company name=\"Microsoft Corp.\" />";
2348 input += " <Company name=\"General Electric\"/>";
2349 input += " <Price type=\"high\">";
2350 input += " <Value>10.0</Value>";
2351 input += " <Date>01/20/2000</Date>";
2352 input += " </Price>";
2353 input += " <Price type=\"low\">";
2354 input += " <Value>1.0</Value>";
2355 input += " <Date>03/21/2002</Date>";
2356 input += " </Price>";
2357 input += " <Price type=\"current\">";
2358 input += " <Value>3.0</Value>";
2359 input += " <Date>TODAY</Date>";
2360 input += " </Price>";
2361 input += " </Stock>";
2362 input += " <Stock name=\"GE\">";
2363 input += " <Company name=\"GE company\"/>";
2364 input += " <Price type=\"high\">";
2365 input += " <Value>22.23</Value>";
2366 input += " <Date>02/12/2001</Date>";
2367 input += " </Price>";
2368 input += " <Price type=\"low\">";
2369 input += " <Value>1.97</Value>";
2370 input += " <Date>04/20/2003</Date>";
2371 input += " </Price>";
2372 input += " <Price type=\"current\">";
2373 input += " <Value>3.0</Value>";
2374 input += " <Date>TODAY</Date>";
2375 input += " </Price>";
2376 input += " </Stock>";
2377 input += " <Stock name=\"Intel\">";
2378 input += " <Company name=\"Intel Corp.\"/>";
2379 input += " <Company name=\"Test1\" />";
2380 input += " <Company name=\"Test2\"/>";
2381 input += " <Price type=\"high\">";
2382 input += " <Value>15.0</Value>";
2383 input += " <Date>01/25/2000</Date>";
2384 input += " </Price>";
2385 input += " <Price type=\"low\">";
2386 input += " <Value>1.0</Value>";
2387 input += " <Date>03/23/2002</Date>";
2388 input += " </Price>";
2389 input += " <Price type=\"current\">";
2390 input += " <Value>3.0</Value>";
2391 input += " <Date>TODAY</Date>";
2392 input += " </Price>";
2393 input += " </Stock>";
2394 input += " <Stock name=\"Mainsoft\">";
2395 input += " <Company name=\"Mainsoft Corp.\"/>";
2396 input += " <Price type=\"high\">";
2397 input += " <Value>30.0</Value>";
2398 input += " <Date>01/26/2000</Date>";
2399 input += " </Price>";
2400 input += " <Price type=\"low\">";
2401 input += " <Value>1.0</Value>";
2402 input += " <Date>03/26/2002</Date>";
2403 input += " </Price>";
2404 input += " <Price type=\"current\">";
2405 input += " <Value>27.0</Value>";
2406 input += " <Date>TODAY</Date>";
2407 input += " </Price>";
2408 input += " </Stock>";
2409 input += "</Stocks>";
2411 sr = new System.IO.StringReader(input);
2412 m_ds.EnforceConstraints = true;
2414 this.privateTestCase("TestCase 1", "Company", "name='Microsoft Corp.'", "Stock", "name='MSFT'", "DS320");
2415 this.privateTestCase("TestCase 2", "Company", "name='General Electric'", "Stock", "name='MSFT'", "DS321");
2416 this.privateTestCase("TestCase 3", "Price", "Date='01/20/2000'", "Stock", "name='MSFT'", "DS322");
2417 this.privateTestCase("TestCase 4", "Price", "Date='03/21/2002'", "Stock", "name='MSFT'", "DS323");
2418 this.privateTestCase("TestCase 5", "Company", "name='GE company'", "Stock", "name='GE'", "DS324");
2419 this.privateTestCase("TestCase 6", "Price", "Date='02/12/2001'", "Stock", "name='GE'", "DS325");
2420 this.privateTestCase("TestCase 7", "Price", "Date='04/20/2003'", "Stock", "name='GE'", "DS326");
2421 this.privateTestCase("TestCase 8", "Company", "name='Intel Corp.'", "Stock", "name='Intel'", "DS327");
2422 this.privateTestCase("TestCase 9", "Company", "name='Test1'", "Stock", "name='Intel'", "DS328");
2423 this.privateTestCase("TestCase 10", "Company", "name='Test2'", "Stock", "name='Intel'", "DS329");
2424 this.privateTestCase("TestCase 11", "Price", "Date='01/25/2000'", "Stock", "name='Intel'", "DS330");
2425 this.privateTestCase("TestCase 12", "Price", "Date='03/23/2002'", "Stock", "name='Intel'", "DS331");
2426 this.privateTestCase("TestCase 13", "Company", "name='Mainsoft Corp.'", "Stock", "name='Mainsoft'", "DS332");
2427 this.privateTestCase("TestCase 12", "Price", "Date='01/26/2000'", "Stock", "name='Mainsoft'", "DS333");
2428 this.privateTestCase("TestCase 12", "Price", "Date='03/26/2002'", "Stock", "name='Mainsoft'", "DS334");
2431 private void privateTestCase(string name, string toTable, string toTestSelect, string toCompareTable, string toCompareSelect, string AssertTag)
2433 DataRow drToTest = m_ds.Tables[toTable].Select(toTestSelect)[0];
2434 DataRow drToCompare = m_ds.Tables[toCompareTable].Select(toCompareSelect)[0];
2435 Assert.AreEqual(m_ds.Tables[toTable].Select(toTestSelect)[0]["Stock_Id"], m_ds.Tables[toCompareTable].Select(toCompareSelect)[0]["Stock_Id"], AssertTag);
2439 public void ReadXml_Strm5()
2444 #region "TestCase 1 - Empty string"
2446 DataSet ds = new DataSet();
2447 System.IO.StringReader sr = new System.IO.StringReader (string.Empty);
2448 System.Xml.XmlTextReader xReader = new System.Xml.XmlTextReader(sr);
2451 ds.ReadXml (xReader);
2452 Assert.Fail("DS335: ReadXml Failed to throw XmlException");
2454 catch (System.Xml.XmlException) {}
2455 catch (AssertionException exc) {throw exc;}
2456 catch (Exception exc)
2458 Assert.Fail("DS336: ReadXml. Wrong exception type. Got:" + exc);
2461 #region "TestCase 2 - Single element"
2462 name = "Single element";
2463 expected = "DataSet Name=a Tables count=0";
2464 xmlData = "<a>1</a>";
2465 PrivateTestCase(name, expected, xmlData);
2467 #region "TestCase 3 - Nesting one level single element."
2468 name = "Nesting one level single element.";
2469 expected = "DataSet Name=NewDataSet Tables count=1 Table Name=a Rows count=1 Items count=1 1";
2470 xmlData = "<a><b>1</b></a>";
2471 PrivateTestCase(name, expected, xmlData);
2473 #region "TestCase 4 - Nesting one level multiple elements."
2474 name = "Nesting one level multiple elements.";
2475 expected = "DataSet Name=NewDataSet Tables count=1 Table Name=a Rows count=1 Items count=3 bb cc dd";
2476 xmlData = "<a><b>bb</b><c>cc</c><d>dd</d></a>";
2477 PrivateTestCase(name, expected, xmlData);
2479 #region "TestCase 5 - Nesting two levels single elements."
2480 name = "Nesting two levels single elements.";
2481 expected = "DataSet Name=a Tables count=1 Table Name=b Rows count=1 Items count=1 cc";
2482 xmlData = "<a><b><c>cc</c></b></a>";
2483 PrivateTestCase(name, expected, xmlData);
2485 #region "TestCase 6 - Nesting two levels multiple elements."
2486 name = "Nesting two levels multiple elements.";
2487 expected = "DataSet Name=a Tables count=1 Table Name=b Rows count=1 Items count=2 cc dd";
2488 xmlData = string.Empty;
2491 xmlData += "<c>cc</c>";
2492 xmlData += "<d>dd</d>";
2495 PrivateTestCase(name, expected, xmlData);
2497 #region "TestCase 7 - Nesting two levels multiple elements."
2498 name = "Nesting two levels multiple elements.";
2499 expected = "DataSet Name=a Tables count=2 Table Name=b Rows count=1 Items count=2 cc dd Table Name=e Rows count=1 Items count=2 cc dd";
2500 xmlData = string.Empty;
2503 xmlData += "<c>cc</c>";
2504 xmlData += "<d>dd</d>";
2507 xmlData += "<c>cc</c>";
2508 xmlData += "<d>dd</d>";
2511 PrivateTestCase(name, expected, xmlData);
2513 #region "TestCase 8 - Nesting three levels single element."
2514 name = "Nesting three levels single element.";
2515 xmlData = string.Empty;
2519 xmlData += "<d>dd</d>";
2523 expected = "DataSet Name=a Tables count=2 Table Name=b Rows count=1 Items count=1 0 Table Name=c Rows count=1 Items count=2 0 dd";
2524 PrivateTestCase(name, expected, xmlData);
2526 #region "TestCase 9 - Nesting three levels multiple elements."
2527 name = "Nesting three levels multiple elements.";
2528 xmlData = string.Empty;
2532 xmlData += "<d>dd</d>";
2533 xmlData += "<e>ee</e>";
2537 expected = "DataSet Name=a Tables count=2 Table Name=b Rows count=1 Items count=1 0 Table Name=c Rows count=1 Items count=3 0 dd ee";
2538 PrivateTestCase(name, expected, xmlData);
2540 #region "TestCase 10 - Nesting three levels multiple elements."
2541 name = "Nesting three levels multiple elements.";
2542 xmlData = string.Empty;
2546 xmlData += "<d>dd</d>";
2547 xmlData += "<e>ee</e>";
2549 xmlData += "<f>ff</f>";
2552 expected = "DataSet Name=a Tables count=2 Table Name=b Rows count=1 Items count=2 0 ff Table Name=c Rows count=1 Items count=3 0 dd ee";
2553 PrivateTestCase(name, expected, xmlData);
2555 #region "TestCase 11 - Nesting three levels multiple elements."
2556 name = "Nesting three levels multiple elements.";
2557 xmlData = string.Empty;
2561 xmlData += "<d>dd</d>";
2562 xmlData += "<e>ee</e>";
2564 xmlData += "<f>ff</f>";
2566 xmlData += "<h>hh</h>";
2567 xmlData += "<i>ii</i>";
2569 xmlData += "<j>jj</j>";
2572 expected = "DataSet Name=a Tables count=3 Table Name=b Rows count=1 Items count=3 0 ff jj Table Name=c Rows count=1 Items count=3 0 dd ee Table Name=g Rows count=1 Items count=3 0 hh ii";
2573 PrivateTestCase(name, expected, xmlData);
2575 #region "TestCase 12 - Nesting three levels multiple elements."
2576 name = "Nesting three levels multiple elements.";
2577 xmlData = string.Empty;
2581 xmlData += "<d>dd</d>";
2582 xmlData += "<e>ee</e>";
2584 xmlData += "<f>ff</f>";
2588 xmlData += "<i>ii</i>";
2589 xmlData += "<j>jj</j>";
2591 xmlData += "<f>ff</f>";
2594 expected = "DataSet Name=a Tables count=4 Table Name=b Rows count=1 Items count=2 0 ff Table Name=c Rows count=1 Items count=3 0 dd ee Table Name=g Rows count=1 Items count=2 ff 0 Table Name=h Rows count=1 Items count=3 0 ii jj";
2595 PrivateTestCase(name, expected, xmlData);
2597 #region "TestCase 13 - Nesting three levels multiple elements."
2598 name = "Nesting three levels multiple elements.";
2599 xmlData = string.Empty;
2603 xmlData += "<d>dd</d>";
2604 xmlData += "<e>ee</e>";
2606 xmlData += "<f>ff</f>";
2608 xmlData += "<l>ll</l>";
2609 xmlData += "<m>mm</m>";
2611 xmlData += "<n>nn</n>";
2615 xmlData += "<i>ii</i>";
2616 xmlData += "<j>jj</j>";
2618 xmlData += "<o>oo</o>";
2621 expected = "DataSet Name=a Tables count=5 Table Name=b Rows count=1 Items count=3 0 ff nn Table Name=c Rows count=1 Items count=3 0 dd ee Table Name=k Rows count=1 Items count=3 0 ll mm Table Name=g Rows count=1 Items count=2 0 oo Table Name=h Rows count=1 Items count=3 0 ii jj";
2622 PrivateTestCase(name, expected, xmlData);
2625 #region "TestCase 14 - for Bug 2387 (System.Data.DataSet.ReadXml(..) - ArgumentException while reading specific XML)"
2627 name = "Specific XML - for Bug 2387";
2628 expected = "DataSet Name=PKRoot Tables count=2 Table Name=Content Rows count=4 Items count=2 0 Items count=2 1 103 Items count=2 2 123 Items count=2 3 252 Table Name=Cont Rows count=3 Items count=3 1 103 0 Items count=3 2 123 0 Items count=3 3 252 -4";
2629 xmlData = "<PKRoot><Content /><Content><ContentId>103</ContentId><Cont><ContentId>103</ContentId><ContentStatusId>0</ContentStatusId></Cont></Content><Content><ContentId>123</ContentId><Cont><ContentId>123</ContentId><ContentStatusId>0</ContentStatusId></Cont></Content><Content><ContentId>252</ContentId><Cont><ContentId>252</ContentId><ContentStatusId>-4</ContentStatusId></Cont></Content></PKRoot>";
2630 PrivateTestCase(name, expected, xmlData);
2635 private void PrivateTestCase(string a_name, string a_expected, string a_xmlData)
2637 DataSet ds = new DataSet();
2638 System.IO.StringReader sr = new System.IO.StringReader(a_xmlData) ;
2639 System.Xml.XmlTextReader xReader = new System.Xml.XmlTextReader(sr) ;
2640 ds.ReadXml (xReader);
2641 Assert.AreEqual(a_expected, this.dataSetDescription(ds), "DS337");
2644 private string dataSetDescription(DataSet ds)
2646 string desc = string.Empty;
2647 desc += "DataSet Name=" + ds.DataSetName;
2648 desc += " Tables count=" + ds.Tables.Count;
2649 foreach (DataTable dt in ds.Tables)
2651 desc += " Table Name=" + dt.TableName;
2652 desc += " Rows count=" + dt.Rows.Count;
2654 string[] colNames = new string[dt.Columns.Count];
2655 for(int i = 0; i < dt.Columns.Count; i++)
2656 colNames[i] = dt.Columns[i].ColumnName;
2658 Array.Sort(colNames);
2660 foreach (DataRow dr in dt.Rows)
2662 desc += " Items count=" + dr.ItemArray.Length;
2663 foreach (string name in colNames)
2665 desc += " " + dr[name].ToString();
2672 [Test] public void ReadXml_Strm6()
2675 DataSet ds = new DataSet();
2676 string xmlData = string.Empty;
2679 xmlData += "<c>1</c>";
2680 xmlData += "<c>2</c>";
2681 xmlData += "<c>3</c>";
2684 System.IO.StringReader sr = new System.IO.StringReader(xmlData) ;
2685 System.Xml.XmlTextReader xReader = new System.Xml.XmlTextReader(sr) ;
2686 ds.ReadXml (xReader);
2687 Assert.AreEqual(3, ds.Tables["c"].Rows.Count, "DS338");
2691 public void ReadXmlSchema_2()
2693 DataSet ds = new DataSet();
2694 string xmlData = string.Empty;
2695 xmlData += "<?xml version=\"1.0\"?>";
2696 xmlData += "<xs:schema id=\"SiteConfiguration\" targetNamespace=\"http://tempuri.org/PortalCfg.xsd\" xmlns:mstns=\"http://tempuri.org/PortalCfg.xsd\" xmlns=\"http://tempuri.org/PortalCfg.xsd\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" attributeFormDefault=\"qualified\" elementFormDefault=\"qualified\">";
2697 xmlData += "<xs:element name=\"SiteConfiguration\" msdata:IsDataSet=\"true\" msdata:EnforceConstraints=\"False\">";
2698 xmlData += "<xs:complexType>";
2699 xmlData += "<xs:choice maxOccurs=\"unbounded\">";
2700 xmlData += "<xs:element name=\"Tab\">";
2701 xmlData += "<xs:complexType>";
2702 xmlData += "<xs:sequence>";
2703 xmlData += "<xs:element name=\"Module\" minOccurs=\"0\" maxOccurs=\"unbounded\">";
2704 xmlData += "<xs:complexType>";
2705 xmlData += "<xs:attribute name=\"ModuleId\" form=\"unqualified\" type=\"xs:int\" />";
2706 xmlData += "</xs:complexType>";
2707 xmlData += "</xs:element>";
2708 xmlData += "</xs:sequence>";
2709 xmlData += "<xs:attribute name=\"TabId\" form=\"unqualified\" type=\"xs:int\" />";
2710 xmlData += "</xs:complexType>";
2711 xmlData += "</xs:element>";
2712 xmlData += "</xs:choice>";
2713 xmlData += "</xs:complexType>";
2714 xmlData += "<xs:key name=\"TabKey\" msdata:PrimaryKey=\"true\">";
2715 xmlData += "<xs:selector xpath=\".//mstns:Tab\" />";
2716 xmlData += "<xs:field xpath=\"@TabId\" />";
2717 xmlData += "</xs:key>";
2718 xmlData += "<xs:key name=\"ModuleKey\" msdata:PrimaryKey=\"true\">";
2719 xmlData += "<xs:selector xpath=\".//mstns:Module\" />";
2720 xmlData += "<xs:field xpath=\"@ModuleID\" />";
2721 xmlData += "</xs:key>";
2722 xmlData += "</xs:element>";
2723 xmlData += "</xs:schema>";
2725 ds.ReadXmlSchema(new StringReader(xmlData));
2729 [Category ("NotWorking")]
2730 public void ReadXml_ByTextReader()
2732 DataSet ds1 = new DataSet();
2733 ds1.Tables.Add(DataProvider.CreateParentDataTable());
2734 ds1.Tables.Add(DataProvider.CreateChildDataTable());
2736 //add data to check GH bug of DataSet.ReadXml of empty strings
2737 ds1.Tables[1].Rows.Add(new object[] {7,1,string.Empty,string.Empty,new DateTime(2000,1,1,0,0,0,0),35});
2738 ds1.Tables[1].Rows.Add(new object[] {7,2," "," ",new DateTime(2000,1,1,0,0,0,0),35});
2739 ds1.Tables[1].Rows.Add(new object[] {7,3,"","",new DateTime(2000,1,1,0,0,0,0),35});
2741 System.IO.StringWriter sw = new System.IO.StringWriter();
2742 //write xml file, data only
2745 //copy both data and schema
2746 DataSet ds2 = ds1.Copy();
2750 System.IO.StringReader sr = new System.IO.StringReader(sw.GetStringBuilder().ToString());
2754 // ReadXml - Tables count
2755 Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS339");
2757 // ReadXml - Table 1 row count
2758 Assert.AreEqual(ds2.Tables[0].Rows.Count, ds1.Tables[0].Rows.Count , "DS340");
2760 // ReadXml - Table 2 row count
2761 Assert.AreEqual(ds2.Tables[1].Rows.Count, ds1.Tables[1].Rows.Count , "DS341");
2768 [Category ("NotWorking")]
2769 public void ReadXml_ByXmlReader()
2771 DataSet ds1 = new DataSet();
2772 ds1.Tables.Add(DataProvider.CreateParentDataTable());
2773 ds1.Tables.Add(DataProvider.CreateChildDataTable());
2775 //add data to check GH bug of DataSet.ReadXml of empty strings
2776 ds1.Tables[1].Rows.Add(new object[] {7,1,string.Empty,string.Empty,new DateTime(2000,1,1,0,0,0,0),35});
2777 ds1.Tables[1].Rows.Add(new object[] {7,2," "," ",new DateTime(2000,1,1,0,0,0,0),35});
2778 ds1.Tables[1].Rows.Add(new object[] {7,3,"","",new DateTime(2000,1,1,0,0,0,0),35});
2780 System.IO.StringWriter sw = new System.IO.StringWriter();
2781 System.Xml.XmlTextWriter xmlTW = new System.Xml.XmlTextWriter(sw);
2783 //write xml file, data only
2784 ds1.WriteXml(xmlTW);
2785 //ds1.WriteXml("C:\\Temp\\q.xml");
2787 //copy both data and schema
2788 DataSet ds2 = ds1.Copy();
2791 System.IO.StringReader sr = new System.IO.StringReader(sw.ToString());
2792 System.Xml.XmlTextReader xmlTR = new System.Xml.XmlTextReader(sr);
2796 // ReadXml - Tables count
2797 Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS342");
2799 // ReadXml - Table 1 row count
2800 Assert.AreEqual(ds2.Tables[0].Rows.Count, ds1.Tables[0].Rows.Count , "DS343");
2802 // ReadXml - Table 2 row count
2803 Assert.AreEqual(ds2.Tables[1].Rows.Count, ds1.Tables[1].Rows.Count , "DS344");
2806 [Test] public void WriteXmlSchema_ForignKeyConstraint ()
2808 DataSet ds1 = new DataSet();
2810 DataTable table1 = ds1.Tables.Add();
2811 DataTable table2 = ds1.Tables.Add();
2813 DataColumn col1_1 = table1.Columns.Add ("col1", typeof (int));
2814 DataColumn col2_1 = table2.Columns.Add ("col1", typeof (int));
2816 table2.Constraints.Add ("fk", col1_1, col2_1);
2818 StringWriter sw = new StringWriter ();
2819 ds1.WriteXmlSchema (sw);
2820 String xml = sw.ToString ();
2822 Assert.IsTrue (xml.IndexOf (@"<xs:keyref name=""fk"" refer=""Constraint1"" " +
2823 @"msdata:ConstraintOnly=""true"">") != -1, "#1");
2826 [Test] public void WriteXmlSchema_RelationAnnotation ()
2828 DataSet ds1 = new DataSet();
2830 DataTable table1 = ds1.Tables.Add();
2831 DataTable table2 = ds1.Tables.Add();
2833 DataColumn col1_1 = table1.Columns.Add ("col1", typeof (int));
2834 DataColumn col2_1 = table2.Columns.Add ("col1", typeof (int));
2836 ds1.Relations.Add ("rel", col1_1, col2_1, false);
2838 StringWriter sw = new StringWriter ();
2839 ds1.WriteXmlSchema (sw);
2840 String xml = sw.ToString ();
2843 Assert.IsTrue (xml.IndexOf (@"<msdata:Relationship name=""rel"" msdata:parent=""Table1""" +
2844 @" msdata:child=""Table2"" msdata:parentkey=""col1"" " +
2845 @"msdata:childkey=""col1"" />") != -1, "#1");
2848 [Test] public void WriteXmlSchema_Relations_ForeignKeys ()
2850 System.IO.MemoryStream ms = null;
2851 System.IO.MemoryStream ms1 = null;
2853 DataSet ds1 = new DataSet();
2855 DataTable table1 = ds1.Tables.Add("Table 1");
2856 DataTable table2 = ds1.Tables.Add("Table 2");
2858 DataColumn col1_1 = table1.Columns.Add ("col 1", typeof (int));
2859 DataColumn col1_2 = table1.Columns.Add ("col 2", typeof (int));
2860 DataColumn col1_3 = table1.Columns.Add ("col 3", typeof (int));
2861 DataColumn col1_4 = table1.Columns.Add ("col 4", typeof (int));
2862 DataColumn col1_5 = table1.Columns.Add ("col 5", typeof (int));
2863 DataColumn col1_6 = table1.Columns.Add ("col 6", typeof (int));
2864 DataColumn col1_7 = table1.Columns.Add ("col 7", typeof (int));
2866 DataColumn col2_1 = table2.Columns.Add ("col 1", typeof (int));
2867 DataColumn col2_2 = table2.Columns.Add ("col 2", typeof (int));
2868 DataColumn col2_3 = table2.Columns.Add ("col 3", typeof (int));
2869 DataColumn col2_4 = table2.Columns.Add ("col 4", typeof (int));
2870 DataColumn col2_5 = table2.Columns.Add ("col 5", typeof (int));
2871 DataColumn col2_6 = table2.Columns.Add ("col 6", typeof (int));
2873 ds1.Relations.Add ("rel 1",
2874 new DataColumn[] {col1_1, col1_2},
2875 new DataColumn[] {col2_1, col2_2});
2876 ds1.Relations.Add ("rel 2",
2877 new DataColumn[] {col1_3, col1_4},
2878 new DataColumn[] {col2_3, col2_4},
2881 table1.Constraints.Add ("pk 1", col1_7, true);
2883 table2.Constraints.Add ("fk 1",
2884 new DataColumn[] {col1_5, col1_6},
2885 new DataColumn[] {col2_5, col2_6});
2887 ms = new System.IO.MemoryStream();
2888 ds1.WriteXmlSchema (ms);
2890 ms1 = new System.IO.MemoryStream (ms.GetBuffer());
2891 DataSet ds2 = new DataSet();
2892 ds2.ReadXmlSchema(ms1);
2894 Assert.AreEqual (2, ds2.Relations.Count, "#1");
2895 Assert.AreEqual (3, ds2.Tables [0].Constraints.Count, "#2");
2896 Assert.AreEqual (2, ds2.Tables [1].Constraints.Count, "#2");
2898 Assert.IsTrue (ds2.Relations.Contains ("rel 1"), "#3");
2899 Assert.IsTrue (ds2.Relations.Contains ("rel 2"), "#4");
2901 Assert.IsTrue (ds2.Tables [0].Constraints.Contains ("pk 1"), "#5");
2902 Assert.IsTrue (ds2.Tables [1].Constraints.Contains ("fk 1"), "#6");
2903 Assert.IsTrue (ds2.Tables [1].Constraints.Contains ("rel 1"), "#7");
2905 Assert.AreEqual (2, ds2.Relations ["rel 1"].ParentColumns.Length, "#8");
2906 Assert.AreEqual (2, ds2.Relations ["rel 1"].ChildColumns.Length, "#9");
2908 Assert.AreEqual (2, ds2.Relations ["rel 2"].ParentColumns.Length, "#10");
2909 Assert.AreEqual (2, ds2.Relations ["rel 2"].ChildColumns.Length, "#11");
2911 ForeignKeyConstraint fk = (ForeignKeyConstraint)ds2.Tables [1].Constraints ["fk 1"];
2912 Assert.AreEqual (2, fk.RelatedColumns.Length, "#12");
2913 Assert.AreEqual (2, fk.Columns.Length, "#13");
2916 [Test] public void RejectChanges()
2918 DataSet ds1,ds2 = new DataSet();
2919 ds2.Tables.Add(DataProvider.CreateParentDataTable());
2923 ds2.Tables[0].Rows[0][0] = "70";
2924 ds2.Tables[0].Rows[1].Delete();
2925 ds2.Tables[0].Rows.Add(new object[] {9,"string1","string2"});
2928 ds2.RejectChanges();
2929 Assert.AreEqual(ds2.GetXml(), ds1.GetXml(), "DS345");
2932 [Test] public void Relations()
2934 DataTable dtChild1,dtChild2,dtParent;
2935 DataSet ds = new DataSet();
2937 dtChild1 = DataProvider.CreateChildDataTable();
2938 dtChild1.TableName = "Child";
2939 dtChild2 = DataProvider.CreateChildDataTable();
2940 dtChild2.TableName = "CHILD";
2941 dtParent= DataProvider.CreateParentDataTable();
2942 //Add tables to dataset
2943 ds.Tables.Add(dtChild1);
2944 ds.Tables.Add(dtChild2);
2946 ds.Tables.Add(dtParent);
2948 DataRelation drl = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild1.Columns["ParentId"]);
2949 DataRelation drl1 = new DataRelation("Parent-CHILD",dtParent.Columns["ParentId"],dtChild2.Columns["ParentId"]);
2951 // Checking Relations - default value
2953 Assert.AreEqual(0, ds.Relations.Count , "DS346");
2955 ds.Relations.Add(drl);
2957 // Checking Relations Count
2958 Assert.AreEqual(1, ds.Relations.Count , "DS347");
2960 // Checking Relations Value
2961 Assert.AreEqual(drl, ds.Relations[0] , "DS348");
2963 // Checking Relations - get by name
2964 Assert.AreEqual(drl, ds.Relations["Parent-Child"] , "DS349");
2966 // Checking Relations - get by name case sensetive
2967 Assert.AreEqual(drl, ds.Relations["PARENT-CHILD"] , "DS350");
2969 // Checking Relations Count 2
2970 ds.Relations.Add(drl1);
2971 Assert.AreEqual(2, ds.Relations.Count , "DS351");
2973 // Checking Relations - get by name case sensetive,ArgumentException
2976 DataRelation tmp = ds.Relations["PARENT-CHILD"];
2977 Assert.Fail("DS352: Relations Failed to throw ArgumentException");
2979 catch (ArgumentException) {}
2980 catch (AssertionException exc) {throw exc;}
2981 catch (Exception exc)
2983 Assert.Fail("DS353: Relations. Wrong exception type. Got:" + exc);
2987 [Test] public void Reset()
2989 DataTable dt1 = DataProvider.CreateParentDataTable();
2990 DataTable dt2 = DataProvider.CreateChildDataTable();
2991 dt1.PrimaryKey = new DataColumn[] {dt1.Columns[0]};
2992 dt2.PrimaryKey = new DataColumn[] {dt2.Columns[0],dt2.Columns[1]};
2993 DataRelation rel = new DataRelation("Rel",dt1.Columns["ParentId"],dt2.Columns["ParentId"]);
2994 DataSet ds = new DataSet();
2995 ds.Tables.AddRange(new DataTable[] {dt1,dt2});
2996 ds.Relations.Add(rel);
3000 // Reset - Relations
3001 Assert.AreEqual(0 , ds.Relations.Count , "DS354");
3003 Assert.AreEqual(0 , ds.Tables.Count , "DS355");
3006 [Test] public void ShouldSerializeRelations()
3008 // DataSet ShouldSerializeRelations
3009 newDataSet ds = new newDataSet();
3011 Assert.AreEqual(true, ds.testMethod(), "DS356");
3014 class newDataSet:DataSet
3016 public bool testMethod()
3018 return ShouldSerializeRelations();
3021 [Test] public void ShouldSerializeTables()
3023 // DataSet ShouldSerializeTables
3024 newDataSet1 ds = new newDataSet1();
3026 Assert.AreEqual(true, ds.testMethod(), "DS357");
3029 class newDataSet1:DataSet
3031 public bool testMethod()
3033 return ShouldSerializeTables();
3036 [Test] public void Tables()
3038 //References by name to tables and relations in a DataSet are case-sensitive. Two or more tables or relations can exist in a DataSet that have the same name, but that differ in case. For example you can have Table1 and table1. In this situation, a reference to one of the tables by name must match the case of the table name exactly, otherwise an exception is thrown. For example, if the DataSet myDS contains tables Table1 and table1, you would reference Table1 by name as myDS.Tables["Table1"], and table1 as myDS.Tables ["table1"]. Attempting to reference either of the tables as myDS.Tables ["TABLE1"] would generate an exception.
3039 //The case-sensitivity rule does not apply if only one table or relation exists with a particular name. That is, if no other table or relation object in the DataSet matches the name of that particular table or relation object, even by a difference in case, you can reference the object by name using any case and no exception is thrown. For example, if the DataSet has only Table1, you can reference it using myDS.Tables["TABLE1"].
3040 //The CaseSensitive property of the DataSet does not affect this behavior. The CaseSensitive property
3042 DataSet ds = new DataSet();
3044 DataTable dt1 = new DataTable();
3045 DataTable dt2 = new DataTable();
3046 DataTable dt3 = new DataTable();
3047 dt3.TableName = "Table3";
3048 DataTable dt4 = new DataTable(dt3.TableName.ToUpper());
3050 // Checking Tables - default value
3052 Assert.AreEqual(0, ds.Tables.Count , "DS358");
3058 // Checking Tables Count
3059 Assert.AreEqual(3, ds.Tables.Count , "DS359");
3061 // Checking Tables Value 1
3062 Assert.AreEqual(dt1, ds.Tables[0] , "DS360");
3064 // Checking Tables Value 2
3065 Assert.AreEqual(dt2, ds.Tables[1] , "DS361");
3067 // Checking Tables Value 3
3068 Assert.AreEqual(dt3, ds.Tables[2] , "DS362");
3070 // Checking get table by name.ToUpper
3071 Assert.AreEqual(dt3, ds.Tables[dt3.TableName.ToUpper()] , "DS363");
3073 // Checking get table by name.ToLower
3074 Assert.AreEqual(dt3, ds.Tables[dt3.TableName.ToLower()] , "DS364");
3076 // Checking Tables add with name case insensetive
3077 ds.Tables.Add(dt4); //same name as Table3, but different case
3078 Assert.AreEqual(4, ds.Tables.Count , "DS365");
3080 // Checking get table by name
3081 Assert.AreEqual(dt4, ds.Tables[dt4.TableName] , "DS366");
3083 // Checking get table by name with diferent case, ArgumentException
3086 DataTable tmp = ds.Tables[dt4.TableName.ToLower()];
3087 Assert.Fail("DS367: Tables Failed to throw ArgumentException");
3089 catch (ArgumentException) {}
3090 catch (AssertionException exc) {throw exc;}
3091 catch (Exception exc)
3093 Assert.Fail("DS368: Tables. Wrong exception type. Got:" + exc);
3097 [Test] public void WriteXml_ByTextWriterXmlWriteMode()
3099 System.IO.StringReader sr = null;
3100 System.IO.StringWriter sw = null;
3104 // ReadXml - DataSetOut
3106 DataSet oDataset = new DataSet("DataSetOut");
3107 sw = new System.IO.StringWriter();
3108 oDataset.WriteXml(sw,System.Data.XmlWriteMode.WriteSchema);
3110 sr = new System.IO.StringReader(sw.GetStringBuilder().ToString());
3111 oDataset = new DataSet("DataSetOut");
3113 oDataset.ReadXml(sr);
3114 Assert.AreEqual(0, oDataset.Tables.Count , "DS369");
3122 [Test] public void ctor()
3128 Assert.AreEqual(true, ds != null , "DS370");
3131 [Test] public void ctor_ByDataSetName()
3136 ds = new DataSet("NewDataSet");
3137 Assert.AreEqual(true, ds != null , "DS371");
3140 Assert.AreEqual("NewDataSet" , ds.DataSetName , "DS372");
3143 [Test] public void extendedProperties()
3145 DataSet ds = new DataSet();
3146 PropertyCollection pc;
3148 pc = ds.ExtendedProperties ;
3150 // Checking ExtendedProperties default
3151 Assert.AreEqual(true, pc != null, "DS373");
3153 // Checking ExtendedProperties count
3154 Assert.AreEqual(0, pc.Count , "DS374");
3158 // Test for bug #76517
3159 [Test] public void SchemaSerializationModeTest ()
3161 DataSet ds = new DataSet ();
3162 Assert.AreEqual (SchemaSerializationMode.IncludeSchema,
3163 ds.SchemaSerializationMode, "#1");
3165 ds.SchemaSerializationMode = SchemaSerializationMode.ExcludeSchema;
3166 Assert.Fail ("#2 InvalidOperationException must be thrown");
3167 }catch (InvalidOperationException e) {
3173 ///<?xml version="1.0" encoding="utf-16"?>
3174 ///<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
3175 /// <xs:element name="NewDataSet" msdata:IsDataSet="true">
3176 /// <xs:complexType>
3177 /// <xs:choice maxOccurs="unbounded">
3178 /// <xs:element name="Parent">
3179 /// <xs:complexType>
3181 /// <xs:element name="ParentId" type="xs:int" minOccurs="0"/>
3182 /// <xs:element name="String1" type="xs:string" minOccurs="0"/>
3183 /// <xs:element name="String2" type="xs:string" minOccurs="0"/>
3184 /// <xs:element name="ParentDateTime" type="xs:dateTime" minOccurs="0"/>
3185 /// <xs:element name="ParentDouble" type="xs:double" minOccurs="0"/>
3186 /// <xs:element name="ParentBool" type="xs:boolean" minOccurs="0"/>
3188 /// </xs:complexType>
3191 /// </xs:complexType>
3196 public void ParentDataTableSchema()
3198 XmlDocument testedSchema;
3199 XmlNamespaceManager testedSchemaNamepaces;
3200 InitParentDataTableSchema(out testedSchema, out testedSchemaNamepaces);
3202 CheckNode("DataSet name", "/xs:schema/xs:element[@name='NewDataSet']", 1, testedSchema, testedSchemaNamepaces);
3204 CheckNode("Parent datatable name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element[@name='Parent']", 1, testedSchema, testedSchemaNamepaces);
3206 CheckNode("ParentId column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='ParentId']", 1, testedSchema, testedSchemaNamepaces);
3208 CheckNode("String1 column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='String1']", 1, testedSchema, testedSchemaNamepaces);
3210 CheckNode("String2 column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='String1']", 1, testedSchema, testedSchemaNamepaces);
3212 CheckNode("ParentDateTime column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='ParentDateTime']", 1, testedSchema, testedSchemaNamepaces);
3214 CheckNode("ParentDouble column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='ParentDouble']", 1, testedSchema, testedSchemaNamepaces);
3216 CheckNode("ParentBool column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='ParentBool']", 1, testedSchema, testedSchemaNamepaces);
3218 CheckNode("Int columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:int']", 1, testedSchema, testedSchemaNamepaces);
3220 CheckNode("string columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:string']", 2, testedSchema, testedSchemaNamepaces);
3222 CheckNode("dateTime columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:dateTime']", 1, testedSchema, testedSchemaNamepaces);
3224 CheckNode("double columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:double']", 1, testedSchema, testedSchemaNamepaces);
3226 CheckNode("boolean columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:boolean']", 1, testedSchema, testedSchemaNamepaces);
3228 CheckNode("minOccurs columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@minOccurs='0']", 6, testedSchema, testedSchemaNamepaces);
3231 private void InitParentDataTableSchema(out XmlDocument schemaDocInit, out XmlNamespaceManager namespaceManagerToInit)
3233 DataSet ds = new DataSet();
3234 ds.Tables.Add(DataProvider.CreateParentDataTable());
3235 string strXML = ds.GetXmlSchema();
3236 schemaDocInit = new XmlDocument();
3237 schemaDocInit.LoadXml(strXML);
3238 namespaceManagerToInit = new XmlNamespaceManager(schemaDocInit.NameTable);
3239 namespaceManagerToInit.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
3240 namespaceManagerToInit.AddNamespace("msdata", "urn:schemas-microsoft-com:xml-msdata");
3243 private void CheckNode(string description, string xPath, int expectedNodesCout, XmlDocument schemaDoc, XmlNamespaceManager nm)
3245 int actualNodeCount = schemaDoc.SelectNodes(xPath, nm).Count;
3246 Assert.AreEqual(expectedNodesCout,actualNodeCount, "DS75" + description);
3250 public void WriteXml_Stream()
3253 DataSet ds = new DataSet();
3254 string input = "<a><b><c>2</c></b></a>";
3255 System.IO.StringReader sr = new System.IO.StringReader(input) ;
3256 System.Xml.XmlTextReader xReader = new System.Xml.XmlTextReader(sr) ;
3257 ds.ReadXml (xReader);
3259 System.Text.StringBuilder sb = new System.Text.StringBuilder();
3260 System.IO.StringWriter sw = new System.IO.StringWriter(sb);
3261 System.Xml.XmlTextWriter xWriter = new System.Xml.XmlTextWriter(sw);
3262 ds.WriteXml(xWriter);
3263 string output = sb.ToString();
3264 Assert.AreEqual(input,output, "DS76");
3267 DataSet ds = new DataSet();
3268 string input = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><a><b><c>2</c></b></a>";
3269 string expectedOutput = "<a><b><c>2</c></b></a>";
3270 System.IO.StringReader sr = new System.IO.StringReader(input) ;
3271 System.Xml.XmlTextReader xReader = new System.Xml.XmlTextReader(sr) ;
3272 ds.ReadXml (xReader);
3274 System.Text.StringBuilder sb = new System.Text.StringBuilder();
3275 System.IO.StringWriter sw = new System.IO.StringWriter(sb);
3276 System.Xml.XmlTextWriter xWriter = new System.Xml.XmlTextWriter(sw);
3277 ds.WriteXml(xWriter);
3278 string output = sb.ToString();
3279 Assert.AreEqual(expectedOutput,output, "DS77");
3282 DataSet ds = new DataSet("DSName");
3283 System.IO.StringWriter sr = new System.IO.StringWriter();
3285 Assert.AreEqual("<DSName />",sr.ToString(), "DS78");
3288 DataSet ds = new DataSet();
3291 //Create parent table.
3292 dt = ds.Tables.Add("ParentTable");
3293 dt.Columns.Add("ParentTable_Id", typeof(int));
3294 dt.Columns.Add("ParentTableCol", typeof(int));
3295 dt.Rows.Add(new object[] {0,1});
3297 //Create child table.
3298 dt = ds.Tables.Add("ChildTable");
3299 dt.Columns.Add("ParentTable_Id", typeof(int));
3300 dt.Columns.Add("ChildTableCol", typeof(string));
3301 dt.Rows.Add(new object[] {0,"aa"});
3303 //Add a relation between parent and child table.
3304 ds.Relations.Add("ParentTable_ChildTable", ds.Tables["ParentTable"].Columns["ParentTable_Id"], ds.Tables["ChildTable"].Columns["ParentTable_Id"], true);
3305 ds.Relations["ParentTable_ChildTable"].Nested=true;
3307 //Reomve the Parent_Child relation.
3308 dt = ds.Tables["ChildTable"];
3309 dt.ParentRelations.Remove("ParentTable_ChildTable");
3311 //Remove the constraint created automatically to enforce the "ParentTable_ChildTable" relation.
3312 dt.Constraints.Remove("ParentTable_ChildTable");
3314 //Remove the child table from the dataset.
3315 ds.Tables.Remove("ChildTable");
3317 //Get the xml representation of the dataset.
3318 System.IO.StringWriter sr = new System.IO.StringWriter();
3320 string xml = sr.ToString();
3322 Assert.AreEqual(-1,xml.IndexOf("<ChildTable>"), "DS79");
3327 public void WriteXmlSchema_ConstraintNameWithSpaces ()
3329 DataSet ds = new DataSet ();
3330 DataTable table1 = ds.Tables.Add ("table1");
3331 DataTable table2 = ds.Tables.Add ("table2");
3333 table1.Columns.Add ("col1", typeof (int));
3334 table2.Columns.Add ("col1", typeof (int));
3336 table1.Constraints.Add ("uc 1", table1.Columns [0], false);
3337 table2.Constraints.Add ("fc 1", table1.Columns [0], table2.Columns [0]);
3339 StringWriter sw = new StringWriter ();
3341 //should not throw an exception
3342 ds.WriteXmlSchema (sw);
3346 public void ReadWriteXmlSchema_Nested ()
3348 DataSet ds = new DataSet ("dataset");
3349 ds.Tables.Add ("table1");
3350 ds.Tables.Add ("table2");
3351 ds.Tables[0].Columns.Add ("col");
3352 ds.Tables[1].Columns.Add ("col");
3353 ds.Relations.Add ("rel", ds.Tables [0].Columns [0],ds.Tables [1].Columns [0], true);
3354 ds.Relations [0].Nested = true;
3356 MemoryStream ms = new MemoryStream ();
3357 ds.WriteXmlSchema (ms);
3359 DataSet ds1 = new DataSet ();
3360 ds1.ReadXmlSchema (new MemoryStream (ms.GetBuffer ()));
3362 // no new relation, and <table>_Id columns, should get created when
3363 // Relation.Nested = true
3364 Assert.AreEqual (1, ds1.Relations.Count, "#1");
3365 Assert.AreEqual (1, ds1.Tables [0].Columns.Count, "#2");
3366 Assert.AreEqual (1, ds1.Tables [1].Columns.Count, "#3");
3370 public void ReadXmlSchema_Nested ()
3372 //when Relation.Nested = false, and the schema is nested, create new relations on <table>_Id
3374 DataSet ds = new DataSet ();
3375 ds.ReadXmlSchema ("Test/System.Data/schemas/test017.xsd");
3376 Assert.AreEqual (2, ds.Relations.Count, "#1");
3377 Assert.AreEqual (3, ds.Tables [0].Columns.Count, "#2");
3378 Assert.AreEqual (3, ds.Tables [1].Columns.Count, "#3");
3379 Assert.AreEqual ("table1_Id_0", ds.Tables [0].Columns [2].ColumnName, "#4");
3380 Assert.AreEqual ("table1_Id_0", ds.Tables [0].PrimaryKey [0].ColumnName, "#5");
3384 public void ReadXmlSchema_TableOrder ()
3386 DataSet ds = new DataSet ();
3387 ds.ReadXmlSchema ("Test/System.Data/schemas/Items.xsd");
3388 Assert.AreEqual ("category", ds.Tables [0].TableName, "#1");
3389 Assert.AreEqual ("childItemId", ds.Tables [1].TableName, "#2");
3390 Assert.AreEqual ("item", ds.Tables [2].TableName, "#3");
3394 public void ReadXml_Diffgram_MissingSchema ()
3396 DataSet ds = new DataSet ();
3397 ds.Tables.Add ("table");
3398 ds.Tables [0].Columns.Add ("col1");
3399 ds.Tables [0].Columns.Add ("col2");
3401 ds.Tables [0].Rows.Add (new object[] {"a", "b"});
3402 ds.Tables [0].Rows.Add (new object[] {"a", "b"});
3404 MemoryStream ms = new MemoryStream ();
3405 ds.WriteXml (ms, XmlWriteMode.DiffGram);
3407 DataSet ds1 = new DataSet ();
3408 ds1.Tables.Add ("table");
3409 ds1.Tables [0].Columns.Add ("col1");
3411 // When table schema is missing, it shud load up the data
3412 // for the existing schema
3413 ds1.ReadXml (new MemoryStream (ms.GetBuffer ()), XmlReadMode.DiffGram);
3415 Assert.AreEqual (2, ds1.Tables [0].Rows.Count, "#1");
3416 Assert.AreEqual (1, ds1.Tables [0].Columns.Count, "#2");
3417 Assert.AreEqual ("a", ds1.Tables [0].Rows [0][0], "#3");
3418 Assert.AreEqual ("a", ds1.Tables [0].Rows [1][0], "#4");
3422 public void WriteXml_Morethan2Relations ()
3424 DataSet ds = new DataSet ();
3425 DataTable p1 = ds.Tables.Add ("parent1");
3426 DataTable p2 = ds.Tables.Add ("parent2");
3427 DataTable p3 = ds.Tables.Add ("parent3");
3428 DataTable c1 = ds.Tables.Add ("child");
3430 c1.Columns.Add ("col1");
3431 c1.Columns.Add ("col2");
3432 c1.Columns.Add ("col3");
3433 c1.Columns.Add ("col4");
3435 p1.Columns.Add ("col1");
3436 p2.Columns.Add ("col1");
3437 p3.Columns.Add ("col1");
3439 ds.Relations.Add ("rel1", p1.Columns [0], c1.Columns [0], false);
3440 ds.Relations.Add ("rel2", p2.Columns [0], c1.Columns [1], false);
3441 ds.Relations.Add ("rel3", p3.Columns [0], c1.Columns [2], false);
3442 ds.Relations [2].Nested = true;
3444 p1.Rows.Add (new object[] {"p1"});
3445 p2.Rows.Add (new object[] {"p2"});
3446 p3.Rows.Add (new object[] {"p3"});
3448 c1.Rows.Add (new object[] {"p1","p2","p3","c1"});
3450 StringWriter sw = new StringWriter ();
3451 XmlTextWriter xw = new XmlTextWriter (sw);
3453 string dataset_xml = sw.ToString ();
3454 string child_xml = "<child><col1>p1</col1><col2>p2</col2><col3>p3</col3><col4>c1</col4></child>";
3455 //the child table data must not be repeated.
3456 Assert.AreEqual (dataset_xml.IndexOf (child_xml), dataset_xml.LastIndexOf (child_xml), "#1");
3460 public void MergeTest_ColumnTypeMismatch ()
3462 DataSet dataSet = new DataSet ();
3463 dataSet.Tables.Add (new DataTable ());
3464 dataSet.Tables [0].Columns.Add (new DataColumn ("id", typeof (int)));
3465 dataSet.Tables [0].Columns.Add (new DataColumn ("name", typeof (string)));
3467 DataSet ds = new DataSet ();
3468 ds.Tables.Add (new DataTable ());
3469 ds.Tables [0].Columns.Add (new DataColumn ("id", typeof (string)));
3472 ds.Merge (dataSet, true, MissingSchemaAction.Add);
3474 } catch (DataException e) {}
3476 ds = new DataSet ();
3477 ds.Tables.Add (new DataTable ());
3478 ds.Tables [0].Columns.Add (new DataColumn("id", typeof (string)));
3480 ds.Merge (dataSet, true, MissingSchemaAction.Ignore);
3482 Assert.AreEqual ("Table1", ds.Tables [0].TableName, "#2");
3483 Assert.AreEqual (1, ds.Tables.Count, "#3");
3484 Assert.AreEqual (1, ds.Tables [0].Columns.Count, "#4");
3485 Assert.AreEqual (typeof (string), ds.Tables [0].Columns [0].DataType, "#5");
3490 public void MergeTest_SameDataSet_536194 ()
3492 DataSet dataSet = new DataSet ("Test");
3494 DataTable dataTable = new DataTable("Test");
3495 dataTable.Columns.Add("Test");
3496 dataTable.Rows.Add("Test");
3497 dataSet.Tables.Add(dataTable);
3498 dataSet.Merge(dataTable);
3499 Assert.AreEqual (1, dataSet.Tables.Count, "1");
3505 public void LoadTest1 ()
3507 DataSet ds1 = new DataSet ();
3508 DataSet ds2 = new DataSet ();
3509 DataTable dt1 = new DataTable ("T1");
3510 DataTable dt2 = new DataTable ("T2");
3511 DataTable dt3 = new DataTable ("T1");
3512 DataTable dt4 = new DataTable ("T2");
3513 dt1.Columns.Add ("ID", typeof (int));
3514 dt1.Columns.Add ("Name", typeof (string));
3515 dt2.Columns.Add ("EmpNO", typeof (int));
3516 dt2.Columns.Add ("EmpName", typeof (string));
3518 dt1.Rows.Add (new object[] {1, "Andrews"});
3519 dt1.Rows.Add (new object[] {2, "Mathew"});
3520 dt1.Rows.Add (new object[] {3, "Jaccob"});
3522 dt2.Rows.Add (new object[] {1, "Arul"});
3523 dt2.Rows.Add (new object[] {2, "Jothi"});
3524 dt2.Rows.Add (new object[] {3, "Murugan"});
3526 ds2.Tables.Add (dt1);
3527 ds2.Tables.Add (dt2);
3528 ds1.Tables.Add (dt3);
3529 ds1.Tables.Add (dt4);
3531 DataTableReader reader = ds2.CreateDataReader ();
3532 //ds1.Load (reader, LoadOption.PreserveChanges, dt3, dt4);
3533 ds1.Load (reader, LoadOption.OverwriteChanges, dt3, dt4);
3535 Assert.AreEqual (ds2.Tables.Count, ds1.Tables.Count, "DataSet Tables count mistmatch");
3537 foreach (DataTable dt in ds1.Tables) {
3538 DataTable dt5 = ds2.Tables[i];
3539 Assert.AreEqual (dt5.Rows.Count, dt.Rows.Count, "Table " + dt.TableName + " row count mistmatch");
3542 foreach (DataRow row in dt.Rows) {
3544 for (int k = 0; k < dt.Columns.Count; k++) {
3545 Assert.AreEqual (row1[k], row[k], "DataRow " + k + " mismatch");
3553 public void LoadTest2 ()
3555 DataSet ds1 = new DataSet ();
3556 DataSet ds2 = new DataSet ();
3557 DataTable dt1 = new DataTable ("T1");
3558 DataTable dt2 = new DataTable ("T2");
3559 DataTable dt3 = new DataTable ("T1");
3560 DataTable dt4 = new DataTable ("T2");
3561 dt1.Columns.Add ("ID", typeof (int));
3562 dt1.Columns.Add ("Name", typeof (string));
3563 dt2.Columns.Add ("EmpNO", typeof (int));
3564 dt2.Columns.Add ("EmpName", typeof (string));
3566 dt1.Rows.Add (new object[] {1, "Andrews"});
3567 dt1.Rows.Add (new object[] {2, "Mathew"});
3568 dt1.Rows.Add (new object[] {3, "Jaccob"});
3570 dt2.Rows.Add (new object[] {1, "Arul"});
3571 dt2.Rows.Add (new object[] {2, "Jothi"});
3572 dt2.Rows.Add (new object[] {3, "Murugan"});
3574 ds2.Tables.Add (dt1);
3575 ds2.Tables.Add (dt2);
3576 ds1.Tables.Add (dt3);
3577 ds1.Tables.Add (dt4);
3579 DataTableReader reader = ds2.CreateDataReader ();
3580 //ds1.Load (reader, LoadOption.PreserveChanges, dt3, dt4);
3581 ds1.Load (reader, LoadOption.OverwriteChanges, dt3, dt4);
3583 Assert.AreEqual (ds2.Tables.Count, ds1.Tables.Count, "DataSet Tables count mistmatch");
3585 foreach (DataTable dt in ds1.Tables) {
3586 DataTable dt5 = ds2.Tables[i];
3587 Assert.AreEqual (dt5.Rows.Count, dt.Rows.Count, "Table " + dt.TableName + " row count mistmatch");
3590 foreach (DataRow row in dt.Rows) {
3592 for (int k = 0; k < dt.Columns.Count; k++) {
3593 Assert.AreEqual (row1[k], row[k], "DataRow " + k + " mismatch");
3601 private void AssertDataTableValues (DataTable dt)
3603 Assert.AreEqual ("data1", dt.Rows[0]["_ID"], "1");
3604 Assert.AreEqual ("data2", dt.Rows[0]["#ID"], "2");
3605 Assert.AreEqual ("data3", dt.Rows[0]["%ID"], "2");
3606 Assert.AreEqual ("data4", dt.Rows[0]["$ID"], "2");
3607 Assert.AreEqual ("data5", dt.Rows[0][":ID"], "2");
3608 Assert.AreEqual ("data6", dt.Rows[0][".ID"], "2");
3609 Assert.AreEqual ("data7", dt.Rows[0]["ID"], "2");
3610 Assert.AreEqual ("data8", dt.Rows[0]["*ID"], "2");
3611 Assert.AreEqual ("data8", dt.Rows[0]["+ID"], "2");
3612 Assert.AreEqual ("data8", dt.Rows[0]["-ID"], "2");
3613 Assert.AreEqual ("data8", dt.Rows[0]["~ID"], "2");
3614 Assert.AreEqual ("data8", dt.Rows[0]["@ID"], "2");
3615 Assert.AreEqual ("data8", dt.Rows[0]["&ID"], "2");
3620 public void Bug537229_BinFormatSerializer_Test ()
3622 DataSet ds = new DataSet ();
3623 DataTable dt = new DataTable ();
3625 dt.Columns.Add ("_ID", typeof(String));
3626 dt.Columns.Add ("#ID", typeof(String));
3627 dt.Columns.Add ("%ID", typeof(String));
3628 dt.Columns.Add ("$ID", typeof(String));
3629 dt.Columns.Add (":ID", typeof(String));
3630 dt.Columns.Add (".ID", typeof(String));
3631 dt.Columns.Add ("ID", typeof(String));
3632 dt.Columns.Add ("*ID", typeof(String));
3633 dt.Columns.Add ("+ID", typeof(String));
3634 dt.Columns.Add ("-ID", typeof(String));
3635 dt.Columns.Add ("~ID", typeof(String));
3636 dt.Columns.Add ("@ID", typeof(String));
3637 dt.Columns.Add ("&ID", typeof(String));
3638 DataRow row = dt.NewRow ();
3639 row["#ID"] = "data2";
3640 row["%ID"] = "data3";
3641 row["$ID"] = "data4";
3642 row["ID"] = "data7";
3643 row[":ID"] = "data5";
3644 row[".ID"] = "data6";
3645 row["_ID"] = "data1";
3646 row["*ID"] = "data8";
3647 row["+ID"] = "data8";
3648 row["-ID"] = "data8";
3649 row["~ID"] = "data8";
3650 row["@ID"] = "data8";
3651 row["&ID"] = "data8";
3654 AssertDataTableValues (dt);
3656 MemoryStream mstm=new MemoryStream();
3657 BinaryFormatter bfmt=new BinaryFormatter();
3658 bfmt.Serialize(mstm,dt);
3659 MemoryStream mstm2=new MemoryStream(mstm.ToArray());
3660 DataTable vdt=(DataTable)bfmt.Deserialize(mstm2);
3661 AssertDataTableValues (vdt);