Hello, ADO.NET(IronPython) World!
Posted on 2月 11th, 2013 by cx20
ADO.NET(IronPython)
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 |
ソースコード(IronPython + ADO.NET + OLE DB + Jet データベース)
import clr
clr.AddReference("System.Data")
from System import *
from System.Data.OleDb import *
conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=hello.mdb;"
sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
con = OleDbConnection(conStr)
cmd = OleDbCommand(sqlStr, con)
con.Open()
reader = cmd.ExecuteReader()
while reader.Read():
Console.WriteLine( reader.GetName(0) )
Console.WriteLine( "---------------------" )
Console.WriteLine( reader.GetValue(0) )
reader.Close()
con.Close() |
import clr
clr.AddReference("System.Data")
from System import *
from System.Data.OleDb import *
conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=hello.mdb;"
sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
con = OleDbConnection(conStr)
cmd = OleDbCommand(sqlStr, con)
con.Open()
reader = cmd.ExecuteReader()
while reader.Read():
Console.WriteLine( reader.GetName(0) )
Console.WriteLine( "---------------------" )
Console.WriteLine( reader.GetValue(0) )
reader.Close()
con.Close()
ソースコード(IronPython + ADO.NET + OLE DB + ACE データベース)
import clr
clr.AddReference("System.Data")
from System import *
from System.Data.OleDb import *
conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=hello.accdb;"
sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
con = OleDbConnection(conStr)
cmd = OleDbCommand(sqlStr, con)
con.Open()
reader = cmd.ExecuteReader()
while reader.Read():
Console.WriteLine( reader.GetName(0) )
Console.WriteLine( "---------------------" )
Console.WriteLine( reader.GetValue(0) )
reader.Close()
con.Close() |
import clr
clr.AddReference("System.Data")
from System import *
from System.Data.OleDb import *
conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=hello.accdb;"
sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
con = OleDbConnection(conStr)
cmd = OleDbCommand(sqlStr, con)
con.Open()
reader = cmd.ExecuteReader()
while reader.Read():
Console.WriteLine( reader.GetName(0) )
Console.WriteLine( "---------------------" )
Console.WriteLine( reader.GetValue(0) )
reader.Close()
con.Close()
ソースコード(IronPython + ADO.NET + SQL Server)
import clr
clr.AddReference("System.Data")
from System import *
from System.Data.SqlClient import *
conStr = "SERVER=(local);DATABASE=master;UID=sa;PWD=P@ssW0rd;"
sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
con = SqlConnection(conStr)
cmd = SqlCommand(sqlStr, con)
con.Open()
reader = cmd.ExecuteReader()
while reader.Read():
Console.WriteLine( reader.GetName(0) )
Console.WriteLine( "---------------------" )
Console.WriteLine( reader.GetValue(0) )
reader.Close()
con.Close() |
import clr
clr.AddReference("System.Data")
from System import *
from System.Data.SqlClient import *
conStr = "SERVER=(local);DATABASE=master;UID=sa;PWD=P@ssW0rd;"
sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
con = SqlConnection(conStr)
cmd = SqlCommand(sqlStr, con)
con.Open()
reader = cmd.ExecuteReader()
while reader.Read():
Console.WriteLine( reader.GetName(0) )
Console.WriteLine( "---------------------" )
Console.WriteLine( reader.GetValue(0) )
reader.Close()
con.Close()
ソースコード(IronPython + ADO.NET + Oracle)
import clr
clr.AddReference("System.Data")
clr.AddReference("System.Data.OracleClient")
from System import *
from System.Data.OracleClient import *
conStr = "Data Source=ORCL;User ID=scott;Password=tiger"
sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message FROM DUAL"
con = OracleConnection(conStr)
cmd = OracleCommand(sqlStr, con)
con.Open()
reader = cmd.ExecuteReader()
while reader.Read():
Console.WriteLine( reader.GetName(0) )
Console.WriteLine( "---------------------" )
Console.WriteLine( reader.GetValue(0) )
reader.Close()
con.Close() |
import clr
clr.AddReference("System.Data")
clr.AddReference("System.Data.OracleClient")
from System import *
from System.Data.OracleClient import *
conStr = "Data Source=ORCL;User ID=scott;Password=tiger"
sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message FROM DUAL"
con = OracleConnection(conStr)
cmd = OracleCommand(sqlStr, con)
con.Open()
reader = cmd.ExecuteReader()
while reader.Read():
Console.WriteLine( reader.GetName(0) )
Console.WriteLine( "---------------------" )
Console.WriteLine( reader.GetValue(0) )
reader.Close()
con.Close()
ソースコード(IronPython + ADO.NET + ODBC + MySQL)
import clr
clr.AddReference("System.Data")
from System import *
from System.Data.Odbc import *
conStr = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;UID=root;PWD=P@ssW0rd"
sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
con = OdbcConnection(conStr)
cmd = OdbcCommand(sqlStr, con)
con.Open()
reader = cmd.ExecuteReader()
while reader.Read():
Console.WriteLine( reader.GetName(0) )
Console.WriteLine( "---------------------" )
Console.WriteLine( reader.GetValue(0) )
reader.Close()
con.Close() |
import clr
clr.AddReference("System.Data")
from System import *
from System.Data.Odbc import *
conStr = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;UID=root;PWD=P@ssW0rd"
sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
con = OdbcConnection(conStr)
cmd = OdbcCommand(sqlStr, con)
con.Open()
reader = cmd.ExecuteReader()
while reader.Read():
Console.WriteLine( reader.GetName(0) )
Console.WriteLine( "---------------------" )
Console.WriteLine( reader.GetValue(0) )
reader.Close()
con.Close()
実行方法(64bit OLE DB 使用時)
実行方法(上記以外)
実行結果
Message
---------------------
Hello, ADO.NET World! |
Message
---------------------
Hello, ADO.NET World!
Tags: ADO.NET
Categories: .NET, ADO.NET, IronPython