Oxygene for .Net Console Demonstrations

StreamWriters and StreamReaders

StreamWriters and StreamReaders make it easy to save strings in files and to retrieve them, as shown in this example. The text file ints5.txt will be saved in the same folder as the executable.

Oxygene code

namespace while_file_oxygene;

interface

uses
  System.IO;

implementation

begin
  var sw := new StreamWriter('ints5.txt');
  for  i : Integer := 1 to 5 do
    sw.WriteLine(i.ToString());
  sw.Close();
  var sr := new StreamReader('ints5.txt');
  var line := '';
  var temp, sum: Integer; 
  sum := 0;
  while (line <> nil) do
    begin
      line := sr.ReadLine();
      Console.WriteLine(line);
      Integer.TryParse(line, out temp);
      sum := sum + temp;
    end;
  sr.Close();
  writeLn('Sum: ' + sum.ToString());
  Console.ReadLine();
end.

Binary File

Using a BinaryWriter and BinaryReader enables you to save a mixture of different data types to a binary file and to read them in the order that the data items were saved.

Oxygene code

namespace random_access_dot_net_oxygene;

interface

uses
  System.IO;

type
  ConsoleApp = class
  public
    class method Main(args: array of String);
  end;

implementation

class method ConsoleApp.Main(args: array of String);
var
  myString: String := 'test';
  myChar: Char := 'a'; 
  myDouble: Double := 12.345;
  myInt: Integer := 42;
  myInt64: Int64 := 123456789;
  fs: FileStream;
  bw: BinaryWriter;
  br: BinaryReader;
	
begin
  fs := new FileStream('test.bin', FileMode.Create, FileAccess.Write);
  bw := new BinaryWriter(fs);
  bw.Write(myString);
  bw.Write(myChar);
  bw.Write(myDouble);
  bw.Write(myInt);
  bw.Write(myInt64);
  bw.Close();
  fs.Close();
  fs := new FileStream('test.bin', FileMode.Open , FileAccess.Read);
  br := new BinaryReader(fs);   
  var myString2: String := br.ReadString().ToString();
	Console.WriteLine(myString2);
  var myChar2: Char := br.ReadChar();
  Console.WriteLine(myChar2);
  var myDouble2: Double := br.ReadDouble();
  Console.WriteLine(myDouble2);
  var myInt2: Integer := br.ReadInt32();
  Console.WriteLine(myInt2);
  var myInt64_2: Int64 := br.ReadInt64();
  Console.WriteLine(myInt64_2);
  br.Close();
  fs.Close();
  Console.ReadLine();
end;

end.


Accessing Data in a Firebird Database

You can set up access to Firebird with the Firebird ADO.Net Data Provider by typing the command Install-Package FirebirdSql.Data.FirebirdClient at the NuGet Package Manager prompt. If you do not have the Package Manager among your Visual Studio tools, you can install it from the NuGet home page. View the Package Manager prompt by selecting menu item Tools > NuGet Package Manager > Package Manager Console.

We give detailed instructions in the database tutorial for getting started with Firebird and for creating and populating the CONTRIBUTIONS.FDB Firebird database. This Oxygene for .Net code accesses a copy of CONTRIBUTIONS.FDB in the root of the F drive. For simplicity, the demonstration has no error trapping.

namespace TinyFirebirdDotNetOxygene;

interface

uses
  FirebirdSql, FirebirdSql.Data, FirebirdSql.Data.FirebirdClient;

implementation

begin
  var conn: FbConnection;
  var connString, sql: String;
  var com: FbCommand;
  var dr: FbDataReader;
  connString := 'User=student;' +
                'Password=pp4s;' +
                'Database=F:\\CONTRIBUTIONS.FDB;' +
                'DataSource=localhost;' +
                'Port=3050;';
  conn := new FbConnection(connString);
  conn.Open;
  sql := 'SELECT Forename FROM Programmer ORDER BY Forename';
  com := new FbCommand(sql, conn);
  dr := com.ExecuteReader;
  while (dr.Read) do
    Console.WriteLine(dr.GetString(0));
  dr.Close;
  conn.Close;
  Console.ReadLine;
end.

Programming - a skill for life!

How to write programs in the Oxygene for .Net dialect of Pascal