public partial class Form1 : Form
{
string strConn = "server=.\\SQLEXPRESS;database=example;uid=sa;pwd=12345";
SqlConnection Conn;
SqlDataAdapter dataAdapter;
DataSet dataSet;
public Form1()
{
InitializeComponent();
}
private void SqlCommandInit()
{
string strSelectSQL = "SELECT * FROM tblEx01";
dataAdapter.SelectCommand = new SqlCommand(strSelectSQL, Conn);
string strInsertCommand = "INSERT INTO tblEx01 (name, age) VALUES (@name, @age)";
dataAdapter.InsertCommand = new SqlCommand(strInsertCommand, Conn);
dataAdapter.InsertCommand.Parameters.Add("@name", SqlDbType.NVarChar, 15, "name");
dataAdapter.InsertCommand.Parameters.Add("@age", SqlDbType.Int, 0, "age");
string strDeleteCommand = "DELETE FROM tblEx01 WHERE id = @id";
dataAdapter.DeleteCommand = new SqlCommand(strDeleteCommand, Conn);
dataAdapter.DeleteCommand.Parameters.Add("@id", SqlDbType.Int, 0, "id");
string strUpdateCommand = "UPDATE tblEx01 SET name = @name, age = @age WHERE id = @id";
dataAdapter.UpdateCommand = new SqlCommand(strUpdateCommand, Conn);
dataAdapter.UpdateCommand.Parameters.Add("@name", SqlDbType.NVarChar, 20, "name");
dataAdapter.UpdateCommand.Parameters.Add("@age", SqlDbType.Int, 0, "age");
dataAdapter.UpdateCommand.Parameters.Add("@id", SqlDbType.Int, 0, "id");
}
private void Form1_Load(object sender, EventArgs e)
{
Conn = new SqlConnection(strConn);
dataAdapter = new SqlDataAdapter();
dataSet = new DataSet();
SqlCommandInit();
dataAdapter.Fill(dataSet, "tblEx01");
dataGridView1.DataSource = dataSet.Tables["tblEx01"];
}
// 입력
private void button1_Click(object sender, EventArgs e)
{
dataSet.Clear();
dataAdapter.Fill(dataSet, "tblEx01");
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;
dataSet.Clear();
dataAdapter.Fill(dataSet, "tblEx01");
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");
dataSet.Clear();
dataAdapter.Fill(dataSet, "tblEx01");
dataGridView1.DataSource = dataSet.Tables["tblEx01"];
}
// 삭제
private void button3_Click(object sender, EventArgs e)
{
int id = (int)dataGridView1.CurrentRow.Cells["id"].Value;
dataSet.Clear();
dataAdapter.Fill(dataSet, "tblEx01");
string strFilter = "id=" + id;
DataRow[] FindRow = dataSet.Tables["tblEx01"].Select(strFilter);
FindRow[0].Delete();
dataAdapter.Update(dataSet, "tblEX01");
dataSet.Clear();
dataAdapter.Fill(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"];
}
}
{
string strConn = "server=.\\SQLEXPRESS;database=example;uid=sa;pwd=12345";
SqlConnection Conn;
SqlDataAdapter dataAdapter;
DataSet dataSet;
public Form1()
{
InitializeComponent();
}
private void SqlCommandInit()
{
string strSelectSQL = "SELECT * FROM tblEx01";
dataAdapter.SelectCommand = new SqlCommand(strSelectSQL, Conn);
string strInsertCommand = "INSERT INTO tblEx01 (name, age) VALUES (@name, @age)";
dataAdapter.InsertCommand = new SqlCommand(strInsertCommand, Conn);
dataAdapter.InsertCommand.Parameters.Add("@name", SqlDbType.NVarChar, 15, "name");
dataAdapter.InsertCommand.Parameters.Add("@age", SqlDbType.Int, 0, "age");
string strDeleteCommand = "DELETE FROM tblEx01 WHERE id = @id";
dataAdapter.DeleteCommand = new SqlCommand(strDeleteCommand, Conn);
dataAdapter.DeleteCommand.Parameters.Add("@id", SqlDbType.Int, 0, "id");
string strUpdateCommand = "UPDATE tblEx01 SET name = @name, age = @age WHERE id = @id";
dataAdapter.UpdateCommand = new SqlCommand(strUpdateCommand, Conn);
dataAdapter.UpdateCommand.Parameters.Add("@name", SqlDbType.NVarChar, 20, "name");
dataAdapter.UpdateCommand.Parameters.Add("@age", SqlDbType.Int, 0, "age");
dataAdapter.UpdateCommand.Parameters.Add("@id", SqlDbType.Int, 0, "id");
}
private void Form1_Load(object sender, EventArgs e)
{
Conn = new SqlConnection(strConn);
dataAdapter = new SqlDataAdapter();
dataSet = new DataSet();
SqlCommandInit();
dataAdapter.Fill(dataSet, "tblEx01");
dataGridView1.DataSource = dataSet.Tables["tblEx01"];
}
// 입력
private void button1_Click(object sender, EventArgs e)
{
dataSet.Clear();
dataAdapter.Fill(dataSet, "tblEx01");
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;
dataSet.Clear();
dataAdapter.Fill(dataSet, "tblEx01");
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");
dataSet.Clear();
dataAdapter.Fill(dataSet, "tblEx01");
dataGridView1.DataSource = dataSet.Tables["tblEx01"];
}
// 삭제
private void button3_Click(object sender, EventArgs e)
{
int id = (int)dataGridView1.CurrentRow.Cells["id"].Value;
dataSet.Clear();
dataAdapter.Fill(dataSet, "tblEx01");
string strFilter = "id=" + id;
DataRow[] FindRow = dataSet.Tables["tblEx01"].Select(strFilter);
FindRow[0].Delete();
dataAdapter.Update(dataSet, "tblEX01");
dataSet.Clear();
dataAdapter.Fill(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"];
}
}