3.2.3. Jazyk C#

Příklad 3.20. DataSet

Třída Demo demonstruje prácí s třídou DataSet

class Demo{
   //we create an instance of class DataSet named "Store" (empty data structure)
   private static DataSet ds = new DataSet("Store");

   static void Main(string[] args){
      //ConstructionDS creates 2 tables with a set of columns and relations among them
      ConstructionDS();
      //fills tables with values
      FillDS();
      //prints content table OrderDetail
      DataTableTest();
      //prints information based on earlz defined relations
      RelaceTest();
      ds.WriteXmlSchema("Schema.xsd");
      ds.WriteXml("Data.xml");
   }

   private static void ConstructionDS(){
      //tables are saved inside DataSet in one collection represented by property Tables
      ds.Tables.Add("Order");
      //tabulka bude mít dva sloupce uložené v kolekci dostupné přes vlastnost Columns
      ds.Tables["Order"].Columns.Add("Customer", Type.GetType("System.String"));
      ds.Tables["Order"].Columns.Add("OrderID", Type.GetType("System.Int32"));

      DataColumn[] keys = new DataColumn[1];
      keys[0] = ds.Tables["Order"].Columns["OrderID"];
      //tabulce prirazujeme primarni keys
      ds.Tables["Order"].PrimaryKey = keys;

      ds.Tables.Add("OrderDetail");
      ds.Tables["OrderDetail"].Columns.Add("OrderID", Type.GetType("System.Int32"));
      ds.Tables["OrderDetail"].Columns.Add("ProductID", Type.GetType("System.Int32"));
      ds.Tables["OrderDetail"].Columns.Add("NumberOfPieces", Type.GetType("System.Int32"));
      ds.Tables["OrderDetail"].Columns.Add("PricePerPiece", Type.GetType("System.Decimal"));

      DataColumn parent = ds.Tables["Order"].Columns["OrderID"];
      Datacolumn child  = ds.Tables["Order"].Columns["OrderID"];

      //creates the relation between tables
      ds.Relations.Add(new DataRelation("OrderDetaily", parent, child);
   }

   //fills the tables with values
   private static void FillDS(){
      DataRow row;
      //creates new row in a table
      row = ds.Tables["Order"].NewRow();

      //filling the table with values
      row["Customer"] = "Dalibor";
      row["OrderID"] = 1;
      //adding row to table
      ds.Tables["Order"].Rows.Add(row);

      row["Customer"] = "Michal";
      row["OrderID"] = 2;
      ds.Tables["Order"].Rows.Add(row);
      row = ds.Tables["OrderDetail"].NewRow();

      row["OrderID"] = 1;
      row["ProductID"] = 1001;
      row["NumberOfPieces"] = 2;
      row["PricePerPiece"] = 20.20d;
      ds.Tables["OrderDetail"].Rows.Add(row);

      row["OrderID"] = 2;
      row["ProductID"] = 1003;
      row["NumberOfPieces"] = 5;
      row["PricePerPiece"] = 13.10d;
      ds.Tables["OrderDetail"].Rows.Add(row);
   }

   //prints content of the table OrderDetail
   private static void DataTableTest(){
      foreach(DataColumn column in ds.Tables["OrderDetail"].Columns){
         Console.WriteLine("{0,-13}", column.ColumnName);
      }
      Console.WrteLine();
      foreach(DataRow row in ds.Tables["OrderDetail"].Rows){
         foreach(DataColumn column in ds.Tables["OrderDetail"].Columns){
            Console.WriteLine("{0,-13}", row[column]);
         }
         Console.WriteLine();
      }
   }
 
   //walks through all orders and prints appropriate details
   private static void RelaceTest(){
      foreach(DataRow row in ds.Tables["Order"].Rows){
         Console.WriteLine("Order:    {0}", row["OrderID"]);
         Console.WriteLine("Customer: {0}", row["Customer"]);
         Console.WriteLine("---Order details---");
         DataRow[] detail = row.GetChildRows("OrderDetail");
         foreach(DataRow item in detail){
            Console.WriteLine("Product ID:       {0}", item["ProductID"]);
            Console.WriteLine("Number of pieces: {0}", item["NumberOfPieces"]);
            Console.WriteLine("Price per piece:  {0}", item["PricePerPiece"]);
         }
         Console.WriteLine("=======================");
      }
   }

}

Příklad 3.21. Práce v síti

using System;
using System.IO;
using System.Net;
using System.Text;

class Example
{ 
  static void Main(string[] args)
  {
    WebRequest req = WebRequest.Create("http://www.google.com");
    WebResponse res = req.GetResponse();

    //ASCI->Unicode
    Stream s = res.GetResponseStream();
    StreamReader sr = new StreamReader(s, Encoding.ASCII);
    string document = sr.ReadToEnd();

    Console.WriteLine(document);
  }
 }
}

Příklad 3.22. Vlákna

using System;
using System.Threading;

class ThreadTest
{
  static void Main(){
    Thread t = new Thread(new ThreadStart(Run));
    t.Start();
    Run();
   }
  static void Run()
  {
    for(char c='a'; c<'z'; c++)
      Console.Write(c);
  }
}

Příklad 3.23. Reflexe

using System;
using System.Reflection;

class Test 
{
  static void Main()
  {
    object o = new Object();
    Information(o.GetType());
    Information(typeof(int));
    Information(Type.GetType("System.String"));
  }
  static void Information(Type t)
  {
    Console.WriteLine("Type: {0}", t);
    MemberInfo[] miarr = t.GetMembers();
    foreach(MemberInfo mi in miarr)
      Console.WriteLine(" {0}={1}", mi.MemberType, mi);
  }
}