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



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

 

<!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>

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

        <asp:Button ID="btnView" runat="server" Text="상세보기" onclick="btnView_Click" />       

        <br />

        번호 :

        <asp:Label ID="lblCategoryID" runat="server"></asp:Label>

        <br />

        카테고리명 :

        <asp:Label ID="lblCategoryName" runat="server"></asp:Label>

        <br />

        부모카테고리:

        <asp:Label ID="lblSuperCategory" runat="server"></asp:Label>

        <br />

        정렬순서 :

        <asp:Label ID="lblAlign" runat="server"></asp:Label>

    </div>

    </form>

</body>

</html>

 









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




==> [FrmCategoryView.aspx.cs]




using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Configuration;

using System.Data;

 

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    protected void btnView_Click(object sender, EventArgs e)

    {

        //[1] 커넥션

        SqlConnection con = new SqlConnection(

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

        con.Open();

 

        //[2] 커맨드 : 단일 레코드

        SqlCommand cmd = new SqlCommand(

            "Select * From Categories Where CategoryID = @CategoryID", con);

        cmd.CommandType = CommandType.Text;

 

        //[!] 파라미터 추가

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

 

        //[3] 데이터리더

        SqlDataReader dr = cmd.ExecuteReader();

 

        //[4] Read : 문자열/정수형인덱서 또는 GetXxx() 메서드로 읽어오기

        while (dr.Read()) // Read() : 데이터가 읽혀지면 True값 반환

        {

            this.lblCategoryID.Text = dr["CategoryID"].ToString(); // 문자열 인덱서

            this.lblCategoryName.Text = dr[1].ToString(); // 정수(서수)형 인덱서

            if (dr["SuperCategory"] != null) // 널값 처리

            {

                this.lblSuperCategory.Text = dr["SuperCategory"].ToString();

            }

            this.lblAlign.Text = dr["Align"].ToString();

        }

        dr.Close();

        con.Close();

    }

}

 




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




[실행결과]

--> 아래그림에서 '텍스트박스'에 [dbo.Categories]테이블에 존재하는 범위의 "CategoryID"값을 대입한 후 "상세보기"버튼을 누르면 해당"CategoryID"값의 데이터가 각각의 레이블에 출력된다.




Posted by holland14
: