Hello, ADO.NET World!
Posted on 3月 24th, 2012 by cx20
ADO.NET
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 |
ソースコード(C# + ADO.NET + OLE DB + Jet データベース)
using System;
using System.Data.OleDb;
class Hello
{
static void Main( String[] args )
{
string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=hello.mdb";
string sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
OleDbConnection con = new OleDbConnection(conStr);
OleDbCommand cmd = new OleDbCommand(sqlStr, con);
con.Open();
OleDbDataReader reader = cmd.ExecuteReader();
while( reader.Read() )
{
Console.WriteLine( reader.GetName(0) );
Console.WriteLine( "---------------------" );
Console.WriteLine( reader[0] );
}
reader.Close();
con.Close();
}
} |
using System;
using System.Data.OleDb;
class Hello
{
static void Main( String[] args )
{
string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=hello.mdb";
string sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
OleDbConnection con = new OleDbConnection(conStr);
OleDbCommand cmd = new OleDbCommand(sqlStr, con);
con.Open();
OleDbDataReader reader = cmd.ExecuteReader();
while( reader.Read() )
{
Console.WriteLine( reader.GetName(0) );
Console.WriteLine( "---------------------" );
Console.WriteLine( reader[0] );
}
reader.Close();
con.Close();
}
}
ソースコード(C# + ADO.NET + OLE DB + ACE データベース)
using System;
using System.Data.OleDb;
class Hello
{
static void Main( String[] args )
{
string conStr = "Provider=Microsoft.ACE.OLEDB.12.0;"
+ "Data Source=.\hello.accdb";
string sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
OleDbConnection con = new OleDbConnection(conStr);
OleDbCommand cmd = new OleDbCommand(sqlStr, con);
con.Open();
OleDbDataReader reader = cmd.ExecuteReader();
while( reader.Read() )
{
Console.WriteLine( reader.GetName(0) );
Console.WriteLine( "---------------------" );
Console.WriteLine( reader[0] );
}
reader.Close();
con.Close();
}
} |
using System;
using System.Data.OleDb;
class Hello
{
static void Main( String[] args )
{
string conStr = "Provider=Microsoft.ACE.OLEDB.12.0;"
+ "Data Source=.\hello.accdb";
string sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
OleDbConnection con = new OleDbConnection(conStr);
OleDbCommand cmd = new OleDbCommand(sqlStr, con);
con.Open();
OleDbDataReader reader = cmd.ExecuteReader();
while( reader.Read() )
{
Console.WriteLine( reader.GetName(0) );
Console.WriteLine( "---------------------" );
Console.WriteLine( reader[0] );
}
reader.Close();
con.Close();
}
}
ソースコード(C# + ADO.NET + SQL Server)
using System;
using System.Data.SqlClient;
class Hello
{
static void Main( String[] args )
{
string conStr = "SERVER=(local);"
+ "DATABASE=master;"
+ "UID=sa;"
+ "PWD=P@ssW0rd";
string sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand(sqlStr, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while( reader.Read() )
{
Console.WriteLine( reader.GetName(0) );
Console.WriteLine( "---------------------" );
Console.WriteLine( reader[0] );
}
reader.Close();
con.Close();
}
} |
using System;
using System.Data.SqlClient;
class Hello
{
static void Main( String[] args )
{
string conStr = "SERVER=(local);"
+ "DATABASE=master;"
+ "UID=sa;"
+ "PWD=P@ssW0rd";
string sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand(sqlStr, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while( reader.Read() )
{
Console.WriteLine( reader.GetName(0) );
Console.WriteLine( "---------------------" );
Console.WriteLine( reader[0] );
}
reader.Close();
con.Close();
}
}
ソースコード(C# + ADO.NET + Oracle)
using System;
using System.Data.OracleClient;
class Hello
{
static void Main( String[] args )
{
string conStr = "Data Source=ORCL;User ID=scott;Password=tiger";
string sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message FROM DUAL";
OracleConnection con = new OracleConnection(conStr);
OracleCommand cmd = new OracleCommand(sqlStr, con);
con.Open();
OracleDataReader reader = cmd.ExecuteReader();
while( reader.Read() )
{
Console.WriteLine( reader.GetName(0) );
Console.WriteLine( "---------------------" );
Console.WriteLine( reader[0] );
}
reader.Close();
con.Close();
}
} |
using System;
using System.Data.OracleClient;
class Hello
{
static void Main( String[] args )
{
string conStr = "Data Source=ORCL;User ID=scott;Password=tiger";
string sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message FROM DUAL";
OracleConnection con = new OracleConnection(conStr);
OracleCommand cmd = new OracleCommand(sqlStr, con);
con.Open();
OracleDataReader reader = cmd.ExecuteReader();
while( reader.Read() )
{
Console.WriteLine( reader.GetName(0) );
Console.WriteLine( "---------------------" );
Console.WriteLine( reader[0] );
}
reader.Close();
con.Close();
}
}
ソースコード(C# + ADO.NET + ODBC + MySQL)
using System;
using System.Data.Odbc;
class Hello
{
static void Main( String[] args )
{
string conStr = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;UID=root;PWD=P@ssW0rd";
string sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
OdbcConnection con = new OdbcConnection(conStr);
OdbcCommand cmd = new OdbcCommand(sqlStr, con);
con.Open();
OdbcDataReader reader = cmd.ExecuteReader();
while( reader.Read() )
{
Console.WriteLine( reader.GetName(0) );
Console.WriteLine( "---------------------" );
Console.WriteLine( reader[0] );
}
reader.Close();
con.Close();
}
} |
using System;
using System.Data.Odbc;
class Hello
{
static void Main( String[] args )
{
string conStr = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;UID=root;PWD=P@ssW0rd";
string sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
OdbcConnection con = new OdbcConnection(conStr);
OdbcCommand cmd = new OdbcCommand(sqlStr, con);
con.Open();
OdbcDataReader reader = cmd.ExecuteReader();
while( reader.Read() )
{
Console.WriteLine( reader.GetName(0) );
Console.WriteLine( "---------------------" );
Console.WriteLine( reader[0] );
}
reader.Close();
con.Close();
}
}
コンパイル方法(32bit OLE DB 使用時)
C:¥> csc Hello.cs /platform:x86 |
C:¥> csc Hello.cs /platform:x86
コンパイル方法(上記以外)
実行結果
Message
---------------------
Hello, ADO.NET World! |
Message
---------------------
Hello, ADO.NET World!
Tags: ADO.NET
Categories: .NET, ADO.NET, library, SQL, SQL Server