[닷넷4.0, ADO.NET]OleDB Oracle을 이용한 grid select, insert예제
OleDB 공급자를 이용하여 오라클 데이터베이스에 접속했으며 EMP 테이블의 데이터를 읽어 그리드에 채우는 간단한 예제다.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
OleDbConnection conn = null;
OleDbDataAdapter adapter = null;
DataSet ds = null;
public Form1()
{
InitializeComponent();
}
{
public partial class Form1 : Form
{
OleDbConnection conn = null;
OleDbDataAdapter adapter = null;
DataSet ds = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
ds = new DataSet("emp");
{
try
{
ds = new DataSet("emp");
//아래 onj는 $ORACLE_HOME/network/admin에 있는 tnsnames.ora 파일에 정의된 이름!
string conStr = "Provider=MSDAORA;data source=onj;User ID=scott;Password=tiger";
conn = new OleDbConnection(conStr);
conn.Open();
adapter = new OleDbDataAdapter("select * from emp", conn);
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "emp Table Loading Error");
}
finally {
conn.Close();
}
}
}
}
new라는 클래스 Constructor 키워드를 사용하여, .NET Framework에서 제공하는 클래스 중의 'OleDbConnection'의 인스턴스를 만들면서 string타입의 파라미터(Parameter)를 취다. 그 stirng(접속 문자열)은 바로 윗줄에서 선언 되고 값이 주어졌다. 마지막에는 인스턴스인 Connection의 원래클래스, 즉 OleDbConnection이 멤버로 포함하고 있는 메쏘드 중의 대표적인 Open()을 호출하여 DataBase와 연결을 설정하고 연결을 얻음. 그런다음 어댑터를 하나 만들어 "select" 명령을 실행 시킨 결과 DataSet으로 fill 시키고 DataGrid의 Datasource로 ds.Tables[0]을 지정했다.
string conStr = "Provider=MSDAORA;data source=onj;User ID=scott;Password=tiger";
conn = new OleDbConnection(conStr);
conn.Open();
adapter = new OleDbDataAdapter("select * from emp", conn);
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "emp Table Loading Error");
}
finally {
conn.Close();
}
}
}
}
new라는 클래스 Constructor 키워드를 사용하여, .NET Framework에서 제공하는 클래스 중의 'OleDbConnection'의 인스턴스를 만들면서 string타입의 파라미터(Parameter)를 취다. 그 stirng(접속 문자열)은 바로 윗줄에서 선언 되고 값이 주어졌다. 마지막에는 인스턴스인 Connection의 원래클래스, 즉 OleDbConnection이 멤버로 포함하고 있는 메쏘드 중의 대표적인 Open()을 호출하여 DataBase와 연결을 설정하고 연결을 얻음. 그런다음 어댑터를 하나 만들어 "select" 명령을 실행 시킨 결과 DataSet으로 fill 시키고 DataGrid의 Datasource로 ds.Tables[0]을 지정했다.
[간단한 입력 예제, 오라클 scott계정의 EMP Table에 한건의 데이터를 간단히 입력]
public partial class Form1 : Form
{
OleDbConnection conn = null;
OleDbDataAdapter adapter = null;
DataSet ds = null;
public Form1()
{
InitializeComponent();
}
{
OleDbConnection conn = null;
OleDbDataAdapter adapter = null;
DataSet ds = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
ds = new DataSet("emp");
//아래 onj는 $ORACLE_HOME/network/admin에 있는 tnsnames.ora 파일에 정의된 이름!
string conStr = "Provider=MSDAORA;data source=onj;User ID=scott;Password=tiger";
conn = new OleDbConnection(conStr);
conn.Open();
OleDbCommand command = new OleDbCommand("insert into emp(empno, ename) values(777, '오라클자바')", conn);
command.ExecuteNonQuery();
adapter = new OleDbDataAdapter("select * from emp where ename='오라클자바'", conn);
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "emp Table Loading Error");
}
finally
{
conn.Close();
}
{
try
{
ds = new DataSet("emp");
//아래 onj는 $ORACLE_HOME/network/admin에 있는 tnsnames.ora 파일에 정의된 이름!
string conStr = "Provider=MSDAORA;data source=onj;User ID=scott;Password=tiger";
conn = new OleDbConnection(conStr);
conn.Open();
OleDbCommand command = new OleDbCommand("insert into emp(empno, ename) values(777, '오라클자바')", conn);
command.ExecuteNonQuery();
adapter = new OleDbDataAdapter("select * from emp where ename='오라클자바'", conn);
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "emp Table Loading Error");
}
finally
{
conn.Close();
}
}
}
| 12-27 | 2303 | |||
| 12-11 | 1660 | |||
| 53 | 03-15 | 1466 | ||
| 52 | 01-31 | 1574 | ||
| 51 | 01-31 | 1941 | ||
| 50 | 01-31 | 1250 | ||
| 49 | 01-19 | 1555 | ||
| 48 | 01-11 | 1376 | ||
| 47 | 01-03 | 1926 | ||
| 46 | 12-27 | 2303 | ||
| 45 | 12-19 | 1649 | ||
| 44 | 12-14 | 1617 | ||
| 43 | 12-11 | 1660 | ||
| 42 | 12-09 | 1325 | ||
| 41 | 12-01 | 1551 | ||
| 40 | 12-01 | 1733 | ||
| 39 | 12-01 | 1193 | ||
댓글 없음:
댓글 쓰기