|
c# 과 오라클 연동 예제
c# - 비주얼 스튜디오 2005
오라클 - 오라클10g Express Edition
상품, 가격, 수량을 [입력]하면 오라클 DB에 저장이 되고
[출력]을 하면 DB에 저장된 내용이 출력되는 연동예제!!
Form1.cs - 소스
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DatabaseProject;
namespace OracleDB_Ex
{
public partial class Form1 : Form
{
ProjectDB connect = new ProjectDB();
string sql = null;
DataSet about = new DataSet();
public Form1()
{
InitializeComponent();
}
//입력
private void button1_Click(object sender, EventArgs e)
{
sql = "insert into posDB (product, price, count) values " +
"('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text+"')";
connect.Open();
connect.ExecuteDB(sql);
connect.Close();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox1.Focus();
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
//출력
private void button3_Click(object sender, EventArgs e)
{
string sql = "select product,price, count from posDB order by product";
connect.Open();
about = connect.GetDataSet(sql);
connect.Close();
dataGridView1.DataSource = about.Tables[0];
}
}
}
ProjectDB.cs - 소스
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Data;
using System.Text;
namespace DatabaseProject
{
class ProjectDB
{
OleDbConnection myConn;
string SQL = "Provider=msdaora.1;Data Source=xe;User ID=scott;Password=tiger;Persist Security Info=True;";
// xe는 오라클 버전명 (오라클 10g일경우 Ora10)
public void Open()
{
myConn = new OleDbConnection(SQL);
myConn.Open();
}
public void Close()
{
myConn.Close();
}
public void ExecuteDB(string sql)
{
OleDbCommand myCmd = new OleDbCommand(sql, myConn);
myCmd.ExecuteNonQuery();
}
public OleDbDataReader ExecuteReader(string sql)
{
OleDbCommand myCmd = new OleDbCommand(sql, myConn);
return myCmd.ExecuteReader();
}
public DataSet GetDataSet(string sql)
{
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand(sql, myConn);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
}
}
'프로그래밍 > C#' 카테고리의 다른 글
| c#과 오라클 연동 (비주얼스튜디오2005 + 오라클) (3) | 2009/02/06 |
|---|---|
| C#으로 만든 윈도우계산기 (5) | 2009/01/24 |
| C# - 간단한 덧셈프로그램 (0) | 2009/01/11 |
| C# 강제 형변환 (0) | 2009/01/10 |
| c# 폼 비활성화 (4) | 2008/11/15 |









비밀댓글 입니다
네, 출처남기고 퍼가세용..ㅎㅎ
간단하게 설명 잘해놓으셨네요~
도움되는 좋은 글 감사합니다^^
좋은 지식 얻고 갑니다.