Archive for 2月 25th, 2013

  1. Hello, ADO.NET(Boo) World!

    Posted on 2月 25th, 2013 by cx20

    ADO.NET(Boo)

    ADO.NET(ActiveX Data Objects .NET)は、ADO の後継で .NET ベースの DBMS 接続用 API である。
    .NET データプロバイダを介することで様々な DBMS への接続が可能となっている。
    .NET Framework 標準で使用できる .NET データプロバイダとしては、以下のようなプロバイダがある。

    データプロバイダ名 説明
    System.Data.SqlClient Microsoft SQL Server
    System.Data.OleDb OLE DB
    System.Data.Odbc ODBC
    System.Data.OracleClient Oracle

    ソースコード(Boo + ADO.NET + OLE DB + Jet データベース)

    import System
    import System.Data.OleDb
     
    [STAThread]
    def Main(argv as (string)):
        conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=hello.mdb;"
        sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
     
        con = OleDbConnection(conStr)
        cmd = OleDbCommand(sqlStr, con)
        con.Open()
     
        reader = cmd.ExecuteReader()
        while reader.Read():
            Console.WriteLine( reader.GetName(0) )
            Console.WriteLine( "---------------------" )
            Console.WriteLine( reader.GetValue(0) )
     
        reader.Close()
        con.Close()

    ソースコード(Boo + ADO.NET + OLE DB + ACE データベース)

    import System
    import System.Data.OleDb
     
    [STAThread]
    def Main(argv as (string)):
        conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=hello.accdb;"
        sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
     
        con = OleDbConnection(conStr)
        cmd = OleDbCommand(sqlStr, con)
        con.Open()
     
        reader = cmd.ExecuteReader()
        while reader.Read():
            Console.WriteLine( reader.GetName(0) )
            Console.WriteLine( "---------------------" )
            Console.WriteLine( reader.GetValue(0) )
     
        reader.Close()
        con.Close()

    ソースコード(Boo + ADO.NET + SQL Server)

    import System
    import System.Data.SqlClient
     
    [STAThread]
    def Main(argv as (string)):
        conStr = "SERVER=(local);DATABASE=master;UID=sa;PWD=P@ssW0rd;"
        sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
     
        con = SqlConnection(conStr)
        cmd = SqlCommand(sqlStr, con)
        con.Open()
     
        reader = cmd.ExecuteReader()
        while reader.Read():
            Console.WriteLine( reader.GetName(0) )
            Console.WriteLine( "---------------------" )
            Console.WriteLine( reader.GetValue(0) )
     
        reader.Close()
        con.Close()

    ソースコード(Boo + ADO.NET + Oracle)

    import System
    import System.Data.OracleClient
     
    [STAThread]
    def Main(argv as (string)):
        conStr = "Data Source=ORCL;User ID=scott;Password=tiger"
        sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message FROM DUAL"
     
        con = OracleConnection(conStr)
        cmd = OracleCommand(sqlStr, con)
        con.Open()
     
        reader = cmd.ExecuteReader()
        while reader.Read():
            Console.WriteLine( reader.GetName(0) )
            Console.WriteLine( "---------------------" )
            Console.WriteLine( reader.GetValue(0) )
     
        reader.Close()
        con.Close()

    ソースコード(Boo + ADO.NET + ODBC + MySQL)

    import System
    import System.Data.Odbc
     
    [STAThread]
    def Main(argv as (string)):
        conStr = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;UID=root;PWD=P@ssW0rd"
        sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
     
        con = OdbcConnection(conStr)
        cmd = OdbcCommand(sqlStr, con)
        con.Open()
     
        reader = cmd.ExecuteReader()
        while reader.Read():
            Console.WriteLine( reader.GetName(0) )
            Console.WriteLine( "---------------------" )
            Console.WriteLine( reader.GetValue(0) )
     
        reader.Close()
        con.Close()

    コンパイル方法(32bit OLE DB 使用時)

    C:¥> booc -platform:x86 Hello.boo

    コンパイル方法(64bit OLE DB 使用時)

    C:¥> booc -platform:x64 Hello.boo

    コンパイル方法(上記以外)

    C:¥> booc Hello.boo

    実行結果

    Message
    ---------------------
    Hello, ADO.NET World!