Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data;
- class Program {
- static void Main(string[] args) {
- var notes = NoteTaker.Load("testing");
- var newRow = notes.NewNote();
- newRow.SetField("Title", "Another Test");
- newRow.SetField("Contents", "Can I add rows to an existing table?");
- newRow.SetField("CreatedDate", DateTime.Now);
- newRow.SetField("LastModifiedDate", DateTime.Now);
- newRow.AcceptChanges();
- notes.Save("testing");
- }
- }
- public class NoteTaker : IDisposable {
- private const string NAMESPACE = "test";
- private DataSet _set;
- private DataTable _notesTable;
- private NoteTaker() {
- _set = new DataSet("NoteTakerDataSet");
- _notesTable = _set.Tables.Add("Notes", NAMESPACE);
- var idColumn = _notesTable.Columns.Add("Id", typeof(int), null);
- idColumn.AutoIncrement = true;
- idColumn.AutoIncrementSeed = 1;
- idColumn.AutoIncrementStep = 1;
- idColumn.ReadOnly = true;
- idColumn.Unique = true;
- _notesTable.PrimaryKey = new[] { idColumn };
- var titleColumn = _notesTable.Columns.Add("Title", typeof(string), null);
- var contentsColumn = _notesTable.Columns.Add("Contents", typeof(string), null);
- var createdDateColumn = _notesTable.Columns.Add("CreatedDate", typeof(DateTime), null);
- var lastModifiedDateColumn = _notesTable.Columns.Add("LastModifiedDate", typeof(DateTime), null);
- }
- private NoteTaker(string schemaPath, string dataPath) {
- _set = new DataSet();
- _set.ReadXmlSchema(schemaPath);
- _set.ReadXml(dataPath);
- _notesTable = _set.Tables["Notes"];
- }
- private NoteTaker(string databaseName)
- : this($"{databaseName}.xsd", $"{databaseName}.xml") {
- }
- public static NoteTaker CreateNew() {
- return new NoteTaker();
- }
- public static NoteTaker Load(string databaseName) {
- return new NoteTaker(databaseName);
- }
- public DataRow NewNote() {
- var row = _notesTable.NewRow();
- _notesTable.Rows.Add(row);
- return row;
- }
- public void Save(string databaseName) {
- Save($"{databaseName}.xsd", $"{databaseName}.xml");
- }
- public void Dispose() {
- _set.Dispose();
- }
- private void Save(string schemaPath, string dataPath) {
- _set.WriteXmlSchema(schemaPath);
- _set.WriteXml(dataPath);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement