using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
OleDbDataAdapter DBAdapter;
OleDbCommandBuilder Builder;
DataSet DS;
private int NextKey = 0;
DataRow[] ResultRows;
public Form1()
{
InitializeComponent();
string conStr = @"provider=Microsoft.JET.OLEDB.4.0; data source=C:\Documents and Settings\Administrator\바탕 화면\MyFriends\WindowsApplication2\Customer.mdb";
string commStr = "select * from Customer";
DBAdapter = new OleDbDataAdapter(commStr, conStr);
Builder = new OleDbCommandBuilder(DBAdapter);
DS = new DataSet();
}
private void MakeSearch(int idx)
{
try
{
DS.Clear();
DBAdapter.Fill(DS, "Customer");
DataTable CustomerTable = DS.Tables["Customer"];
string searchStr = comboBox1.Text;
if (searchStr == "항목선택") searchStr = "Name";
ResultRows = CustomerTable.Select(searchStr + " like '" + txtSearch.Text + "%'", "id");
DataRow currRow = ResultRows[idx];
txtNumber.Text = currRow["id"].ToString();
txtName.Text = currRow["Name"].ToString();
txtAge.Text = currRow["Age"].ToString();
txtSex.Text = currRow["Sex"].ToString();
txtPhone.Text = currRow["Phone"].ToString();
NextKey++;
}
catch (DataException DE)
{
MessageBox.Show(DE.Message);
}
catch (Exception DE)
{
MessageBox.Show(DE.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
NextKey = 0;
MakeSearch(NextKey);
}
private void button1_Click(object sender, EventArgs e)
{
MakeSearch(NextKey);
}
private void ClearTextBoxes()
{
txtNumber.Clear();
txtName.Clear();
txtAge.Clear();
txtSex.Clear();
txtPhone.Clear();
}
private void button3_Click(object sender, EventArgs e)
{
try
{
DS.Clear();
DBAdapter.Fill(DS, "Customer");
DataTable CustomerTable = DS.Tables["Customer"];
DataRow newRow = CustomerTable.NewRow();
newRow["Name"] = txtName.Text;
newRow["Age"] = txtAge.Text;
newRow["Sex"] = txtSex.Text;
newRow["Phone"] = txtPhone.Text;
CustomerTable.Rows.Add(newRow);
DBAdapter.Update(DS, "Customer");
DS.AcceptChanges();
ClearTextBoxes();
}
catch (DataException DE)
{
MessageBox.Show(DE.Message);
}
catch (Exception DE)
{
MessageBox.Show(DE.Message);
}
}
private void button4_Click(object sender, EventArgs e)
{
try
{
DS.Clear();
DBAdapter.Fill(DS, "Customer");
DataTable CustomerTable = DS.Tables["Customer"];
DataColumn[] PrimaryKey = new DataColumn[1];
PrimaryKey[0] = CustomerTable.Columns["id"];
CustomerTable.PrimaryKey = PrimaryKey;
DataRow currRow = CustomerTable.Rows.Find(txtNumber.Text);
currRow.Delete();
DBAdapter.Update(DS.GetChanges(DataRowState.Deleted), "Customer");
MessageBox.Show(txtName.Text + "님의 레코드를 삭제하였습니다.");
ClearTextBoxes();
}
catch (DataException DE)
{
MessageBox.Show(DE.Message);
}
catch (Exception DE)
{
MessageBox.Show(DE.Message);
}
}
}
}