Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3.  
  4. class Program {
  5. static void Main(string[] args) {
  6. var notes = NoteTaker.Load("testing");
  7.  
  8. var newRow = notes.NewNote();
  9. newRow.SetField("Title", "Another Test");
  10. newRow.SetField("Contents", "Can I add rows to an existing table?");
  11. newRow.SetField("CreatedDate", DateTime.Now);
  12. newRow.SetField("LastModifiedDate", DateTime.Now);
  13. newRow.AcceptChanges();
  14.  
  15. notes.Save("testing");
  16. }
  17. }
  18.  
  19. public class NoteTaker : IDisposable {
  20. private const string NAMESPACE = "test";
  21.  
  22. private DataSet _set;
  23. private DataTable _notesTable;
  24.  
  25. private NoteTaker() {
  26. _set = new DataSet("NoteTakerDataSet");
  27.  
  28. _notesTable = _set.Tables.Add("Notes", NAMESPACE);
  29. var idColumn = _notesTable.Columns.Add("Id", typeof(int), null);
  30. idColumn.AutoIncrement = true;
  31. idColumn.AutoIncrementSeed = 1;
  32. idColumn.AutoIncrementStep = 1;
  33. idColumn.ReadOnly = true;
  34. idColumn.Unique = true;
  35. _notesTable.PrimaryKey = new[] { idColumn };
  36.  
  37. var titleColumn = _notesTable.Columns.Add("Title", typeof(string), null);
  38. var contentsColumn = _notesTable.Columns.Add("Contents", typeof(string), null);
  39. var createdDateColumn = _notesTable.Columns.Add("CreatedDate", typeof(DateTime), null);
  40. var lastModifiedDateColumn = _notesTable.Columns.Add("LastModifiedDate", typeof(DateTime), null);
  41. }
  42.  
  43. private NoteTaker(string schemaPath, string dataPath) {
  44. _set = new DataSet();
  45. _set.ReadXmlSchema(schemaPath);
  46. _set.ReadXml(dataPath);
  47.  
  48. _notesTable = _set.Tables["Notes"];
  49. }
  50.  
  51. private NoteTaker(string databaseName)
  52. : this($"{databaseName}.xsd", $"{databaseName}.xml") {
  53. }
  54.  
  55. public static NoteTaker CreateNew() {
  56. return new NoteTaker();
  57. }
  58.  
  59. public static NoteTaker Load(string databaseName) {
  60. return new NoteTaker(databaseName);
  61. }
  62.  
  63. public DataRow NewNote() {
  64. var row = _notesTable.NewRow();
  65. _notesTable.Rows.Add(row);
  66. return row;
  67. }
  68.  
  69. public void Save(string databaseName) {
  70. Save($"{databaseName}.xsd", $"{databaseName}.xml");
  71. }
  72.  
  73. public void Dispose() {
  74. _set.Dispose();
  75. }
  76.  
  77. private void Save(string schemaPath, string dataPath) {
  78. _set.WriteXmlSchema(schemaPath);
  79. _set.WriteXml(dataPath);
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement