이 프로그램은 코드만 가지고는 아무것도 구현할 수 없다. 컴퓨터의 Microsoft SQL 서버를 구동하고 서버 탐색기에서 데이터베이스를 생성 한 후 데이터베이스에 테이블을 만든다. 테이블을 만들었다면 윈폼에 바인딩 시킨 후 쿼리를 만들어 버튼에 할당해야 겨우 기능 하나가 구현된다.
※ 구현 기능
○ 라디오버튼을 체크하면 해당하는 그룹박스만 보임
○ 모든 학생 보기 버튼을 클릭하면 모든 chk가 'Y'인 데이터 출력
○ 탈퇴 학생 포함 버튼을 클릭하면 모든 데이터 출력
○ 아이디를 입력하고 아이디를 찾기 버튼을 클릭하면 일치하는 아이디가 존재할 경우 해당 데이터 출력
○ 이름을 입력하고 이름으로 찾기 버튼을 클릭하면 이름 중 일부라도 일치한다면 모두 출력
○ 아이디, 이름, 전화번호, 주소를 모두 입력하고 등록 버튼을 클릭하면 아이디가 존재하지 않을 경우 입력 값들로 데이터베이스에 새로운 데이터 등록 (chk='Y', date=GETDATE( ))
○ 아이디, 이름, 전화번호, 주소를 모두 입력하고 갱신 버튼을 클릭하면 해당 아이디가 존재할 경우 아이디의 나머지 값을 입력 값으로 변경
○ 아이디를 입력하고 탈퇴하기 버튼을 클릭하면 해당 아이디가 존재할 경우 chk가 'N'로 변경
○ 아이디를 입력하고 정보삭제 버튼을 클릭하면 해당 아이디가 존재할 경우 아이디의 데이터를 데이터베이스에서 삭제
테이블과 쿼리 정보
코드
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Database
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
gbShow.Visible = false;
gbInsert.Visible = false;
gbDelete.Visible = false;
}
private void rbShow_CheckedChanged(object sender, EventArgs e)
{
if (rbShow.Checked)
gbShow.Visible = true;
else
gbShow.Visible = false;
}
private void rbInsert_CheckedChanged(object sender, EventArgs e)
{
if (rbInsert.Checked)
gbInsert.Visible = true;
else
gbInsert.Visible = false;
}
private void rbDelete_CheckedChanged(object sender, EventArgs e)
{
if (rbDelete.Checked)
gbDelete.Visible = true;
else
gbDelete.Visible = false;
}
private void btnShowAll_Click(object sender, EventArgs e)
{
this.studentTableAdapter.Fill(this.studentDataSet.Student);
}
private void btnShowEX_Click(object sender, EventArgs e)
{
this.studentTableAdapter.FillByAll(this.studentDataSet.Student);
}
private void btnID_Click(object sender, EventArgs e)
{
string id = txtID.Text;
if (string.IsNullOrEmpty(id))
{
MessageBox.Show("아이디를 입력하세요");
return;
}
try
{
studentTableAdapter.FillByID(studentDataSet.Student, id);
int count = studentTableAdapter.GetDataByID(id).Rows.Count;
if (count <= 0)
{
throw new Exception("찾는 학생이 없습니다.");
}
}
catch (Exception ee) { MessageBox.Show(ee.Message); }
}
private void btnName_Click(object sender, EventArgs e)
{
string name = txtName.Text;
if (string.IsNullOrEmpty(name))
{
MessageBox.Show("이름을 입력하세요");
return;
}
try
{
studentTableAdapter.FillByName(studentDataSet.Student, "%" + name + "%");
int count = studentTableAdapter.GetDataByName("%" + name + "%").Rows.Count;
if (count <= 0)
{
throw new Exception("찾는 학생이 없습니다.");
}
}
catch (Exception ee) { MessageBox.Show(ee.Message); }
}
private void btnInsert_Click(object sender, EventArgs e)
{
string id = txtID.Text;
string name = txtName.Text;
string phone = txtPhone.Text;
string address = txtAddress.Text;
if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(name)
|| string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(address))
{
MessageBox.Show("빈칸이 존재합니다.");
return;
}
int count = 0;
try
{
count = studentTableAdapter.InsertQuery(id, name, phone, address);
studentTableAdapter.FillByID(studentDataSet.Student, id);
MessageBox.Show("학생을 추가했습니다.");
}
catch { MessageBox.Show("추가 실패"); }
}
private void btnUpdate_Click(object sender, EventArgs e)
{
string id = txtID.Text;
string name = txtName.Text;
string phone = txtPhone.Text;
string address = txtAddress.Text;
if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(name)
|| string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(address))
{
MessageBox.Show("빈칸이 존재합니다.");
return;
}
int count = 0;
try
{
count = studentTableAdapter.UpdateQuery(name, phone, address,id);
studentTableAdapter.FillByID(studentDataSet.Student, id);
MessageBox.Show("갱신했습니다.");
}
catch { MessageBox.Show("갱신 실패"); }
}
private void btnDelete_Click(object sender, EventArgs e)
{
string id = txtDelete.Text;
if (string.IsNullOrEmpty(id))
{
MessageBox.Show("아이디를 입력하세요");
return;
}
try
{
studentTableAdapter.DeleteQuery(id);
int count = studentTableAdapter.GetDataByID(id).Rows.Count;
if (count > 0)
throw new Exception("삭제 실패");
MessageBox.Show("삭제되었습니다.");
}
catch (Exception ee) { MessageBox.Show(ee.Message); }
this.studentTableAdapter.FillByAll(this.studentDataSet.Student);
}
private void btnExit_Click(object sender, EventArgs e)
{
string id = txtDelete.Text;
if (string.IsNullOrEmpty(id))
{
MessageBox.Show("아이디를 입력하세요");
return;
}
try
{
studentTableAdapter.ExitID(id);
int count=studentTableAdapter.GetDataByExitID(id).Rows.Count;
if (count <= 0)
throw new Exception("탈퇴 실패");
MessageBox.Show("탈퇴되었습니다.");
}
catch (Exception ee) { MessageBox.Show(ee.Message); }
this.studentTableAdapter.FillByAll(this.studentDataSet.Student);
}
}
}
'C# > 개발' 카테고리의 다른 글
[C#] 서버 - 클라이언트 1 : N 비동기 채팅 (0) | 2022.08.05 |
---|---|
[C#] 웹 브라우저 (0) | 2022.08.05 |
[C#] 지하철 최단거리 (0) | 2022.08.05 |
[C#] 숫자 야구 (0) | 2022.08.05 |
[C#] 아날로그 시계 (0) | 2022.08.05 |