Hello, ADO.NET(ClojureCLR) World!
Posted on 2月 17th, 2013 by cx20
ADO.NET(ClojureCLR)
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 |
ソースコード(ClojureCLR + ADO.NET + OLE DB + Jet データベース)
(System.Reflection.Assembly/LoadWithPartialName "System.Data")
(import '(System.Data.OleDb OleDbConnection))
(import '(System.Data.OleDb OleDbCommand))
(import '(System.Data.OleDb OleDbDataReader))
(def conStr "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=hello.mdb")
(def sqlStr "SELECT 'Hello, ADO.NET World!' AS Message")
(def con (OleDbConnection. conStr))
(def cmd (OleDbCommand. sqlStr con))
(.Open con)
(def reader (.ExecuteReader cmd))
(if (.Read reader)
(do
(System.Console/WriteLine (.GetName reader 0))
(System.Console/WriteLine "---------------------")
(System.Console/WriteLine (.GetValue reader 0))
)
)
(.Close reader)
(.Close con) |
(System.Reflection.Assembly/LoadWithPartialName "System.Data")
(import '(System.Data.OleDb OleDbConnection))
(import '(System.Data.OleDb OleDbCommand))
(import '(System.Data.OleDb OleDbDataReader))
(def conStr "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=hello.mdb")
(def sqlStr "SELECT 'Hello, ADO.NET World!' AS Message")
(def con (OleDbConnection. conStr))
(def cmd (OleDbCommand. sqlStr con))
(.Open con)
(def reader (.ExecuteReader cmd))
(if (.Read reader)
(do
(System.Console/WriteLine (.GetName reader 0))
(System.Console/WriteLine "---------------------")
(System.Console/WriteLine (.GetValue reader 0))
)
)
(.Close reader)
(.Close con)
ソースコード(ClojureCLR + ADO.NET + OLE DB + ACE データベース)
(System.Reflection.Assembly/LoadWithPartialName "System.Data")
(import '(System.Data.OleDb OleDbConnection))
(import '(System.Data.OleDb OleDbCommand))
(import '(System.Data.OleDb OleDbDataReader))
(def conStr "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=hello.accdb")
(def sqlStr "SELECT 'Hello, ADO.NET World!' AS Message")
(def con (OleDbConnection. conStr))
(def cmd (OleDbCommand. sqlStr con))
(.Open con)
(def reader (.ExecuteReader cmd))
(if (.Read reader)
(do
(System.Console/WriteLine (.GetName reader 0))
(System.Console/WriteLine "---------------------")
(System.Console/WriteLine (.GetValue reader 0))
)
)
(.Close reader)
(.Close con) |
(System.Reflection.Assembly/LoadWithPartialName "System.Data")
(import '(System.Data.OleDb OleDbConnection))
(import '(System.Data.OleDb OleDbCommand))
(import '(System.Data.OleDb OleDbDataReader))
(def conStr "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=hello.accdb")
(def sqlStr "SELECT 'Hello, ADO.NET World!' AS Message")
(def con (OleDbConnection. conStr))
(def cmd (OleDbCommand. sqlStr con))
(.Open con)
(def reader (.ExecuteReader cmd))
(if (.Read reader)
(do
(System.Console/WriteLine (.GetName reader 0))
(System.Console/WriteLine "---------------------")
(System.Console/WriteLine (.GetValue reader 0))
)
)
(.Close reader)
(.Close con)
ソースコード(ClojureCLR + ADO.NET + SQL Server)
(System.Reflection.Assembly/LoadWithPartialName "System.Data")
(import '(System.Data.SqlClient SqlConnection))
(import '(System.Data.SqlClient SqlCommand))
(import '(System.Data.SqlClient SqlDataReader))
(def conStr "SERVER=(local);DATABASE=master;UID=sa;PWD=P@ssW0rd")
(def sqlStr "SELECT 'Hello, ADO.NET World!' AS Message")
(def con (SqlConnection. conStr))
(def cmd (SqlCommand. sqlStr con))
(.Open con)
(def reader (.ExecuteReader cmd))
(if (.Read reader)
(do
(System.Console/WriteLine (.GetName reader 0))
(System.Console/WriteLine "---------------------")
(System.Console/WriteLine (.GetValue reader 0))
)
)
(.Close reader)
(.Close con) |
(System.Reflection.Assembly/LoadWithPartialName "System.Data")
(import '(System.Data.SqlClient SqlConnection))
(import '(System.Data.SqlClient SqlCommand))
(import '(System.Data.SqlClient SqlDataReader))
(def conStr "SERVER=(local);DATABASE=master;UID=sa;PWD=P@ssW0rd")
(def sqlStr "SELECT 'Hello, ADO.NET World!' AS Message")
(def con (SqlConnection. conStr))
(def cmd (SqlCommand. sqlStr con))
(.Open con)
(def reader (.ExecuteReader cmd))
(if (.Read reader)
(do
(System.Console/WriteLine (.GetName reader 0))
(System.Console/WriteLine "---------------------")
(System.Console/WriteLine (.GetValue reader 0))
)
)
(.Close reader)
(.Close con)
ソースコード(ClojureCLR + ADO.NET + Oracle)
(System.Reflection.Assembly/LoadWithPartialName "System.Data")
(System.Reflection.Assembly/LoadWithPartialName "System.Data.OracleClient")
(import '(System.Data.OracleClient OracleConnection))
(import '(System.Data.OracleClient OracleCommand))
(import '(System.Data.OracleClient OracleDataReader))
(def conStr "Data Source=ORCL;User ID=scott;Password=tiger")
(def sqlStr "SELECT 'Hello, ADO.NET World!' AS Message FROM DUAL")
(def con (OracleConnection. conStr))
(def cmd (OracleCommand. sqlStr con))
(.Open con)
(def reader (.ExecuteReader cmd))
(if (.Read reader)
(do
(System.Console/WriteLine (.GetName reader 0))
(System.Console/WriteLine "---------------------")
(System.Console/WriteLine (.GetValue reader 0))
)
)
(.Close reader)
(.Close con) |
(System.Reflection.Assembly/LoadWithPartialName "System.Data")
(System.Reflection.Assembly/LoadWithPartialName "System.Data.OracleClient")
(import '(System.Data.OracleClient OracleConnection))
(import '(System.Data.OracleClient OracleCommand))
(import '(System.Data.OracleClient OracleDataReader))
(def conStr "Data Source=ORCL;User ID=scott;Password=tiger")
(def sqlStr "SELECT 'Hello, ADO.NET World!' AS Message FROM DUAL")
(def con (OracleConnection. conStr))
(def cmd (OracleCommand. sqlStr con))
(.Open con)
(def reader (.ExecuteReader cmd))
(if (.Read reader)
(do
(System.Console/WriteLine (.GetName reader 0))
(System.Console/WriteLine "---------------------")
(System.Console/WriteLine (.GetValue reader 0))
)
)
(.Close reader)
(.Close con)
ソースコード(ClojureCLR + ADO.NET + ODBC + MySQL)
(System.Reflection.Assembly/LoadWithPartialName "System.Data")
(import '(System.Data.Odbc OdbcConnection))
(import '(System.Data.Odbc OdbcCommand))
(import '(System.Data.Odbc OdbcDataReader))
(def conStr "Driver={MySQL ODBC 5.1 Driver};Server=localhost;UID=root;PWD=P@ssW0rd")
(def sqlStr "SELECT 'Hello, ADO.NET World!' AS Message FROM DUAL")
(def con (OdbcConnection. conStr))
(def cmd (OdbcCommand. sqlStr con))
(.Open con)
(def reader (.ExecuteReader cmd))
(if (.Read reader)
(do
(System.Console/WriteLine (.GetName reader 0))
(System.Console/WriteLine "---------------------")
(System.Console/WriteLine (.GetValue reader 0))
)
)
(.Close reader)
(.Close con) |
(System.Reflection.Assembly/LoadWithPartialName "System.Data")
(import '(System.Data.Odbc OdbcConnection))
(import '(System.Data.Odbc OdbcCommand))
(import '(System.Data.Odbc OdbcDataReader))
(def conStr "Driver={MySQL ODBC 5.1 Driver};Server=localhost;UID=root;PWD=P@ssW0rd")
(def sqlStr "SELECT 'Hello, ADO.NET World!' AS Message FROM DUAL")
(def con (OdbcConnection. conStr))
(def cmd (OdbcCommand. sqlStr con))
(.Open con)
(def reader (.ExecuteReader cmd))
(if (.Read reader)
(do
(System.Console/WriteLine (.GetName reader 0))
(System.Console/WriteLine "---------------------")
(System.Console/WriteLine (.GetValue reader 0))
)
)
(.Close reader)
(.Close con)
実行方法
C:¥> Clojure.Main hello.clj |
C:¥> Clojure.Main hello.clj
実行結果
Message
---------------------
Hello, ADO.NET World! |
Message
---------------------
Hello, ADO.NET World!
Tags: ADO.NET
Categories: ADO.NET, ClojureCLR