2013년 8월 9일 금요일

[ORACLEJAVA커뮤니티 .NET교육, JAVA교육]닷넷(C#)으로 만든 주소록 예제

닷넷 주소록은 Visual Studio .Net 에서 작성하시면 되구요 ... 대충 보시면 이해가 되겠지만 ADO.Net을 사용한 예제이며 DB는 오라클을 사용 했습니다.
(주소에 대한 입력/수정/삭제 가능하구요, 검색도 되는 멋진 주소록 입니다.)

Oracle의 Scott에 아래의 주소록 테이블을 만드신 후 실습 바랍니다.
/*
*
* 실습용 Table script
create table AddrBook (
name varchar2(20) not null primary key,
sex varchar2(2) not null constraint ck_sex check (sex in ('M','F')),
addr varchar2(50),
tel varchar2(20) not null
)
*
* */

오라클자바커뮤니티에서 설립한 오엔제이프로그래밍 실무교육센터
(오라클SQL, 튜닝, 힌트,자바프레임워크, 안드로이드, 아이폰, 닷넷 실무전문 강의)  



--------------------------------------------------------------------
Visual Studio .Net을 실행 하신 후 C#, Window 응용 프로그램을 선택, 프로젝트 이름은 주소록으로 주시기 바랍니다.

AddrBook.cs와 CodeFile1.cs 두개의 파일을 만들어 주시면 됩니다. CodeFile1은 AddrBook.cs에서 DB 처리 부분을 탇게될 라이브러리 형태 입니다. AddrBook.cs 파일을 만들려면 새프로젝트가 시작된 상태에서 우측의 솔루션 탐색기에서 주소록에서 마우스 우측 버튼을 눌러 추가 --> 구성요소 추가 --> 새항목 추가 --> 윈도우 폼을 하시고 CodeFile1.cs의 경우 CodeFile 로 하자구요~

우선 아래의 AddrBook.cs를 다 입력하고 나면 저절로 디자인 화면에 아래처럼 디자인된 화면이 나타날겁니다. 물론 실제 개발하면 대부분의 GUI 프로그램 처럼 먼저 디자인을 하고 나중에 필요한 스크립트를 추가하는 형태가 되겠죠...

-------------------------
1. AddrBook.cs
-------------------------
using System;
using System.Data.OleDb;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace medical
{
///

/// CDJohap에 대한 요약 설명입니다.
///

public class AddrBook : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.ColumnHeader Gubun;
private System.Windows.Forms.ColumnHeader JohapCd;
private System.Windows.Forms.ColumnHeader JohapNm;
private System.Windows.Forms.ColumnHeader Bigo;
private System.Windows.Forms.Button btnNew;
private System.Windows.Forms.Button btnInput;
private System.Windows.Forms.Button btnUpdate;
private System.Windows.Forms.Button btnDelete;
private System.Windows.Forms.Button btnSearch;
private System.Windows.Forms.TextBox txtAddr;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.TextBox txtTel;
private System.Windows.Forms.ComboBox comSex;
private System.Windows.Forms.TextBox txtSearchName;
///

/// 필수 디자이너 변수입니다.
///

private System.ComponentModel.Container components = null;

public AddrBook()
{
//
// Windows Form 디자이너 지원에 필요합니다.
//
InitializeComponent();
//
// TODO: InitializeComponent를 호출한 다음 생성자 코드를 추가합니다.
//
}
///

/// 사용 중인 모든 리소스를 정리합니다.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///

/// 디자이너 지원에 필요한 메서드입니다.
/// 이 메서드의 내용을 코드 편집기로 수정하지 마십시오.
///

private void InitializeComponent()
{
this.Gubun = new System.Windows.Forms.ColumnHeader();
this.JohapCd = new System.Windows.Forms.ColumnHeader();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.btnSearch = new System.Windows.Forms.Button();
this.txtSearchName = new System.Windows.Forms.TextBox();
this.label6 = new System.Windows.Forms.Label();
this.txtAddr = new System.Windows.Forms.TextBox();
this.JohapNm = new System.Windows.Forms.ColumnHeader();
this.listView1 = new System.Windows.Forms.ListView();
this.Bigo = new System.Windows.Forms.ColumnHeader();
this.txtName = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.txtTel = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.comSex = new System.Windows.Forms.ComboBox();
this.btnNew = new System.Windows.Forms.Button();
this.btnInput = new System.Windows.Forms.Button();
this.btnUpdate = new System.Windows.Forms.Button();
this.btnDelete = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// Gubun
//
this.Gubun.Text = "성별";
this.Gubun.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.Gubun.Width = 100;
//
// JohapCd
//
this.JohapCd.Text = "성명";
this.JohapCd.Width = 80;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btnSearch);
this.groupBox1.Controls.Add(this.txtSearchName);
this.groupBox1.Location = new System.Drawing.Point(8, 6);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(264, 52);
this.groupBox1.TabIndex = 38;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "사용자 검색창 : 성명";
//
// btnSearch
//
this.btnSearch.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224)));
this.btnSearch.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnSearch.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnSearch.Font = new System.Drawing.Font("굴림", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(129)));
this.btnSearch.Location = new System.Drawing.Point(176, 16);
this.btnSearch.Name = "btnSearch";
this.btnSearch.Size = new System.Drawing.Size(72, 26);
this.btnSearch.TabIndex = 47;
this.btnSearch.Text = "검색";
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
//
// txtSearchName
//
this.txtSearchName.AutoSize = false;
this.txtSearchName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtSearchName.Font = new System.Drawing.Font("굴림", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(129)));
this.txtSearchName.Location = new System.Drawing.Point(17, 16);
this.txtSearchName.Name = "txtSearchName";
this.txtSearchName.Size = new System.Drawing.Size(160, 26);
this.txtSearchName.TabIndex = 47;
this.txtSearchName.Text = "";
//
// label6
//
this.label6.BackColor = System.Drawing.Color.White;
this.label6.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label6.Font = new System.Drawing.Font("굴림", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(129)));
this.label6.Location = new System.Drawing.Point(16, 130);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(550, 2);
this.label6.TabIndex = 34;
//
// txtAddr
//
this.txtAddr.AutoSize = false;
this.txtAddr.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtAddr.Font = new System.Drawing.Font("굴림", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(129)));
this.txtAddr.Location = new System.Drawing.Point(103, 94);
this.txtAddr.Name = "txtAddr";
this.txtAddr.Size = new System.Drawing.Size(160, 26);
this.txtAddr.TabIndex = 33;
this.txtAddr.Text = "";
//
// JohapNm
//
this.JohapNm.Text = "주소";
this.JohapNm.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.JohapNm.Width = 200;
//
// listView1
//
this.listView1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(225)));
this.listView1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.JohapCd,
this.Gubun,
this.JohapNm,
this.Bigo});
this.listView1.Font = new System.Drawing.Font("굴림", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(129)));
this.listView1.FullRowSelect = true;
this.listView1.GridLines = true;
this.listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
this.listView1.Location = new System.Drawing.Point(8, 144);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(568, 368);
this.listView1.TabIndex = 31;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
//
// Bigo
//
this.Bigo.Text = "전화번호";
this.Bigo.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.Bigo.Width = 175;
//
// txtName
//
this.txtName.AutoSize = false;
this.txtName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtName.Font = new System.Drawing.Font("굴림", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(129)));
this.txtName.Location = new System.Drawing.Point(103, 70);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(160, 26);
this.txtName.TabIndex = 29;
this.txtName.Text = "";
//
// label3
//
this.label3.BackColor = System.Drawing.SystemColors.Control;
this.label3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label3.Font = new System.Drawing.Font("굴림", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(129)));
this.label3.Location = new System.Drawing.Point(8, 94);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(96, 26);
this.label3.TabIndex = 28;
this.label3.Text = "주소";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this.label2.BackColor = System.Drawing.SystemColors.Control;
this.label2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label2.Font = new System.Drawing.Font("굴림", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(129)));
this.label2.Location = new System.Drawing.Point(8, 70);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(96, 26);
this.label2.TabIndex = 27;
this.label2.Text = "성명*";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label1
//
this.label1.BackColor = System.Drawing.SystemColors.Control;
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label1.Font = new System.Drawing.Font("굴림", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(129)));
this.label1.Location = new System.Drawing.Point(262, 70);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 26);
this.label1.TabIndex = 32;
this.label1.Text = "성별*";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// txtTel
//
this.txtTel.AutoSize = false;
this.txtTel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtTel.Font = new System.Drawing.Font("굴림", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(129)));
this.txtTel.Location = new System.Drawing.Point(357, 94);
this.txtTel.Name = "txtTel";
this.txtTel.Size = new System.Drawing.Size(216, 26);
this.txtTel.TabIndex = 39;
this.txtTel.Text = "";
//
// label4
//
this.label4.BackColor = System.Drawing.SystemColors.Control;
this.label4.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label4.Font = new System.Drawing.Font("굴림", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(129)));
this.label4.Location = new System.Drawing.Point(262, 94);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(96, 26);
this.label4.TabIndex = 40;
this.label4.Text = "전화번호*";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// comSex
//
this.comSex.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comSex.Font = new System.Drawing.Font("굴림", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(129)));
this.comSex.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.comSex.Location = new System.Drawing.Point(357, 72);
this.comSex.Name = "comSex";
this.comSex.Size = new System.Drawing.Size(216, 23);
this.comSex.TabIndex = 41;
//
// btnNew
//
this.btnNew.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224)));
this.btnNew.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnNew.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnNew.Font = new System.Drawing.Font("굴림", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(129)));
this.btnNew.Location = new System.Drawing.Point(288, 24);
this.btnNew.Name = "btnNew";
this.btnNew.Size = new System.Drawing.Size(72, 26);
this.btnNew.TabIndex = 43;
this.btnNew.Text = "신규";
this.btnNew.Click += new System.EventHandler(this.btnNew_Click);
//
// btnInput
//
this.btnInput.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224)));
this.btnInput.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnInput.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnInput.Font = new System.Drawing.Font("굴림", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(129)));
this.btnInput.Location = new System.Drawing.Point(359, 24);
this.btnInput.Name = "btnInput";
this.btnInput.Size = new System.Drawing.Size(72, 26);
this.btnInput.TabIndex = 44;
this.btnInput.Text = "입력";
this.btnInput.Click += new System.EventHandler(this.btnInput_Click);
//
// btnUpdate
//
this.btnUpdate.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224)));
this.btnUpdate.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnUpdate.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnUpdate.Font = new System.Drawing.Font("굴림", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(129)));
this.btnUpdate.Location = new System.Drawing.Point(430, 24);
this.btnUpdate.Name = "btnUpdate";
this.btnUpdate.Size = new System.Drawing.Size(72, 26);
this.btnUpdate.TabIndex = 45;
this.btnUpdate.Text = "수정";
this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
//
// btnDelete
//
this.btnDelete.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224)));
this.btnDelete.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnDelete.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnDelete.Font = new System.Drawing.Font("굴림", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(129)));
this.btnDelete.Location = new System.Drawing.Point(501, 24);
this.btnDelete.Name = "btnDelete";
this.btnDelete.Size = new System.Drawing.Size(72, 26);
this.btnDelete.TabIndex = 46;
this.btnDelete.Text = "삭제";
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
//
// AddrBook
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(252)), ((System.Byte)(242)), ((System.Byte)(255)));
this.ClientSize = new System.Drawing.Size(584, 516);
this.Controls.Add(this.btnDelete);
this.Controls.Add(this.btnUpdate);
this.Controls.Add(this.btnInput);
this.Controls.Add(this.btnNew);
this.Controls.Add(this.comSex);
this.Controls.Add(this.txtTel);
this.Controls.Add(this.label4);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.label6);
this.Controls.Add(this.txtAddr);
this.Controls.Add(this.listView1);
this.Controls.Add(this.txtName);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "AddrBook";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "주소록";
this.Load += new System.EventHandler(this.CDJohap_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion
static void Main()
{
Application.Run(new AddrBook());
}
private OleDbConnection LocalConn;
//**************************************************************
//
// ListView에 성명 데이터 전체를 로드하는 함수
//
//
//**************************************************************
private void DataLoad(OleDbDataReader myDataReader)
{
listView1.Items.Clear();
while(myDataReader.Read())
{
ListViewItem myitem1;
string gubun;
if (myDataReader["Sex"].ToString()=="M")
{
gubun = "남자";
}
else
{
gubun = "여자";
}

myitem1 = new ListViewItem(myDataReader["Name"].ToString());
myitem1.SubItems.Add(gubun);
//myitem1.SubItems.Add(myDataReader["Addr"].ToString());
//주소가 null 값을 리턴하면...
if(myDataReader.IsDBNull(2))
{
myitem1.SubItems.Add("");
}
else
{
myitem1.SubItems.Add(myDataReader["Addr"].ToString());
}
//Bigo 가 null 값을 리턴하면...
myitem1.SubItems.Add(myDataReader["Tel"].ToString());

listView1.Items.Add(myitem1);
}
myDataReader.Close();
}

//**************************************************************
//
// 주소록 폼이 로딩 될때 전체 데이터를 ListView에 로딩
// 콤보박스 초기화 작업
//
//**************************************************************
private void CDJohap_Load(object sender, System.EventArgs e)
{
try
{
//--------------------------------------------
LocalConn = Common_DB.DBConnection();
//--------------------------------------------
LocalConn.Open();
OleDbDataReader myReader = Common_DB.DataSelect("select * from AddrBook ",LocalConn);
DataLoad(myReader);

//Combo Box 채우기
comSex.Items.Add("남자");
comSex.Items.Add("여자");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "AddrBook Form Loading");
}
finally
{
LocalConn.Close();
}
}
//**************************************************************
//
// ListView에 마우스를 클릭 했을때 윗부분의 TextBox에 자료 로딩
// 2003.10.20
//
//**************************************************************
private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
{
try
{
txtName.Text = listView1.SelectedItems[0].SubItems[0].Text;
comSex.Text = listView1.SelectedItems[0].SubItems[1].Text;
txtAddr.Text = listView1.SelectedItems[0].SubItems[2].Text;
txtTel.Text = listView1.SelectedItems[0].SubItems[3].Text;
}
catch(Exception)
{
//MessageBox.Show(ex.Message, "listView1_SelectedIndexChanged");
}
}
//**************************************************************
//
// 신규버튼 클릭시... TextBox부분을 Clear(새로운 데이터 입력을 위한)
//
//
//**************************************************************
private void btnNew_Click(object sender, System.EventArgs e)
{
txtName.Text="";
txtAddr.Text="";
comSex.Text="남자";
txtTel.Text="";
}
//**************************************************************
//
// 입력 버튼 클릭시 DB에 Insert 하는 부분
//
//
//**************************************************************
private void btnInput_Click(object sender, System.EventArgs e)
{
if(txtName.Text==""|| txtTel.Text==""||comSex.Text=="")
{
MessageBox.Show("성명, 성별, 전화번호는 필수 입력사항 입니다.");
txtName.Focus();
return;
}

string gubun;
if (comSex.Text=="남자")
{
gubun="M";
}
else
{
gubun="F";
}
LocalConn.Open();
string myExecuteQuery = "Insert Into AddrBook (Name, Sex, Addr, Tel) values(";
myExecuteQuery += "'" + txtName.Text+"'"+",";
myExecuteQuery += "'" + gubun+"'"+",";
myExecuteQuery += "'"+ txtAddr.Text + "'" + ",";
myExecuteQuery += "'"+ txtTel.Text + "'" + ")";

if (Common_DB.DataManupulation(myExecuteQuery, LocalConn))
{
OleDbDataReader myReader = Common_DB.DataSelect("select * from AddrBook",LocalConn);
DataLoad(myReader);
MessageBox.Show("정상적으로 입력 되었습니다...");
}
LocalConn.Close();
}
//**************************************************************
//
// 수정 버튼 클릭시 DB에 Update 하는 부분
//
//
//**************************************************************
private void btnUpdate_Click(object sender, System.EventArgs e)
{
if(txtName.Text==""|| txtTel.Text==""||comSex.Text=="")
{
MessageBox.Show("성명, 성별, 전화번호는 필수 입력사항 입니다.");
txtName.Focus();
return;
}

string gubun;
if (comSex.Text=="남자")
{
gubun="M";
}
else
{
gubun="F";
}
LocalConn.Open();
string myExecuteQuery = "Update AddrBook set Name='" + txtName.Text + "'" + ",";
myExecuteQuery += " Addr = '" + txtAddr.Text + "'" + ",";
myExecuteQuery += " Sex = '" + gubun + "'" + ",";
myExecuteQuery += " Tel = '" + txtTel.Text + "'" ;
myExecuteQuery += " where Name = " + "'" + txtName.Text + "'";

if (Common_DB.DataManupulation(myExecuteQuery, LocalConn))
{
OleDbDataReader myReader = Common_DB.DataSelect("select * from AddrBook",LocalConn);
DataLoad(myReader);
MessageBox.Show(" 정상적으로 수정 되었습니다...");
}
LocalConn.Close();
}
//**************************************************************
//
// 삭제 버튼클릭시 DB의 자료를 Delete
//
//
//**************************************************************
private void btnDelete_Click(object sender, System.EventArgs e)
{
OleDbDataReader myReader=null;
if(txtName.Text==""|| txtTel.Text==""||comSex.Text=="")
{
MessageBox.Show("성명, 성별, 전화번호는 필수 입력사항 입니다.");
txtName.Focus();
return;
}
//------- 삭제 확인
if (MessageBox.Show ("정말 삭제 하시겠습니까?", "삭제확인",
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
!= DialogResult.Yes)
{
return;
}

LocalConn.Open();
string myExecuteQuery = "Delete AddrBook ";
myExecuteQuery += " where Name = " + "'" + txtName.Text + "'";

if (Common_DB.DataManupulation(myExecuteQuery, LocalConn))
{
myReader = Common_DB.DataSelect("select * from AddrBook",LocalConn);
DataLoad(myReader);
MessageBox.Show(" 정상적으로 삭제 되었습니다...");
}
LocalConn.Close();
}
//**************************************************************
//
// 검색 버튼클릭시 DB의 자료를 검색
//
//
//**************************************************************
private void btnSearch_Click(object sender, System.EventArgs e)
{
OleDbDataReader myReader=null;

LocalConn.Open();
myReader = Common_DB.DataSelect("select * from AddrBook where name like '%"+txtSearchName.Text +"%'",LocalConn);
if (myReader != null)
{
DataLoad(myReader);
myReader.Close();
}
LocalConn.Close();
}
}
}


-------------------------
2. CodeFile1.cs
-------------------------
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlTypes;
using System.Windows.Forms;
public class Common_DB
{
//-----------------------------------------------------------------------------
// DataBase Connection
//-----------------------------------------------------------------------------
public static OleDbConnection DBConnection()
{
OleDbConnection Conn;
//아래는 오라클용 접속 문자열, data source 애는 tnsnames.ora 파일에 있는 Alias명을 넣으면 됩니다.
string ConStr = ("Provider=MSDAORA;data source=\\printserver;User ID=scott;Password=tiger");
Conn = new OleDbConnection(ConStr);
return Conn;
}
//-----------------------------------------------------------------------------
// DataSelect
//-----------------------------------------------------------------------------
public static OleDbDataReader DataSelect(string sql, OleDbConnection Conn)
{
try
{
OleDbCommand myCommand = new OleDbCommand(sql, Conn);
return myCommand.ExecuteReader();
}
catch(Exception ex)
{
//Log File에 출력
MessageBox.Show(sql + "\n" + ex.Message, "DataSelect");
return null;
}
finally
{
}
}
//-----------------------------------------------------------------------------
// DataDelete, DataInsert
//-----------------------------------------------------------------------------
public static bool DataManupulation(string sql, OleDbConnection Conn)
{
try
{
OleDbCommand myCommand = new OleDbCommand(sql, Conn);
myCommand.ExecuteNonQuery();
return true;
}
catch(Exception ex)
{
//Log File에 출력
MessageBox.Show(sql + "\n" + ex.Message, "DataManupulation");
return false;
}
finally
{

}
}
/*
*
* 실습용 Table script
create table AddrBook (
name varchar2(20) not null primary key,
sex varchar2(2) not null constraint ck_sex check (sex in ('M','F')),
addr varchar2(50),
tel varchar2(20) not null
)
*
* */
}



댓글 없음:

댓글 쓰기