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!

Tags:

Categories: .NET, ADO.NET, C++/CLI

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

WP-SpamFree by Pole Position Marketing