Creating a DataSet From Scratch


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. using System;
  2. using System.Data;
  3.  
  4. namespace CrtDataSet
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. //-- Instantiate the data set and table
  11. DataSet SongDS = new DataSet();
  12. DataTable songTable = SongDS.Tables.Add();
  13.  
  14. //-- Add columns to the data table
  15. songTable.Columns.Add("ID", typeof(int));
  16. songTable.Columns.Add("Band", typeof(string));
  17. songTable.Columns.Add("Song", typeof(string));
  18.  
  19. //-- Add rows to the data table
  20. songTable.Rows.Add(1, "Breaking Benjamin", "Diary of Jane");
  21. songTable.Rows.Add(2, "Three Days Grace", "Pain");
  22. songTable.Rows.Add(3, "Seether", "Fake It");
  23. songTable.Rows.Add(4, "Finger Eleven", "Paralyzer");
  24. songTable.Rows.Add(5, "Three Doors Down", "Citizen Soldier");
  25.  
  26. //-- Cycle thru the data table printing the values to the screen
  27. foreach (DataTable table in SongDS.Tables)
  28. {
  29. foreach (DataRow row in table.Rows)
  30. {
  31. Console.WriteLine(row["Band"] + " ~ " + row["Song"]);
  32. }
  33. }
  34. }
  35. }
  36. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.