Archive for 4月 13th, 2013

  1. Hello, Connector/NET(Oxygene) World!

    Posted on 4月 13th, 2013 by cx20

    Connector/NET(Oxygene)

    Connector/NET は、.NET ベースの MySQL 接続用 API である。
    以下は Oxygene による Connector/NET ライブラリを使用した MySQL への接続例となっている。

    ソースコード(Oxygene + Connector/NET + MySQL)

    namespace hello;
     
    interface
    uses
        System,
        MySql.Data.MySqlClient;
     
    type
        Hello = class
    public
        class method Main(args: array of String): Integer;
    end;
     
    implementation
     
    class method Hello.Main(args: array of String): Integer;
    var
        conStr: String;
        sqlStr: String;
        con: MySqlConnection;
        cmd: MySqlCommand;
        reader: MySqlDataReader;
    begin
        conStr := "server=localhost;user id=root;password=P@ssW0rd";
        sqlStr := "SELECT 'Hello, Connector/NET World!' AS Message";
        con := new MySqlConnection(conStr);
        cmd := new MySqlCommand(sqlStr, con);
        con.Open();
        reader := cmd.ExecuteReader();
        while reader.Read() do begin
            System.Console.WriteLine( reader.GetName(0) );
            System.Console.WriteLine( "---------------------" );
            System.Console.WriteLine( reader[0] );
        end;
        reader.Close();
        con.Close();
    end;
     
    end.

    コンパイル方法

    C:¥> oxygene Hello.pas ^
           -type:exe -cputype:x86 ^
           -ref:System.dll;System.Data.dll;MySql.Data.dll

    実行結果

    MESSAGE
    ---------------------
    Hello, Connector/NET World!