Mega Code Archive

 
Categories / C# Tutorial / ADO Net
 

Create DataSet and DataTable

using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Windows.Forms; public struct Employee {     public String Name;     public DateTime Day;     public Int16 SickDay;     public Boolean Sunny;     public Int16 Salary;     public Double YearOfService; } public class MainClass {     public static void Main(string[] args)     {         DataSet TestMe = CreateDS("TestDS", "TestTable");         EnumerableRowCollection<DataRow> DataOut =            TestMe.Tables["TestTable"].AsEnumerable().Where(            ThisEntry => ThisEntry.Field<Int16>("Salary") > 9).OrderBy(            ThisEntry => ThisEntry.Field<String>("Name")).ThenBy(            ThisEntry => ThisEntry.Field<Double>("YearOfService"));         DataView ThisView = DataOut.AsDataView();     }     public static DataSet CreateDS(String DSName, String DTName)     {         Employee[] Data = {             new Employee {Name = "A", Day = new DateTime(08, 09, 01),                 SickDay = 75, Sunny = true, Salary = 10, YearOfService=30.02},             new Employee {Name = "B", Day = new DateTime(08, 09, 02),                 SickDay = 70, Sunny = false, Salary = 25, YearOfService=29.35},             new Employee {Name = "C", Day = new DateTime(08, 09, 03),                 SickDay = 89, Sunny = true, Salary = 0, YearOfService=30.15},             new Employee {Name = "D", Day = new DateTime(08, 09, 04),                 SickDay = 84, Sunny = true, Salary = 5, YearOfService=29.98},             new Employee {Name = "E", Day = new DateTime(08, 09, 05),                 SickDay = 72, Sunny = false, Salary = 30, YearOfService=29.41},             new Employee {Name = "F", Day = new DateTime(08, 09, 06),                 SickDay = 82, Sunny = false, Salary = 10, YearOfService=29.11}          };         DataTable TestTable = CreateDT(Data, DTName);         DataSet ThisDS = new DataSet(DSName);         ThisDS.Tables.Add(TestTable);         return ThisDS;     }     public static DataTable CreateDT(Employee[] Entries, String DTName)     {         DataTable DT = new DataTable(DTName);         DT.Columns.Add("Name", typeof(String));         DT.Columns.Add("Day", typeof(DateTime));         DT.Columns.Add("SickDay", typeof(Int16));         DT.Columns.Add("Sunny", typeof(Boolean));         DT.Columns.Add("Windspeed", typeof(Int16));         DT.Columns.Add("YearOfService", typeof(Double));         foreach (Employee ThisEntry in Entries)             DT.Rows.Add(ThisEntry.Name, ThisEntry.Day, ThisEntry.SickDay,                ThisEntry.Sunny, ThisEntry.Salary, ThisEntry.YearOfService);         return DT;     } }