0% found this document useful (0 votes)
8 views3 pages

24 - 윈폼 - ver4 - 소스 - 최종

pdff

Uploaded by

gim01312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

24 - 윈폼 - ver4 - 소스 - 최종

pdff

Uploaded by

gim01312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

C# 윈폼 ver4 소스 {

MessageBox.Show(DE.Message);
//ver4 }
catch (Exception DE)
===== form1 ======== {
using System; MessageBox.Show(DE.Message);
using System.Collections.Generic; }
using System.ComponentModel; }
using System.Data;
using System.Drawing; private void AppendBtn_Click(object sender, EventArgs e)
using System.Linq; {
using System.Text; try
using System.Windows.Forms; {
using Oracle.DataAccess.Client; MessageBox.Show("텍스트 상자에 모든 데이터 입력
namespace ADOForm 하셨으면 추가합니다!");
{ // DS.Clear();//*
public partial class Form1 : Form
{ // DBAdapter.Fill(DS, "Phone");//*
new Form1 Parent; //*****
dbc.PhoneTable = dbc.DS.Tables["Phone"];//*
/* 중복 되는 부분 DataRow newRow = dbc.PhoneTable.NewRow();
newRow["id"] =Convert.ToInt32(txtid.Text);
//수정하거나 삭제하기 위해 선택된 행의 인덱스를 저장한다. newRow["PName"] = txtName.Text;
private int SelectedRowIndex; newRow["Phone"] = txtPhone.Text;
newRow["Email"] = txtMail.Text;
// Data Provider인 DBAdapter 입니다.
OracleDataAdapter DBAdapter; dbc.PhoneTable.Rows.Add(newRow);
dbc.DBAdapter.Update(dbc.DS, "Phone");
// DataSet 객체입니다. dbc.DS.AcceptChanges();//*
DataSet DS; ClearTextBoxes(); //*
DBGrid.DataSource =
// 추가, 수정, 삭제시에 필요한 명령문을 dbc.DS.Tables["Phone"].DefaultView;
// 자동으로 작성해주는 객체입니다. }
OracleCommandBuilder myCommandBuilder; catch (DataException DE)
{
// ataTable 객체입니다. MessageBox.Show(DE.Message);
DataTable phoneTable; }
catch (Exception DE)
// 수정, 삭제할 때 필요한 레코드의 키값을 보관할 필드 {
private int SelectedKeyValue; MessageBox.Show(DE.Message);
}
}
private void DB_Open()
{ private void dataGridView1_CellClick(object sender,
try DataGridViewCellEventArgs e)
{ {
string connectionString = "User Id=honga1; try
Password=1111; Data Source=(DESCRIPTION = (ADDRESS = {
(PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) // DataSet DS = new DataSet();//*
(CONNECT_DATA = (SERVER = DEDICATED) //DBAdapter.Fill(DS, "Phone");
(SERVICE_NAME = xe) ) );";
DataTable phoneTable = dbc.DS.Tables["Phone"];
string commandString = "select * from Phone";
if (e.RowIndex < 0)
DBAdapter = new {
OracleDataAdapter(commandString, connectionString); // DBGrid의 컬럼 헤더를 클릭하면 컬럼을 정렬하므
myCommandBuilder = new 로
OracleCommandBuilder(DBAdapter); // 아무 메시지도 띄우지 않습니다.
return;
DS = new DataSet(); }
} else if (e.RowIndex > phoneTable.Rows.Count - 1)
catch (DataException DE) {
{ MessageBox.Show("해당하는 데이터가 존재하지 않
MessageBox.Show(DE.Message); 습니다.");
} return;
} }
중복 끝 */
DataRow currRow = phoneTable.Rows[e.RowIndex];
DBClass dbc = new DBClass(); //*****DBClass 객체 생성 txtid.Text = currRow["id"].ToString();
private void ClearTextBoxes() txtName.Text = currRow["PName"].ToString();
{ txtPhone.Text = currRow["Phone"].ToString();
txtid.Clear(); txtMail.Text = currRow["EMail"].ToString();
txtName.Clear();
txtPhone.Clear(); dbc.SelectedRowIndex =
txtMail.Clear(); Convert.ToInt32(currRow["id"]);
} }
public Form1() catch (DataException DE)
{ {
InitializeComponent(); MessageBox.Show(DE.Message);
dbc.DB_ObjCreate(); //***** }
dbc.DB_Open();//***** catch (Exception DE)
} {
MessageBox.Show(DE.Message);
}
private void DAOpenBtn_Click(object sender, EventArgs e) }
{
try private void UpdateBtn_Click(object sender, EventArgs e)
{ {
dbc.DS.Clear(); try
dbc.DBAdapter.Fill(dbc.DS, "phone"); {
// phoneTable = DS.Tables["Phone"];//* //DS.Clear();
DBGrid.DataSource = //DBAdapter.Fill(DS, "Phone");
dbc.DS.Tables["phone"].DefaultView;
}
catch (DataException DE) dbc.PhoneTable = dbc.DS.Tables["Phone"];//*
DataColumn[] PrimaryKey = new DataColumn[1]; ====form2===================
PrimaryKey[0] = dbc.PhoneTable.Columns["id"]; using System;
dbc.PhoneTable.PrimaryKey = PrimaryKey; using System.Collections.Generic;
using System.ComponentModel;
DataRow currRow = using System.Data;
dbc.PhoneTable.Rows.Find(dbc.SelectedRowIndex); using System.Drawing;
using System.Linq;
currRow.BeginEdit(); using System.Text;
currRow["id"] = txtid.Text; using System.Windows.Forms;
currRow["PName"] = txtName.Text; using Oracle.DataAccess.Client;
currRow["Phone"] = txtPhone.Text; namespace ADOForm
currRow["EMail"] = txtMail.Text; {
currRow.EndEdit(); public partial class Form2 : Form
{
DataSet UpdatedSet = new Form1 Parent; //*****
dbc.DS.GetChanges(DataRowState.Modified);
if (UpdatedSet.HasErrors) /* 중복 되는 부분
{
MessageBox.Show("변경된 데이터에 문제가 있 //수정하거나 삭제하기 위해 선택된 행의 인덱스를 저장한다.
습니다."); private int SelectedRowIndex;
}
else // Data Provider인 DBAdapter 입니다.
{ OracleDataAdapter DBAdapter;
dbc.DBAdapter.Update(UpdatedSet, "Phone");
dbc.DS.AcceptChanges(); // DataSet 객체입니다.
} DataSet DS;

DBGrid.DataSource = // 추가, 수정, 삭제시에 필요한 명령문을


dbc.DS.Tables["Phone"].DefaultView; // 자동으로 작성해주는 객체입니다.
OracleCommandBuilder myCommandBuilder;
}
catch (DataException DE) // ataTable 객체입니다.
{ DataTable phoneTable;
MessageBox.Show(DE.Message);
} // 수정, 삭제할 때 필요한 레코드의 키값을 보관할 필드
catch (Exception DE) private int SelectedKeyValue;
{
MessageBox.Show(DE.Message);
} private void DB_Open()
} {
try
private void DeleteBtn_Click(object sender, EventArgs e) {
{ string connectionString = "User Id=honga1;
try Password=1111; Data Source=(DESCRIPTION = (ADDRESS =
{ (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
// DS.Clear(); (CONNECT_DATA = (SERVER = DEDICATED)
// DBAdapter.Fill(DS, "Phone"); (SERVICE_NAME = xe) ) );";

string commandString = "select * from Phone";


dbc.PhoneTable = dbc.DS.Tables["Phone"];//*
DataColumn[] PrimaryKey = new DataColumn[1]; DBAdapter = new
PrimaryKey[0] = dbc.PhoneTable.Columns["id"]; OracleDataAdapter(commandString, connectionString);
dbc.PhoneTable.PrimaryKey = PrimaryKey; myCommandBuilder = new
OracleCommandBuilder(DBAdapter);
DataRow currRow =
dbc.PhoneTable.Rows.Find(dbc.SelectedRowIndex); DS = new DataSet();
currRow.Delete(); }
catch (DataException DE)
{
dbc.DBAdapter.Update(dbc.DS.GetChanges(DataRowState.Deleted), MessageBox.Show(DE.Message);
"Phone"); }
DBGrid.DataSource = }
dbc.DS.Tables["Phone"].DefaultView; 중복 끝 */
}
catch (DataException DE) DBClass dbc = new DBClass(); //*****DBClass 객체 생성
{ private void ClearTextBoxes()
MessageBox.Show(DE.Message); {
} txtid.Clear();
catch (Exception DE) txtName.Clear();
{ txtPhone.Clear();
MessageBox.Show(DE.Message); txtMail.Clear();
} }
} public Form2()
{
private void DBGrid_CellContentClick(object sender, InitializeComponent();
DataGridViewCellEventArgs e) dbc.DB_ObjCreate(); //*****
{ dbc.DB_Open();//*****
}
}
private void UpBtn_Click(object sender, EventArgs e)
private void SearchBtn_Click(object sender, EventArgs e) {
{ if (NameList.SelectedIndex != 0)
Form2 frm2 = new Form2(); {
frm2.Owner = this; NameList.SelectedIndex = NameList.SelectedIndex - 1;
frm2.ShowDialog(); }
frm2.Dispose(); else
} {
public String TxtS MessageBox.Show("이곳은 레코드의 처음입니다.");
{ }
get { return txtSearch.Text; } }
set { txtSearch.Text = value.ToString(); }
} private void DownBtn_Click(object sender, EventArgs e)
{
} if (NameList.SelectedIndex != NameList.Items.Count - 1)
} {
NameList.SelectedIndex = NameList.SelectedIndex + 1;
} public DataTable PhoneTable { get { return phoneTable; } set {
else phoneTable = value; } }
{
MessageBox.Show("이곳은 레코드의 마지막입니다."); public void DB_Open()
} { try
} {
string connectionString = "User Id=hong1;
private void NameList_SelectedIndexChanged(object Password=1111; Data Source=(DESCRIPTION = (ADDRESS =
sender, EventArgs e) (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
{ (CONNECT_DATA = (SERVER = DEDICATED)
//DS.Clear(); (SERVICE_NAME = xe) ) );";
//DBAdapter.Fill(DS, "Phone");
Parent = (Form1)Owner; string commandString = "select * from Phone";
dbc.PhoneTable = dbc.DS.Tables["Phone"];
DBAdapter = new
DataRow[] ResultRows = OracleDataAdapter(commandString, connectionString);
dbc.PhoneTable.Select("PName like '%" + Parent.TxtS + "%'"); MyCommandBuilder = new
OracleCommandBuilder(DBAdapter);
DataColumn[] PrimaryKey = new DataColumn[1];
PrimaryKey[0] = dbc.PhoneTable.Columns["id"]; DS = new DataSet();
dbc.PhoneTable.PrimaryKey = PrimaryKey; }
catch (DataException DE)
DataRow currRow = {
dbc.PhoneTable.Rows.Find(NameList.Text.Substring(0, 2)); MessageBox.Show(DE.Message);
}
dbc.SelectedKeyValue = }
Convert.ToInt32(currRow["id"].ToString()); public void DB_ObjCreate()
txtid.Text = currRow["id"].ToString(); {
txtName.Text = currRow["PName"].ToString(); PhoneTable = new DataTable();
txtMail.Text = currRow["Email"].ToString(); }
txtPhone.Text = currRow["Phone"].ToString(); }//DBClass 정의 끝
} }
private void Form2_Load(object sender, EventArgs e)
{
dbc.DS.Clear();
dbc.DBAdapter.Fill(dbc.DS, "Phone");
Parent = (Form1)Owner;
dbc.PhoneTable = dbc.DS.Tables["Phone"];
DataRow[] ResultRows
=dbc.PhoneTable.Select("PName like '%" + Parent.TxtS + "%'");
NameList.Items.Clear();
foreach (DataRow currRow in ResultRows)
{
NameList.Items.Add(currRow["Id"].ToString()
+ " " + currRow["PName"].ToString());
}
}
}
}

===사용자 클래스 정의 DBClass.cs ===


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Oracle.DataAccess.Client;//ADO.Net 개체 참조
using System.Data; //DataSet 개체 참조
using System.Windows.Forms; //MessageBox 개체 참조

namespace ADOForm
{
public class DBClass //DBClass 정의 시작
{
private int selectedRowIndex;//수정하거나 삭제하기 위해 선택된 행
의 인덱스를 저장한다.
private int selectedKeyValue; // 수정, 삭제할 때 필요한 레코드의 키
값을 보관할 필드

OracleDataAdapter dBAdapter; //Data Provider인 DBAdapter 입니다.


DataSet dS;// DataSet 객체입니다.
OracleCommandBuilder myCommandBuilder; // 추가, 수정, 삭제시에
필요한 명령문을 자동으로 작성해주는 객체입니다.
DataTable phoneTable;// DataTable 객체입니다.

public int SelectedRowIndex { get { return selectedRowIndex; } set


{ selectedRowIndex = value; } }
public int SelectedKeyValue { get { return selectedKeyValue; } set
{ selectedKeyValue = value; } }
public OracleDataAdapter DBAdapter { get { return dBAdapter; }
set { dBAdapter = value; } }
public DataSet DS { get { return dS; } set { dS = value; } }

public OracleCommandBuilder MyCommandBuilder { get { return


myCommandBuilder; } set { myCommandBuilder = value; } }

You might also like