using System;
using System.Data;
using Oracle.DataAccess.Client;
class Example
{
OracleConnection con;
void Connect()
{
con = new OracleConnection();
con.ConnectionString = "User Id=USERID;Password=PAW;Data Source=ORCL;";
con.Open();
Console.WriteLine("Connected to Oracle");
Console.WriteLine(con.ServerVersion);
}
void Close()
{
con.Close();
con.Dispose();
}
void Reader()
{
//string strSQL = "select ename from emp";
string strSQL = "select /* コメント */ ename from emp";
OracleCommand myCmd = new OracleCommand(strSQL, con);
OracleDataReader myReader = myCmd.ExecuteReader();
Console.WriteLine("Read from OracleDataReader:");
while(myReader.Read())
{
Console.WriteLine(myReader.GetString(0));
}
myReader.Close();
myReader.Dispose();
}
//はじめての ODP.NET アプリケーション開発
void ReadDataSet()
{
string strSQL = "select ename,empno from emp";
OracleCommand myCmd = new OracleCommand(strSQL, con);
OracleDataAdapter myDa = new OracleDataAdapter(myCmd);
Console.WriteLine("Read from DataSet:");
DataSet myDs = new DataSet();
myDa.Fill(myDs, "emp");
/* ---本文のソース---
foreach(DataRow dr in myDs.Tables[0].Rows)
{
Console.WriteLine(dr[0]);
}
*/
/* ----全列の値を取得して整形して表示---*/
foreach (DataRow dr in myDs.Tables[0].Rows)
{
for (int i = 0; i<myDs.Tables[0].Columns.Count; i++)
{
Console.Write("{0,10}", dr[i]);
}
Console.Write("\n");
}
}
void delData()
{
string strSQL = "delete from emp where empno=9999";
OracleCommand myCmd = new OracleCommand(strSQL, con);
int res = myCmd.ExecuteNonQuery();
if (res >= 1)
Console.WriteLine("Delete Success");
else
Console.WriteLine("No Data to Delete");
}
void insData()
{
//string strSQL = "Insert into emp(empno,ename,deptno) values(9999,'ODP.NET',10)";
string strSQL = "insert into test values ('あ')";
OracleCommand myCmd = new OracleCommand(strSQL, con);
OracleTransaction txn = con.BeginTransaction();
try
{
int res = myCmd.ExecuteNonQuery();
if (res == 1)
{
txn.Commit();
Console.WriteLine("Insert Success and Commited.");
}
}
catch(Exception e)
{
txn.Rollback();
Console.WriteLine("No record was inserted ." + e.Message);
}
}
static void Main()
{
try
{
Console.WriteLine("Start:");
while (true )
{
Example example = new Example();
example.Connect();
example.Reader();
example.insData();
example.delData();
example.ReadDataSet();
example.Close();
Console.Write("\n");
Console.WriteLine("End!!!!");
if (Console.ReadKey(true).Key == ConsoleKey.Escape)
break;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
//Console.WriteLine(ex.Message .StartsWith ("ORA"));
Console.WriteLine("End!!!!");
if (Console.ReadKey(true).Key == ConsoleKey.Escape)
Console.WriteLine("Wait!!!!");
}
}
}
using System.Data;
using Oracle.DataAccess.Client;
class Example
{
OracleConnection con;
void Connect()
{
con = new OracleConnection();
con.ConnectionString = "User Id=USERID;Password=PAW;Data Source=ORCL;";
con.Open();
Console.WriteLine("Connected to Oracle");
Console.WriteLine(con.ServerVersion);
}
void Close()
{
con.Close();
con.Dispose();
}
void Reader()
{
//string strSQL = "select ename from emp";
string strSQL = "select /* コメント */ ename from emp";
OracleCommand myCmd = new OracleCommand(strSQL, con);
OracleDataReader myReader = myCmd.ExecuteReader();
Console.WriteLine("Read from OracleDataReader:");
while(myReader.Read())
{
Console.WriteLine(myReader.GetString(0));
}
myReader.Close();
myReader.Dispose();
}
//はじめての ODP.NET アプリケーション開発
void ReadDataSet()
{
string strSQL = "select ename,empno from emp";
OracleCommand myCmd = new OracleCommand(strSQL, con);
OracleDataAdapter myDa = new OracleDataAdapter(myCmd);
Console.WriteLine("Read from DataSet:");
DataSet myDs = new DataSet();
myDa.Fill(myDs, "emp");
/* ---本文のソース---
foreach(DataRow dr in myDs.Tables[0].Rows)
{
Console.WriteLine(dr[0]);
}
*/
/* ----全列の値を取得して整形して表示---*/
foreach (DataRow dr in myDs.Tables[0].Rows)
{
for (int i = 0; i<myDs.Tables[0].Columns.Count; i++)
{
Console.Write("{0,10}", dr[i]);
}
Console.Write("\n");
}
}
void delData()
{
string strSQL = "delete from emp where empno=9999";
OracleCommand myCmd = new OracleCommand(strSQL, con);
int res = myCmd.ExecuteNonQuery();
if (res >= 1)
Console.WriteLine("Delete Success");
else
Console.WriteLine("No Data to Delete");
}
void insData()
{
//string strSQL = "Insert into emp(empno,ename,deptno) values(9999,'ODP.NET',10)";
string strSQL = "insert into test values ('あ')";
OracleCommand myCmd = new OracleCommand(strSQL, con);
OracleTransaction txn = con.BeginTransaction();
try
{
int res = myCmd.ExecuteNonQuery();
if (res == 1)
{
txn.Commit();
Console.WriteLine("Insert Success and Commited.");
}
}
catch(Exception e)
{
txn.Rollback();
Console.WriteLine("No record was inserted ." + e.Message);
}
}
static void Main()
{
try
{
Console.WriteLine("Start:");
while (true )
{
Example example = new Example();
example.Connect();
example.Reader();
example.insData();
example.delData();
example.ReadDataSet();
example.Close();
Console.Write("\n");
Console.WriteLine("End!!!!");
if (Console.ReadKey(true).Key == ConsoleKey.Escape)
break;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
//Console.WriteLine(ex.Message .StartsWith ("ORA"));
Console.WriteLine("End!!!!");
if (Console.ReadKey(true).Key == ConsoleKey.Escape)
Console.WriteLine("Wait!!!!");
}
}
}