Hello, ADO.NET(F#) World!
Posted on 9月 8th, 2012 by cx20
ADO.NET(F#)
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 |
ソースコード(F# + ADO.NET + OLE DB + Jet データベース)
open System;
open System.Data.OleDb;
let conStr = "Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=.hello.mdb;"
let con = new OleDbConnection(conStr)
let sqlStr = "SELECT 'Hello, ADO.NET World' AS Message"
let cmd = new OleDbCommand(sqlStr, con)
con.Open()
let reader = cmd.ExecuteReader()
while reader.Read() do
printfn "%s" (reader.GetName(0))
printfn "---------------------"
printfn "%s" (reader.GetString(0))
reader.Close()
con.Close() |
open System;
open System.Data.OleDb;
let conStr = "Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=.hello.mdb;"
let con = new OleDbConnection(conStr)
let sqlStr = "SELECT 'Hello, ADO.NET World' AS Message"
let cmd = new OleDbCommand(sqlStr, con)
con.Open()
let reader = cmd.ExecuteReader()
while reader.Read() do
printfn "%s" (reader.GetName(0))
printfn "---------------------"
printfn "%s" (reader.GetString(0))
reader.Close()
con.Close()
ソースコード(F# + ADO.NET + OLE DB + ACE データベース)
open System;
open System.Data.OleDb;
let conStr = "Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=.hello.accdb;"
let con = new OleDbConnection(conStr)
let sqlStr = "SELECT 'Hello, ADO.NET World' AS Message"
let cmd = new OleDbCommand(sqlStr, con)
con.Open()
let reader = cmd.ExecuteReader()
while reader.Read() do
printfn "%s" (reader.GetName(0))
printfn "---------------------"
printfn "%s" (reader.GetString(0))
reader.Close()
con.Close() |
open System;
open System.Data.OleDb;
let conStr = "Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=.hello.accdb;"
let con = new OleDbConnection(conStr)
let sqlStr = "SELECT 'Hello, ADO.NET World' AS Message"
let cmd = new OleDbCommand(sqlStr, con)
con.Open()
let reader = cmd.ExecuteReader()
while reader.Read() do
printfn "%s" (reader.GetName(0))
printfn "---------------------"
printfn "%s" (reader.GetString(0))
reader.Close()
con.Close()
ソースコード(F# + ADO.NET + SQL Server)
open System;
open System.Data.SqlClient;
let conStr = "SERVER=(local);
DATABASE=master;
UID=sa;PWD=P@ssW0rd;"
let con = new SqlConnection(conStr)
let sqlStr = "SELECT 'Hello, ADO.NET World' AS Message"
let cmd = new SqlCommand(sqlStr, con)
con.Open()
let reader = cmd.ExecuteReader()
while reader.Read() do
printfn "%s" (reader.GetName(0))
printfn "---------------------"
printfn "%s" (reader.GetString(0))
reader.Close()
con.Close() |
open System;
open System.Data.SqlClient;
let conStr = "SERVER=(local);
DATABASE=master;
UID=sa;PWD=P@ssW0rd;"
let con = new SqlConnection(conStr)
let sqlStr = "SELECT 'Hello, ADO.NET World' AS Message"
let cmd = new SqlCommand(sqlStr, con)
con.Open()
let reader = cmd.ExecuteReader()
while reader.Read() do
printfn "%s" (reader.GetName(0))
printfn "---------------------"
printfn "%s" (reader.GetString(0))
reader.Close()
con.Close()
ソースコード(F# + ADO.NET + Oracle)
open System;
open System.Data.OracleClient;
let conStr = "Data Source=ORCL;User ID=scott;Password=tiger;"
let con = new OracleConnection(conStr)
let sqlStr = "SELECT 'Hello, ADO.NET World' AS Message FROM DUAL"
let cmd = new OracleCommand(sqlStr, con)
con.Open()
let reader = cmd.ExecuteReader()
while reader.Read() do
printfn "%s" (reader.GetName(0))
printfn "---------------------"
printfn "%s" (reader.GetString(0))
reader.Close()
con.Close() |
open System;
open System.Data.OracleClient;
let conStr = "Data Source=ORCL;User ID=scott;Password=tiger;"
let con = new OracleConnection(conStr)
let sqlStr = "SELECT 'Hello, ADO.NET World' AS Message FROM DUAL"
let cmd = new OracleCommand(sqlStr, con)
con.Open()
let reader = cmd.ExecuteReader()
while reader.Read() do
printfn "%s" (reader.GetName(0))
printfn "---------------------"
printfn "%s" (reader.GetString(0))
reader.Close()
con.Close()
ソースコード(F# + ADO.NET + ODBC + MySQL)
open System;
open System.Data.Odbc;
let conStr = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;UID=root;PWD=P@ssW0rd"
let con = new OdbcConnection(conStr)
let sqlStr = "SELECT 'Hello, ADO.NET World' AS Message"
let cmd = new OdbcCommand(sqlStr, con)
con.Open()
let reader = cmd.ExecuteReader()
while reader.Read() do
printfn "%s" (reader.GetName(0))
printfn "---------------------"
printfn "%s" (reader.GetString(0))
reader.Close()
con.Close() |
open System;
open System.Data.Odbc;
let conStr = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;UID=root;PWD=P@ssW0rd"
let con = new OdbcConnection(conStr)
let sqlStr = "SELECT 'Hello, ADO.NET World' AS Message"
let cmd = new OdbcCommand(sqlStr, con)
con.Open()
let reader = cmd.ExecuteReader()
while reader.Read() do
printfn "%s" (reader.GetName(0))
printfn "---------------------"
printfn "%s" (reader.GetString(0))
reader.Close()
con.Close()
コンパイル方法(32bit OLE DB 使用時)
C:¥> fsc Hello.fs --platform:x86 |
C:¥> fsc Hello.fs --platform:x86
コンパイル方法(上記以外)
実行結果
Message
---------------------
Hello, ADO.NET World! |
Message
---------------------
Hello, ADO.NET World!
Tags: ADO.NET
Categories: .NET, ADO.NET, F#