DB연결해서 테이블의 데이터 값과 개수를 출력하는 예제문이다.
[결과 화면] | [DB 쿼리문 출력 결과] |
코드
<%
-- DB 연결
Set oraConn = Server.CreateObject("ADODB.Connection")
oraConn.Open("dsn=[dsn];uid=[id];pwd=[password]")
Set oraRec = Server.CreateObject("ADODB.Recordset")
sql = "select * from emp"
oraRec.Open sql, oraConn
%>
<html>
<head>
<title>예제 - 여러 개 데이터 출력하기</title>
</head>
<body>
<%If OraRec.EOF Then %>
<p>데이터가 없습니다.</p>
<p>감사합니다.</p>
<%Else %>
<h2>쿼리 결과로 가져온 데이터입니다.</h2><hr />
<%Dim count %>
<%Do While Not OraRec.EOF %>
<p>
번호는 <b><%=OraRec.Fields("EMPNO")%> </b>,
이름은 <b><%=OraRec.Fields("ENAME")%> </b>,
직업는 <b><%=OraRec.Fields("JOB")%> </b>,
부서이름은 <b><%=OraRec.Fields("DEPTNO")%> </b> 입니다.
</p>
<%OraRec.MoveNext%>
<%count = count+1 %>
<%Loop %>
<b><%=count%></b>개 찾았습니다.
<%End If %>
</body>
</html>
코드 리뷰
<%If OraRec.EOF Then %>
<p>데이터가 없습니다.</p>
<p>감사합니다.</p>
<%Else %>
<h2>쿼리 결과로 가져온 데이터입니다.</h2>
<%End If %>
- 데이터가 없는 경우 OraRec.EOF : true > "데이터가 없습니다." 출력
- 데이터가 있는 경우 OraRec.EOF : false > "쿼리 결과로 가져온 데이터입니다." 출력
<%Dim count %>
<%Do While Not OraRec.EOF %>
<p>
번호는 <b><%=OraRec.Fields("EMPNO")%> </b>,
이름은 <b><%=OraRec.Fields("ENAME")%> </b>,
직업는 <b><%=OraRec.Fields("JOB")%> </b>,
부서이름은 <b><%=OraRec.Fields("DEPTNO")%> </b> 입니다.
</p>
<%OraRec.MoveNext%>
<%count = count+1 %>
<%Loop %>
<b><%=count%></b>개 찾았습니다.
- Dim count 변수 선언
- While Not OraRec.EOF(데이터가 있으니까 false) > 앞에 Not을 붙여줘서 true 됨 > 데이터가 있는 동안 실행
- OraRec.Fields("필드명")으로 필드 출력
- OraRec.MoveNext로 다음 레코드로 넘어감
- count에 1씩 더해주면서 개수 저장
'프로그램 > ASP' 카테고리의 다른 글
[ASP] 게시판 만들기 1 - 프로젝트 기획, DB테이블 구성 (0) | 2021.04.02 |
---|---|
[ASP] 디렉토리 목록 얻어오기, DB값 테이블로 출력하기 (0) | 2021.03.10 |
[ASP] 문자열 함수, 숫자 함수 실습 (0) | 2021.03.09 |
[ASP] 날짜 및 시간 함수 실습 (0) | 2021.03.09 |
[ASP] 레코드 이동하기 (MoveNext, MovePrevious, MoveFirst, MoveLast) (0) | 2021.03.09 |