Archive for 2月 19th, 2013
-
Hello, ADO.NET(Phalanger) World!
Posted on 2月 19th, 2013 by cx20
ADO.NET(Phalanger)
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 ソースコード(Phalanger + ADO.NET + OLE DB + Jet データベース)
<?xml version="1.0" encoding="utf-8"?> <configuration> <phpNet> <classLibrary> <add assembly="mscorlib" /> <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <add assembly="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </classLibrary> </phpNet> </configuration>
<? use System; use SystemDataOleDb; class Hello { static function Main() { $conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=hello.mdb"; $sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"; $con = new SystemDataOleDbOleDbConnection($conStr); $cmd = new SystemDataOleDbOleDbCommand($sqlStr, $con); $con->Open(); $reader = $cmd->ExecuteReader(); while ($reader->Read()) { SystemConsole::WriteLine( $reader->GetName(0) ); SystemConsole::WriteLine( "---------------------" ); SystemConsole::WriteLine( $reader->GetValue(0) ); } $reader->Close(); $con->Close(); } } ?>
ソースコード(Phalanger + ADO.NET + OLE DB + ACE データベース)
<?xml version="1.0" encoding="utf-8"?> <configuration> <phpNet> <classLibrary> <add assembly="mscorlib" /> <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <add assembly="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </classLibrary> </phpNet> </configuration>
<? use System; use SystemDataOleDb; class Hello { static function Main() { $conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\hello.accdb"; $sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"; $con = new SystemDataOleDbOleDbConnection($conStr); $cmd = new SystemDataOleDbOleDbCommand($sqlStr, $con); $con->Open(); $reader = $cmd->ExecuteReader(); while ($reader->Read()) { SystemConsole::WriteLine( $reader->GetName(0) ); SystemConsole::WriteLine( "---------------------" ); SystemConsole::WriteLine( $reader->GetValue(0) ); } $reader->Close(); $con->Close(); } } ?>
ソースコード(Phalanger + ADO.NET + SQL Server)
<?xml version="1.0" encoding="utf-8"?> <configuration> <phpNet> <classLibrary> <add assembly="mscorlib" /> <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <add assembly="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </classLibrary> </phpNet> </configuration>
<? use System; use SystemDataSqlClient; class Hello { static function Main() { $conStr = "SERVER=(local);DATABASE=master;UID=sa;PWD=P@ssW0rd"; $sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"; $con = new SystemDataSqlClientSqlConnection($conStr); $cmd = new SystemDataSqlClientSqlCommand($sqlStr, $con); $con->Open(); $reader = $cmd->ExecuteReader(); while ($reader->Read()) { SystemConsole::WriteLine( $reader->GetName(0) ); SystemConsole::WriteLine( "---------------------" ); SystemConsole::WriteLine( $reader->GetValue(0) ); } $reader->Close(); $con->Close(); } } ?>
ソースコード(Phalanger + ADO.NET + Oracle)
<?xml version="1.0" encoding="utf-8"?> <configuration> <phpNet> <classLibrary> <add assembly="mscorlib" /> <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <add assembly="System.Data.OracleClient, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </classLibrary> </phpNet> </configuration>
<? use System; use SystemDataOracleClient; class Hello { static function Main() { $conStr = "Data Source=ORCL;User ID=scott;Password=tiger"; $sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message FROM DUAL"; $con = new SystemDataOracleClientOracleConnection($conStr); $cmd = new SystemDataOracleClientOracleCommand($sqlStr, $con); $con->Open(); $reader = $cmd->ExecuteReader(); while ($reader->Read()) { SystemConsole::WriteLine( $reader->GetName(0) ); SystemConsole::WriteLine( "---------------------" ); SystemConsole::WriteLine( $reader->GetValue(0) ); } $reader->Close(); $con->Close(); } } ?>
ソースコード(Phalanger + ADO.NET + ODBC + MySQL)
<?xml version="1.0" encoding="utf-8"?> <configuration> <phpNet> <classLibrary> <add assembly="mscorlib" /> <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <add assembly="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </classLibrary> </phpNet> </configuration>
<? use System; use System\Data\Odbc; class Hello { static function Main() { $conStr = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;UID=root;PWD=P@ssW0rd"; $sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"; $con = new System\Data\Odbc\OdbcConnection($conStr); $cmd = new System\Data\Odbc\OdbcCommand($sqlStr, $con); $con->Open(); $reader = $cmd->ExecuteReader(); while ($reader->Read()) { System\Console::WriteLine( $reader->GetName(0) ); System\Console::WriteLine( "---------------------" ); System\Console::WriteLine( $reader->GetValue(0) ); } $reader->Close(); $con->Close(); } } ?>
コンパイル方法
C:¥> phpc /pure Hello.php
実行結果
Message --------------------- Hello, ADO.NET World!