Archive for 3月 1st, 2013
-
Hello, ADO.NET(Oxygene) World!
Posted on 3月 1st, 2013 by cx20
ADO.NET(Oxygene)
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 ソースコード(Oxygene + ADO.NET + OLE DB + Jet データベース)
namespace hello; interface uses System, System.Data.OleDb; 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: OleDbConnection; cmd: OleDbCommand; reader: OleDbDataReader; begin conStr := "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=hello.mdb"; sqlStr := "SELECT 'Hello, ADO.NET World!' AS Message"; con := new OleDbConnection(conStr); cmd := new OleDbCommand(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.
ソースコード(Oxygene + ADO.NET + OLE DB + ACE データベース)
namespace hello; interface uses System, System.Data.OleDb; 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: OleDbConnection; cmd: OleDbCommand; reader: OleDbDataReader; begin conStr := "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=hello.accdb;"; sqlStr := "SELECT 'Hello, ADO.NET World!' AS Message"; con := new OleDbConnection(conStr); cmd := new OleDbCommand(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.
ソースコード(Oxygene + ADO.NET + SQL Server)
namespace hello; interface uses System, System.Data.SqlClient; 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: SqlConnection; cmd: SqlCommand; reader: SqlDataReader; begin conStr := "SERVER=(local);DATABASE=master;UID=sa;PWD=P@ssW0rd"; sqlStr := "SELECT 'Hello, ADO.NET World!' AS Message"; con := new SqlConnection(conStr); cmd := new SqlCommand(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.
ソースコード(Oxygene + ADO.NET + Oracle)
namespace hello; interface uses System, System.Data.OracleClient; 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: OracleConnection; cmd: OracleCommand; reader: OracleDataReader; begin conStr := "Data Source=ORCL;User ID=scott;Password=tiger"; sqlStr := "SELECT 'Hello, ADO.NET World!' AS Message FROM DUAL"; con := new OracleConnection(conStr); cmd := new OracleCommand(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.
ソースコード(Oxygene + ADO.NET + ODBC + MySQL)
namespace hello; interface uses System, System.Data.Odbc; 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: OdbcConnection; cmd: OdbcCommand; reader: OdbcDataReader; begin conStr := "Driver={MySQL ODBC 5.1 Driver};Server=localhost;UID=root;PWD=P@ssW0rd"; sqlStr := "SELECT 'Hello, ADO.NET World!' AS Message"; con := new OdbcConnection(conStr); cmd := new OdbcCommand(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.
コンパイル方法(32bit OLE DB 使用時)
C:¥> oxygene Hello.pas ^ -type:exe -cputype:x86 ^ -ref:System.dll;System.Data.dll
コンパイル方法(64bit OLE DB 使用時)
C:¥> oxygene Hello.pas ^ -type:exe -cputype:x64 ^ -ref:System.dll;System.Data.dll
コンパイル方法(上記以外)
C:¥> oxygene Hello.pas ^ -type:exe ^ -ref:System.dll;System.Data.dll
実行結果
Message --------------------- Hello, ADO.NET World!