ASP.NET : Database 접근

Download Report

Transcript ASP.NET : Database 접근

3주 실습강의
ASP.NET : Database 접근
2010 컴퓨터공학실험(Ⅰ)
Preface
 폼의 유효성 검사 : Field Validation
- RequiredFieldValidator
- RegularExpressionValidator
- ValidationSummary
 ASP.NET에서 Access DB (MDB) 연결 / 조작
- AccessDataSource (or SqlDataSource)
- GridView
- OleDbConnection, OleDbCommand,
OleDbDataReader
Artificial Intelligence Laboratory
2
DB 테이블
create table cee_w3_board (
seq int identity (1, 1) not null primary key clustered,
writer varchar (20) not null ,
pwd varchar (20) not null ,
email varchar (100) null ,
title varchar (200) not null ,
writedate smalldatetime not null default (getdate()),
readed int not null default (0),
mode tinyint not null ,
content varchar (8000) null
)
Artificial Intelligence Laboratory
3
MS JetDB : 테이블 구조
 Access – 디자인 보기
Artificial Intelligence Laboratory
4
Write.aspx – 레이아웃
Artificial Intelligence Laboratory
5
Write.aspx – RadioButtonList 속성
Artificial Intelligence Laboratory
6
Write.aspx 접근 방법
Form
입력/검증
Form →
DB Column
Binding
Store Form
Data in
Database
Artificial Intelligence Laboratory
7
Write.aspx – 웹폼 컨트롤 속성
Artificial Intelligence Laboratory
8
Write.aspx – Validation 컨트롤 속성
Artificial Intelligence Laboratory
9
Validator의 종류 / 기능
Artificial Intelligence Laboratory
10
Write.aspx – 데이터 컨트롤
OR
Artificial Intelligence Laboratory
11
Write.aspx – 데이터 소스 구성 (1/5)
Artificial Intelligence Laboratory
12
Write.aspx – 데이터 소스 구성 (2/5)
Artificial Intelligence Laboratory
13
Write.aspx – 데이터 소스 구성 (3/5)
Artificial Intelligence Laboratory
14
Write.aspx – 데이터 소스 구성 (4/5)
Artificial Intelligence Laboratory
15
Write.aspx – 데이터 소스 구성 (5/5)
Artificial Intelligence Laboratory
16
Write.aspx – 데이터 컨트롤 속성
데이터 소스 구성을 통하지 않고,
“데이터” - 각 쿼리 부분에 원하는 쿼리를 작성하여
설정을 완료할 수 있다.
“동작” – DataSourceMode속성에는 DataSet과
DataReader가 있는데, GridView와 같은 컨트롤
에서는 DataSet을, ListBox와 같이 정렬, 페이징
등이 필요 없는 컨트롤에서는 DataReader로
설정하여 사용할 수 있다.
Artificial Intelligence Laboratory
17
Write.aspx – 쿼리문 입력
Artificial Intelligence Laboratory
18
Write.aspx – 파라메터 컬렉션 편집
Artificial Intelligence Laboratory
19
Write.aspx – 코드 입력 및 완료
private void btnSubmit_Click(object sender, System.EventArgs e)
{
if (IsValid && Label1.Visible==false)
{
Sqldata.InsertParameters["write"].DefaultValue = txtWriter.Text;
Sqldata.InsertParameters["pwd"].DefaultValue = txtPassword.Text;
Sqldata.InsertParameters["email"].DefaultValue = txtEmail.Text;
Sqldata.InsertParameters["title"].DefaultValue = txtTitle.Text;
Sqldata.InsertParameters["writedate"].DefaultValue = DateTime.Now.ToString();
Sqldata.InsertParameters["content"].DefaultValue = txtContent.Text;
Sqldata.InsertParameters["mode"].DefaultValue = rdoMode.SelectedIndex.ToString();
Sqldata.Insert();
Response.Redirect("list.aspx");
}
}
만약 Parameter 연결을 했을 경우, 다음과 같이 짧은 코드로 완성이 가능하다.
protected void Button_Submit_Click(object sender, EventArgs e)
{
if(IsValid) // 글 삽입 모드
{
Sqldata.Insert();
Response.Redirect("list.aspx");
}
}
Artificial Intelligence Laboratory
20
List.aspx – 개발 접근 순서
DataSource
선택 / 컨트
• DB에 저장된 게시물 목록을 가져오고,
• 각 게시물과 연결을 어떻게 해야 할 것인가?
롤 배치
• GridView – Access(or Sql)DataSource 연결
페이지
• Paging – GridView 기본 사용? 직접 구현?
• 게시물을 보기 위해 TemplateItem을 구현
각 게시물
연결
• 작성자 메일 연결을 구현
Artificial Intelligence Laboratory
21
List.aspx – 컨트롤 설정
Artificial Intelligence Laboratory
22
List.aspx – DataSource : Select 구문
Artificial Intelligence Laboratory
23
List.aspx – GridView 속성
Artificial Intelligence Laboratory
24
List.aspx – GridView 필드 설정
Artificial Intelligence Laboratory
25
List.aspx – 데이터 컨트롤 속성
Artificial Intelligence Laboratory
26
List.aspx – 데이터 컨트롤 속성
Artificial Intelligence Laboratory
27
List.aspx – 데이터 컨트롤 속성
Artificial Intelligence Laboratory
28
List.aspx – 자동 서식 지정
Artificial Intelligence Laboratory
29
List.aspx – 호출되는 메서드 작성
public partial class List : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("view.aspx?id=" + GridView1.SelectedValue.ToString());
}
}
Artificial Intelligence Laboratory
30
View.aspx – 컨트롤 설정
Artificial Intelligence Laboratory
31
View.aspx 구현 접근 과정
Artificial Intelligence Laboratory
32
View.aspx – DB관련 개체 설정
 OleDbConnection 하나
- ConnectionString 설정
 OleDbCommand 1
- (Name) : dbCommandGetArticle
- Connection : dbConnection
- CommandText :
SELECT writer,email,title,mode,content,readed,writeDate
FROM cee_w3_board WHERE seq = ?
- Parameters : 하나 추가
ParameterName : seq
SqlDbType : integer
Artificial Intelligence Laboratory
33
View.aspx – DB관련 개체 설정
 OleDbCommand 2
- (Name) : dbCommandUpdateReadCount
- Connection : dbConnection
- CommandText :
UPDATE cee_w3_board SET readed=readed+1 WHERE seq
=?
- Parameters : 하나 추가
ParameterName : seq
SqlDbType : integer
Artificial Intelligence Laboratory
34
View.aspx – DB관련 개체 설정
 OleDbCommand 3
- (Name) : dbCommandGetPrevArticle
- Connection : dbConnection
- CommandText :
SELECT top 1 seq, title FROM cee_w3_board WHERE seq > ?
ORDER BY seq ASC;
- Parameters : 하나 추가
ParameterName :seq
SqlDbType : integer
Artificial Intelligence Laboratory
35
View.aspx – DB관련 개체 설정
 OleDbCommand 4
- (Name) : dbCommandGetNextArticle
- Connection : dbConnection
- CommandText :
SELECT top 1 seq, title FROM cee_w3_board WHERE seq < ?
ORDER BY seq DESC
- Parameters : 하나 추가
ParameterName : seq
SqlDbType : integer
Artificial Intelligence Laboratory
36
View.aspx 세부 구현 (Page_Load : 1)
[seq]
Artificial Intelligence Laboratory
37
View.aspx 세부 구현 (Page_Load: 2)
[seq]
Artificial Intelligence Laboratory
38
Delete.aspx – 컨트롤 설정
Artificial Intelligence Laboratory
39
Delete.aspx – DB관련 개체 설정
Artificial Intelligence Laboratory
40
Delete.aspx – DB관련 개체 설정
Artificial Intelligence Laboratory
41
Delete.aspx – DB관련 개체 설정
Artificial Intelligence Laboratory
42
Delete.aspx – 소스 편집
Artificial Intelligence Laboratory
43
글 수정 – Write.aspx 의 재구성
Artificial Intelligence Laboratory
44
글 수정 – Write.aspx 재구성 접근 방법
• 게시물 번호를 넘겨 받으면 수정
새 글 쓰기 / 수
정의 구분
페이지 수정의
• 그렇지 않으면 새 글 작성
• Page_Load 시점에서 기존의 글 내용을 읽어와야 함.
경우
• 새 글 쓰기 / 수정을 구분해서 INSERT / UPDATE를 사용.
데이터베이스 삽
입
• 수정부를 직접 구현? / DataBind를 지정?
Artificial Intelligence Laboratory
45
글수정 – DB관련 개체 추가(1/2)
 OleDbCommand 1
- (Name) : dbCommandGetArticleForModify
- Connection : dbConnection
- CommandText :
SELECT writer, email, title, content, mode FROM
cee_w3_board WHERE seq = ?
- Parameters : 하나 추가
ParameterName : seq
SqlDbType : integer
Artificial Intelligence Laboratory
46
글수정 – DB관련 개체 추가(2/2)
 OleDbCommand 2
- (Name) : dbCommandModifyArticle
- Connection : dbConnection
- CommandText :
UPDATE cee_w3_board SET writer = ?, email = ?,
title = ?, content = ?, mode = ? WHERE seq = ?
AND pwd = ?
- Parameters : 7개 추가
Artificial Intelligence Laboratory
47
글 수정 – Write.aspx 구현 : Page_load (1)
Artificial Intelligence Laboratory
48
글 수정 – Write.aspx 구현 : Page_load (2)
Artificial Intelligence Laboratory
49
글 수정 – Write.aspx 세부 구현 : DataBind 사용
Artificial Intelligence Laboratory
50
글 수정 – Write.aspx
Artificial Intelligence Laboratory
51
글 수정 – Write.aspx
Artificial Intelligence Laboratory
52