{
string strConn = "server=.\\SQLEXPRESS;database=example;uid=sa;pwd=12345";
SqlConnection Conn;
SqlDataAdapter dataAdapter;
DataSet dataSet;
SqlCommandBuilder cmdBuilder;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Conn = new SqlConnection(strConn);
dataAdapter = new SqlDataAdapter("SELECT * FROM tblEx01", Conn);
dataSet = new DataSet();
cmdBuilder = new SqlCommandBuilder(dataAdapter);
dataAdapter.Fill(dataSet, "tblEx01");
dataGridView1.DataSource = dataSet.Tables["tblEx01"];
}
// 입력
private void button1_Click(object sender, EventArgs e)
{
DataRow NewRow = dataSet.Tables["tblEx01"].NewRow();
NewRow["name"] = textBox1.Text;
NewRow["age"] = textBox2.Text;
dataSet.Tables["tblEx01"].Rows.Add(NewRow);
dataAdapter.Update(dataSet, "tblEx01");
dataSet.Clear();
dataAdapter.Fill(dataSet, "tblEx01");
dataGridView1.DataSource = dataSet.Tables["tblEx01"];
}
// 수정
private void button2_Click(object sender, EventArgs e)
{
int id = (int)dataGridView1.SelectedCells[0].Value;
string strFilter = "id=" + id;
DataRow[] FindRow = dataSet.Tables["tblEx01"].Select(strFilter);
FindRow[0]["name"] = textBox1.Text;
FindRow[0]["age"] = int.Parse(textBox2.Text);
dataAdapter.Update(dataSet, "tblEX01");
dataGridView1.DataSource = dataSet.Tables["tblEx01"];
}
// 삭제
private void button3_Click(object sender, EventArgs e)
{
int id = (int)dataGridView1.CurrentRow.Cells["id"].Value;
string strFilter = "id=" + id;
DataRow[] FindRow = dataSet.Tables["tblEx01"].Select(strFilter);
FindRow[0].Delete();
dataAdapter.Update(dataSet, "tblEX01");
dataGridView1.DataSource = dataSet.Tables["tblEx01"];
}
// 새로고침
private void button4_Click(object sender, EventArgs e)
{
dataSet.Clear();
dataAdapter.Fill(dataSet, "tblEx01");
dataGridView1.DataSource = dataSet.Tables["tblEx01"];
}
}