この資料では、ADO.NET の OLE DB マネージ プロバイダを使用して Oracle データベースにアクセスする方法について、実例を基に説明します。
必要条件
必要なハードウェア、ソフトウェア、ネットワーク インフラストラクチャ、および Service Pack は次のとおりです。 • Microsoft Windows 2000 Professional、Windows 2000 Server、Windows 2000 Advanced Server、Windows NT 4.0 Server のいずれか
• Oracle Client ツール (コンピュータにインストールされていること)
• Microsoft Visual Studio .NET
この資料は、次のトピックについて詳しい知識のあるユーザーを対象としています。 • Visual Studio .NET
• ADO.NET の基本および構文
• Oracle への接続
Oracle データベースへのアクセス手順
1. Oracle で次のステートメントを使用して、TestTable という名前のテーブルを作成します。
2. 次のステートメントを実行して、TestTable にデータを挿入します。
3. Visual Studio .NET を起動します。
4. Visual C# .NET で新しい Windows アプリケーション プロジェクトを開きます。
5. プロジェクトに System.Data 名前空間への参照が含まれていることを確認し、含まれていない場合は追加します。
6. Button コントロールを Form1 に配置し、Name プロパティを btnTest に変更します。
7. 後続のコードで System、System.Data、System.Data.OleDb の名前空間を使用して宣言を修飾しなくてもよいようにするため、以下の例のように、using ステートメントに各名前空間を定義します。
8. フォーム ビューに切り替え、[btnTest] をダブルクリックしてクリック イベント ハンドラを追加します。ハンドラに、以下のコードを追加します。
9. プロジェクトを保存します。
10. [デバッグ] メニューの [開始] をクリックして、プロジェクトを実行します。
11. [btnTest] ボタンをクリックして、データを表示します。
========================================================================================
[img]http://dl.iteye.com/upload/picture/pic/116779/0915753d-f3cc-31dd-9619-7683664ca4bc.png[/img]
必要条件
必要なハードウェア、ソフトウェア、ネットワーク インフラストラクチャ、および Service Pack は次のとおりです。 • Microsoft Windows 2000 Professional、Windows 2000 Server、Windows 2000 Advanced Server、Windows NT 4.0 Server のいずれか
• Oracle Client ツール (コンピュータにインストールされていること)
• Microsoft Visual Studio .NET
この資料は、次のトピックについて詳しい知識のあるユーザーを対象としています。 • Visual Studio .NET
• ADO.NET の基本および構文
• Oracle への接続
Oracle データベースへのアクセス手順
1. Oracle で次のステートメントを使用して、TestTable という名前のテーブルを作成します。
Create Table TestTable (c1 char(5));
2. 次のステートメントを実行して、TestTable にデータを挿入します。
Insert into TestTable c1 values('Test1');
Insert into TestTable c1 values('Test2');
Insert into TestTable c1 values('Test3');
3. Visual Studio .NET を起動します。
4. Visual C# .NET で新しい Windows アプリケーション プロジェクトを開きます。
5. プロジェクトに System.Data 名前空間への参照が含まれていることを確認し、含まれていない場合は追加します。
6. Button コントロールを Form1 に配置し、Name プロパティを btnTest に変更します。
7. 後続のコードで System、System.Data、System.Data.OleDb の名前空間を使用して宣言を修飾しなくてもよいようにするため、以下の例のように、using ステートメントに各名前空間を定義します。
using System;
using System.Data;
using System.Data.OleDb;
8. フォーム ビューに切り替え、[btnTest] をダブルクリックしてクリック イベント ハンドラを追加します。ハンドラに、以下のコードを追加します。
String sConnectionString =
"Provider=MSDAORA.1;User ID=myUID;password=myPWD;
Data Source=myOracleServer;Persist Security Info=False";
String mySelectQuery =
"SELECT * FROM TestTable where c1 LIKE ?";
OleDbConnection myConnection = new OleDbConnection(sConnectionString);
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
myCommand.Parameters.Add("@p1", OleDbType.Char, 5).Value = "Test%";
myConnection.Open();
OleDbDataReader myReader = myCommand.ExecuteReader();
int RecordCount=0;
try
{
while (myReader.Read())
{
RecordCount = RecordCount + 1;
MessageBox.Show(myReader.GetString(0).ToString());
}
if (RecordCount == 0)
{
MessageBox.Show("No data returned");
}
else
{
MessageBox.Show("Number of records returned: " + RecordCount);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
myReader.Close();
myConnection.Close();
}
9. プロジェクトを保存します。
10. [デバッグ] メニューの [開始] をクリックして、プロジェクトを実行します。
11. [btnTest] ボタンをクリックして、データを表示します。
========================================================================================
[img]http://dl.iteye.com/upload/picture/pic/116779/0915753d-f3cc-31dd-9619-7683664ca4bc.png[/img]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Collections;
using System.Configuration;
namespace ConnectionOracle
{
public partial class main : Form
{
public main()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Configuration config = ConfigurationManager.OpenExeConfiguration("");
AppSettingsSection settings = (AppSettingsSection)config.GetSection("appSettings");
String connectionString = settings.Settings["connectionString"].Value;
String sqlString = settings.Settings["sqlString"].Value;
OleDbConnection conn = new OleDbConnection(connectionString);
OleDbDataReader reader;
DataTable table = new DataTable(); ;
try
{
conn.Open();
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText =sqlString+ " and rownum<=10";
using (reader = cmd.ExecuteReader())
{
table.Load(reader);
}
reader.Close();
conn.Close();
Console.WriteLine(table.Columns);
dataGridView1.DataSource = table;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
conn.Close();
}
}
private void main_Load(object sender, EventArgs e)
{
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (exitConfirm(e))
{
toRelease();
}
}
private bool exitConfirm(FormClosingEventArgs e)
{
bool exitFlag=false;
closeConfirm cc = new closeConfirm();
DialogResult dr = cc.ShowDialog(this);
if (dr == DialogResult.Cancel)
{
e.Cancel = true;
}
else
{
if (!cc.closePass.Text.Equals("123456"))
{
e.Cancel = true;
}
else
{
exitFlag = true;
}
}
return exitFlag;
}
private void toRelease() {
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="connectionString" value="Provider=MSDAORA;Data Source=ywinweb;Persist Security Info=True;Password=btw;User ID=btw"/>
<add key="sqlString" value="select ip_address,create_dt,model from t_log where log_level=0"/>
</appSettings>
</configuration>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ConnectionOracle
{
public partial class closeConfirm : Form
{
public closeConfirm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
}
}