Archive for 9月 5th, 2012

  1. Hello, ODP.NET(C++/CLI) World!

    Posted on 9月 5th, 2012 by cx20

    ODP.NET(C++/CLI)

    ODP.NET(Oracle Data Provider for .NET)は、.NET ベースの Oracle Database 接続用 API である。ODAC(Oracle Data Access Component)と呼ばれるパッケージに含まれる。
    .NET 環境での Oracle Database 用データプロバイダとしては、マイクロソフト社が提供する「Microsoft Oracle Client」とオラクル社が提供する「ODP.NET」があるが、現在、「Microsoft Oracle Client」はマイクロソフト社自身が非推奨としており、今後は ODP.NET の使用が推奨されている。

    データプロバイダ 説明
    System.Data.OracleClient .NET Framework Data Provider for Oracle
    Oracle.DataAccess.Client Oracle Data Provider for .NET

    ソースコード(C++/CLI + ODP.NET + Oracle)

    #using <System.dll>
    #using <System.Data.dll>
    #using <Oracle.DataAccess.dll>
     
    using namespace System;
    using namespace Oracle::DataAccess::Client;
     
    int main( array<System::String^>^ args )
    {
        String^ conStr = "Data Source=ORCL;User ID=scott;Password=tiger;";
        String^ sqlStr = "SELECT 'Hello, ODP.NET World!' AS Message FROM DUAL";
     
        OracleConnection^ con = gcnew OracleConnection(conStr);
        OracleCommand^ cmd = gcnew OracleCommand(sqlStr, con);
        con->Open();
        OracleDataReader^ reader = cmd->ExecuteReader();
        while( reader->Read() )
        {
            Console::WriteLine( reader->GetName(0) );
            Console::WriteLine( "---------------------" );
            Console::WriteLine( reader[0] );
        }
        reader->Close();
        con->Close();
     
       return 0;
    }

    コンパイル方法

    C:¥> SET LIBPATH=<ORA_HOME>odp.netbin2.x;%LIBPATH%
    C:¥> cl Hello.cpp /clr

    実行結果

    MESSAGE
    ---------------------
    Hello, ODP.NET World!
  2. Hello, ADO.NET(C++/CLI) World!

    Posted on 9月 5th, 2012 by cx20

    ADO.NET(C++/CLI)

    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

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

    #using <System.dll>
    #using <System.Data.dll>
     
    using namespace System;
    using namespace System::Data::OleDb;
     
    int main( array<System::String^>^ args )
    {
        String^ conStr = "Provider=Microsoft.Jet.OLEDB.4.0;"
            + "Data Source=.\hello.mdb;";
        String^ sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
     
        OleDbConnection^ con = gcnew OleDbConnection(conStr);
        OleDbCommand^ cmd = gcnew OleDbCommand(sqlStr, con);
        con->Open();
        OleDbDataReader^ reader = cmd->ExecuteReader();
        while( reader->Read() )
        {
            Console::WriteLine( reader->GetName(0) );
            Console::WriteLine( "---------------------" );
            Console::WriteLine( reader[0] );
        }
        reader->Close();
        con->Close();
     
       return 0;
    }

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

    #using <System.dll>
    #using <System.Data.dll>
     
    using namespace System;
    using namespace System::Data::OleDb;
     
    int main( array<System::String^>^ args )
    {
        String^ conStr = "Provider=Microsoft.ACE.OLEDB.12.0;"
            + "Data Source=.\hello.accdb;";
        String^ sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
     
        OleDbConnection^ con = gcnew OleDbConnection(conStr);
        OleDbCommand^ cmd = gcnew OleDbCommand(sqlStr, con);
        con->Open();
        OleDbDataReader^ reader = cmd->ExecuteReader();
        while( reader->Read() )
        {
            Console::WriteLine( reader->GetName(0) );
            Console::WriteLine( "---------------------" );
            Console::WriteLine( reader[0] );
        }
        reader->Close();
        con->Close();
     
       return 0;
    }

    ソースコード(C++/CLI + ADO.NET + SQL Server)

    #using <System.dll>
    #using <System.Data.dll>
     
    using namespace System;
    using namespace System::Data::SqlClient;
     
    int main( array<System::String^>^ args )
    {
        String^ conStr = "SERVER=(local);"
            + "DATABASE=master;"
            + "UID=sa;"
            + "PWD=P@ssW0rd";
        String^ sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
     
        SqlConnection^ con = gcnew SqlConnection(conStr);
        SqlCommand^ cmd = gcnew SqlCommand(sqlStr, con);
        con->Open();
        SqlDataReader^ reader = cmd->ExecuteReader();
        while( reader->Read() )
        {
            Console::WriteLine( reader->GetName(0) );
            Console::WriteLine( "---------------------" );
            Console::WriteLine( reader[0] );
        }
        reader->Close();
        con->Close();
     
       return 0;
    }

    ソースコード(C++/CLI + ADO.NET + Oracle)

    #using <System.dll>
    #using <System.Data.dll>
    #using <System.Data.OracleClient.dll>
     
    using namespace System;
    using namespace System::Data::OracleClient;
     
    int main( array<System::String^>^ args )
    {
        String^ conStr = "Data Source=ORCL;User ID=scott;Password=tiger;";
        String^ sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message FROM DUAL";
     
        OracleConnection^ con = gcnew OracleConnection(conStr);
        OracleCommand^ cmd = gcnew OracleCommand(sqlStr, con);
        con->Open();
        OracleDataReader^ reader = cmd->ExecuteReader();
        while( reader->Read() )
        {
            Console::WriteLine( reader->GetName(0) );
            Console::WriteLine( "---------------------" );
            Console::WriteLine( reader[0] );
        }
        reader->Close();
        con->Close();
     
       return 0;
    }

    ソースコード(C++/CLI + ADO.NET + ODBC + MySQL)

    #using <System.dll>
    #using <System.Data.dll>
     
    using namespace System;
    using namespace System::Data::Odbc;
     
    int main( array<System::String^>^ args )
    {
        String^ conStr = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;UID=root;PWD=P@ssW0rd";
        String^ sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
     
        OdbcConnection^ con = gcnew OdbcConnection(conStr);
        OdbcCommand^ cmd = gcnew OdbcCommand(sqlStr, con);
        con->Open();
        OdbcDataReader^ reader = cmd->ExecuteReader();
        while( reader->Read() )
        {
            Console::WriteLine( reader->GetName(0) );
            Console::WriteLine( "---------------------" );
            Console::WriteLine( reader[0] );
        }
        reader->Close();
        con->Close();
     
       return 0;
    }

    コンパイル方法

    C:¥> cl Hello.cpp /clr

    実行結果

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