비연결기반의 UPDATE를 예제를 통해 알아보도록 합시다.
{
if (!Page.IsPostBack)
{
BindDropDownList();
SetInfo();
}
}
// 페이지 최초 로드시 임의로 작성한 BindDropDownList(), SetInfo() 메서드를 호출합니다.
void BindDropDownList()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT user_id FROM Member", con);
SqlDataAdapter ad = new SqlDataAdapter();
ad.SelectCommand = cmd;
DataSet ds = new DataSet();
ad.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataValueField = "user_id";
DropDownList1.DataTextField = "user_id";
DropDownList1.DataBind();
}
//user_id 열의 값을 가지는 DataSet 개체를 DropDownList1에 바인딩하게 합니다
void SetInfo()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT * FROM Member WHERE user_id = @user_id", con);
cmd.Parameters.AddWithValue("@user_id", DropDownList1.SelectedItem.Value);
SqlDataAdapter ad = new SqlDataAdapter();
ad.SelectCommand = cmd;
DataSet ds = new DataSet();
ad.Fill(ds);
DataTable table = ds.Tables[0];
DataRow row = table.Rows[0];
Label1.Text = row["user_id"].ToString();
TextBox1.Text = row["password"].ToString();
TextBox2.Text = row["name"].ToString();
TextBox3.Text = row["phone"].ToString();
}
// DropDownList1에서 선택한 user_id에 따라 해당 user_id와 관련된 아이디, 암호, 이름, 전화번호 정보를
DataSet 개체에 채우고 ds를 파싱하여 해당 열의 값을 출력합니다. TextBox1은 Password를 출력하도록 되어있네요!
{
SetInfo();
}
//드롭다운리스트1의 선택 항목이 변경될 경우 그 항목의 정보를 다시 출력하기 위해 SetInfo()메서드를 호출합니다.
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString);
string strSelect = "SELECT * FROM Member";
string strUpdate = "UPDATE Member SET password = @password, name = @name, phone = @phone WHERE user_id = @user_id";
SqlCommand cmdSelect = new SqlCommand(strSelect, con);
SqlCommand cmdUpdate = new SqlCommand(strUpdate, con);
cmdUpdate.Parameters.AddWithValue("@password", TextBox1.Text);
cmdUpdate.Parameters.AddWithValue("@name", TextBox2.Text);
cmdUpdate.Parameters.AddWithValue("@phone", TextBox3.Text);
cmdUpdate.Parameters.AddWithValue("@user_id", DropDownList1.SelectedItem.Value);
SqlDataAdapter ad = new SqlDataAdapter();
ad.SelectCommand = cmdSelect;
ad.UpdateCommand = cmdUpdate;
DataSet ds = new DataSet();
ad.Fill(ds);
DataTable table = ds.Tables[0];
DataRow[] rows = table.Select("user_id = '" + DropDownList1.SelectedItem.Value + "'");
if (rows.Length > 0)
{
rows[0]["password"] = TextBox1.Text;
rows[0]["name"] = TextBox2.Text;
rows[0]["phone"] = TextBox3.Text;
ad.Update(ds);
}
Label2.Text = Label1.Text + "의 정보가 수정되었습니다.";
}
// 수정된 정보를 다시 업데이트하는 구문입니다. 앞의 프로세스 예제문을 참고하세요!
DropDownList에서 선택한 항목에 대한 정보가 TextBox에 각각 출력되고 수정하고자 하는 부분을 수정하여
수정버튼을 누르게 되면 수정이됩니다!!
'ASP.NET' 카테고리의 다른 글
[ASP.NET] 테마(Themes) #3 (0) | 2012.01.17 |
---|---|
[ASP.NET] 테마(Themes) #2 (0) | 2012.01.17 |
[ASP.NET] 테마(Themes) #1 (0) | 2012.01.17 |
[ADO.NET]DB연동-비연결기반 INSERT (0) | 2012.01.17 |
[ADO.NET] DB연동-비연결기반 SELECT (0) | 2012.01.17 |
[ADO.NET] DB연결- 연결기반 UPDATE (0) | 2012.01.17 |
[ADO.NET] DB 연동하기 - 연결기반 INSERT (0) | 2012.01.17 |