Dataset simple demonstration

Source: Internet
Author: User
Code:
  1. Using system;
  2. Using system. IO;
  3. Using system. Collections. Generic;
  4. Using system. LINQ;
  5. Using system. Data;
  6. Using system. text;
  7. Using system. runtime. serialization. formatters. Binary;
  8. Namespace simpledataset
  9. {
  10. Class Program
  11. {
  12. Static void main (string [] ARGs)
  13. {
  14. Console. Write ("****** fun with datasets *****/N ");
  15. Dataset carsinventoryds = new dataset ("Car inventory ");
  16. Carsinventoryds. extendedproperties ["timestamp"] = datetime. now;
  17. Carsinventoryds. extendedproperties ["datasetid"] = guid. newguid ();
  18. Carsinventoryds. extendedproperties ["company"] = "Intertech training ";
  19. Datacolumn caridcolumn = new datacolumn ("carid", typeof (INT ));
  20. Caridcolumn. Caption = "Car ID ";
  21. Caridcolumn. readonly = true;
  22. Caridcolumn. allowdbnull = false;
  23. Caridcolumn. Unique = true;
  24. Caridcolumn. autoincrement = true;
  25. Caridcolumn. autoincrementseed = 0;
  26. Caridcolumn. autoincrementstep = 1;
  27. Datacolumn carmakecolumn = new datacolumn ("make", typeof (string ));
  28. Datacolumn carcolorcolumn = new datacolumn ("color", typeof (string ));
  29. Datacolumn carpetnamecolumn = new datacolumn ("petname", typeof (string ));
  30. Carpetnamecolumn. Caption = "pet name ";
  31. Datatable inventroytable = new datatable ("inventroy ");
  32. Inventroytable. Columns. addrange (New datacolumn []
  33. {Caridcolumn, carmakecolumn, carcolorcolumn, carpetnamecolumn });
  34. Datarow Carrow = inventroytable. newrow ();
  35. Inventroytable. primarykey = new datacolumn [] {inventroytable. Columns [0]};
  36. Carrow ["make"] = "BMW ";
  37. Carrow ["color"] = "black ";
  38. Carrow ["petname"] = "Hamlet ";
  39. Inventroytable. Rows. Add (Carrow );
  40. Carrow = inventroytable. newrow ();
  41. Carrow [1] = "Saab ";
  42. Carrow [2] = "red ";
  43. Carrow [3] = "Sea Breeze ";
  44. Inventroytable. Rows. Add (Carrow );
  45. Manipulatedatarowstate ();
  46. Carsinventoryds. Tables. Add (inventroytable );
  47. Datacolumn customidcolumn = new datacolumn ("customid", typeof (INT ));
  48. Customidcolumn. Caption = "customid ID ";
  49. Customidcolumn. readonly = true;
  50. Customidcolumn. allowdbnull = false;
  51. Customidcolumn. Unique = true;
  52. Customidcolumn. autoincrement = true;
  53. Customidcolumn. autoincrementseed = 1987;
  54. Customidcolumn. autoincrementstep = 1;
  55. Datacolumn firstcolumn = new datacolumn ("firstname", typeof (string ));
  56. Datacolumn lastcolumn = new datacolumn ("lastname", typeof (string ));
  57. Firstcolumn. Caption = "first name ";
  58. Datatable customtable = new datatable ("Custom ");
  59. Customtable. Columns. addrange (New datacolumn [] {customidcolumn, firstcolumn, lastcolumn });
  60. Datarow customrow = customtable. newrow ();
  61. Customtable. primarykey = new datacolumn [] {customtable. Columns [0]};
  62. Customrow ["firstname"] = "Xue ";
  63. Customrow ["lastname"] = "Yong ";
  64. Customtable. Rows. Add (customrow );
  65. Customrow = customtable. newrow ();
  66. Customrow [1] = "Zhang ";
  67. Customrow [2] = "Chuang ";
  68. Customtable. Rows. Add (customrow );
  69. Manipulatedatarowstate ();
  70. Carsinventoryds. Tables. Add (customtable );
  71. Printdataset (carsinventoryds );
  72. Foreach (datatable DT in carsinventoryds. Tables)
  73. Printtable (DT );
  74. Datasetasxml (carsinventoryds );
  75. Datasetasbinary (carsinventoryds );
  76. }
  77. Static void datasetasbinary (Dataset carsinventoryds)
  78. {
  79. Carsinventoryds. remotingformat = serializationformat. Binary;
  80. Filestream FS = new filestream ("binarycars. bin", filemode. Create );
  81. Binaryformatter bformat = new binaryformatter ();
  82. Bformat. serialize (FS, carsinventoryds );
  83. FS. Close ();
  84. Carsinventoryds. Clear ();
  85. FS = new filestream ("binarycars. bin", filemode. Open );
  86. Dataset DATA = (Dataset) bformat. deserialize (FS );
  87. }
  88. Static void datasetasxml (Dataset carsinventoryds)
  89. {
  90. Carsinventoryds. writexml ("carsdataset. xml ");
  91. Carsinventoryds. writexmlschema ("carsdataset. XSD ");
  92. Carsinventoryds. Clear ();
  93. Carsinventoryds. readxml ("carsdataset. xml ");
  94. }
  95. Static void printdataset (Dataset DS)
  96. {
  97. Console. writeline ("datasset is named: {0}", DS. datasetname );
  98. Foreach (system. Collections. dictionaryentry de in DS. extendedproperties)
  99. {
  100. Console. writeline ("Key = {0}, value = {1}", De. Key, De. value );
  101. }
  102. Foreach (datatable DT in DS. Tables)
  103. {
  104. Console. writeline ("-> {0} table:", DT. tablename );
  105. For (INT curcol = 0; curcol <DT. Columns. Count; curcol ++)
  106. {
  107. Console. Write (Dt. Columns [curcol]. columnname + "/T ");
  108. }
  109. Console. writeline ("/n -------------------------------");
  110. For (INT currow = 0; currow <DT. Rows. Count; currow ++)
  111. {
  112. For (INT curcol = 0; curcol <DT. Columns. Count; curcol ++)
  113. {
  114. Console. Write (Dt. Rows [currow] [curcol]. tostring () + "/T ");
  115. }
  116. Console. writeline ();
  117. }
  118. }
  119. }
  120. Static void printtable (datatable DT)
  121. {
  122. Datatablereader dtreader = DT. createdatareader ();
  123. While (dtreader. Read ())
  124. {
  125. For (INT I = 0; I <dtreader. fieldcount; I ++)
  126. {
  127. Console. writeline ("{0}/T", dtreader. getvalue (I). tostring (). Trim ());
  128. }
  129. Console. writeline ();
  130. }
  131. Dtreader. Close ();
  132. }
  133. Private Static void manipulatedatarowstate ()
  134. {
  135. Datatable temp = new datatable ("Temp ");
  136. Temp. Columns. Add (New datacolumn ("tempcolumn", typeof (INT )));
  137. Datarow ROW = temp. newrow ();
  138. Console. writeline ("after calling newrow (): {0}", row. rowstate );
  139. Temp. Rows. Add (ROW );
  140. Console. writeline ("after calling rows. Add (): {0}", row. rowstate );
  141. Row ["tempcolumn"] = 10;
  142. Console. writeline ("after first assignment: {0}", row. rowstate );
  143. Temp. acceptchanges ();
  144. Console. writeline ("after calling acceptchanges: {0}", row. rowstate );
  145. Row ["tempcolumn"] = 11;
  146. Console. writeline ("after first modify: {0}", row. rowstate );
  147. Temp. Rows [0]. Delete ();
  148. Console. writeline ("after calling Delete: {0}", row. rowstate );
  149. }
  150. }
  151. }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.