==> [FrmCategoryModify.aspx] 소스 및 디자인



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmCategoryModify.aspx.cs" Inherits="Category_FrmCategoryModify" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

   

        카테고리 수정/삭제<br />

        <br />

        카테고리번호 :

        <asp:TextBox ID="txtCategoryID" runat="server"></asp:TextBox>

        <br />

        카테고리명 :

        <asp:TextBox ID="txtCategoryName" runat="server"></asp:TextBox>

        <br />

        <asp:Button ID="btnModify" runat="server" Text="수정" onclick="btnModify_Click" />

        <asp:Button ID="btnDelete" runat="server" Text="삭제" onclick="btnDelete_Click"

            style="height: 21px" /><br />

        <asp:Label ID="lblDisplay" runat="server" ForeColor="Red"></asp:Label>

   

    </div>

    </form>

</body>

</html>

 









-------------------------------------------------------------------------------------




==> [FrmCategoryModify.aspx.cs]





using System;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

 

public partial class Category_FrmCategoryModify : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    protected void btnModify_Click(object sender, EventArgs e)

    {

        //[0] 변수 선언부

        string updateQuery = @"

            Update Categories

            Set CategoryName = @CategoryName

            Where CategoryID = @CategoryID";

 

        //[1] 커넥션

        SqlConnection con = new SqlConnection(

            ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        con.Open();

 

        //[2] 커맨드

        SqlCommand cmd = new SqlCommand(updateQuery, con);

        cmd.CommandType = CommandType.Text;

 

        //[3] 파라미터 추가

        cmd.Parameters.AddWithValue("@CategoryID", txtCategoryID.Text);

        cmd.Parameters.AddWithValue("@CategoryName", txtCategoryName.Text);

 

        //[4] 실행 : Create, Alter, Drop, Insert, Update,Delete는 모두 ExecuteNonQuery()

        int recordAffected = cmd.ExecuteNonQuery();

 

        //[5] 마무리

        lblDisplay.Text = recordAffected.ToString() + "개가 변경됨";

        con.Close();

    }

    protected void btnDelete_Click(object sender, EventArgs e)

    {

        //[0] 변수 선언부

        string deleteQuery = "Delete Categories Where CategoryID = @CategoryID";

 

        //[1] 커넥션

        SqlConnection con = new SqlConnection(

            ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        con.Open();

 

        //[2] 커맨드

        SqlCommand cmd = new SqlCommand(deleteQuery, con);

        cmd.CommandType = CommandType.Text;

 

        //[3] 파라미터 추가

        cmd.Parameters.AddWithValue("@CategoryID", txtCategoryID.Text);

 

        //[4] 실행 : Create, Alter, Drop, Insert, Update,Delete는 모두 ExecuteNonQuery()

        int recordAffected = cmd.ExecuteNonQuery();

 

        //[5] 마무리

        lblDisplay.Text = recordAffected.ToString() + "개가 변경됨";

        con.Close();

 

    }

}

 

 

 



-------------------------------------------------------------------------------------




[실행결과]

- 수정-

--> 수정하기 전 [SQL Server] - [Market]데이터베이스 - [dbo.Categories]테이블의 화면




--> 웹페이지에서 텍스트박스에 각각 "19/컴퓨터"를 입력하고 수정버튼을 누르면 버튼아래 "레이블"에 빨간색 글씨로 "1개가 변경됨"이라는 텍스트가 출력되면서 [dbo.Categories]테이블이 수정된다.



--> 수정 후 [dbo.Categories]테이블의 화면(위의 그림에서는 [dbo.Categories]테이블의 'CategoryID = 19'인 값은 '노트북'임.)






- 삭제 -

--> 삭제하기 전 [SQL Server] - [Market]데이터베이스 - [dbo.Categories]테이블의 화면



--> 웹페이지에서  "카테고리번호"의 텍스트박스에 '26'을 입력하고 삭제버튼을 누르면 버튼아래 "레이블"에 빨간색 글씨로 "1개가 변경됨"이라는 텍스트가 출력되면서 [dbo.Categories]테이블이 수정된다.



--> 삭제 후 [dbo.Categories]테이블의 화면(위의 그림에서는 [dbo.Categories]테이블의 'CategoryID = 26'의 데이터가 존재했었으나, '삭제' 후 'CategoryID = 26'의 데이터가 [dbo.Categories]테이블에서 삭제된 것을 볼 수 있다.)



Posted by holland14
: