ADO2.1버전부터 데이터베이스의 결과값을 XML로 리턴시킬수가 있습니다.
그렇게 하면 이런 방법은 사용할 필요가 없겠죠. 이 소스는 예제로서 XML의 이해와
테이블의 바인딩예제를 보여줍니다.
데이터베이스는 MSSQL에 기본으로 들어있는 Pubs를 사용했으며
수정하셔야 할것은 Data Source의 컴퓨터 명만수정하시고 asp를 실행시키면 됩니다.
간단한 예제이지만 의외로 이예제를 필요로 하시는 분이 계셔서 만들어 올립니다.
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<%
'데이터베이스 커넥션
Dim objConn, strConn
Set objConn = CreateObject("ADODB.Connection")
strConn = ""
strConn = strConn & "Provider = SQLOLEDB;"
strConn = strConn & "Data Source = JUNWORKS77;" '컴퓨터명 수정해주세요.
strConn = strConn & "Initial Catalog = pubs;"
strConn = strConn & "User Id = sa;"
strConn = strConn & "Password ="
objConn.ConnectionString = strConn
objConn.Open
objConn.CursorLocation = 3
'레코드셋 연결
Dim objRs, strQuery, strSupport
Set objRs = CreateObject("ADODB.Recordset")
objRs.CursorType = adOpenStatic
'쿼리
strQuery = "SELECT * FROM authors"
objRs.Open strQuery, objConn
objString = ""
objString = "<?xml version='1.0' encoding='euc-kr'?><xml
id='xmlAuthor'><root>"
'데이터베이스의 값만큼 XML을 만든다.
Do Until objRs.EOF
objString = objString & "<author>"
objString = objString & "<au_id >"&objRs("au_id")&"</au_id>"
objString = objString & "<au_lname>"&objRs("au_lname")
&"</au_lname>"
objString = objString & "<au_fname>"&objRs("au_fname")
&"</au_fname>"
objString = objString & "<phone>"&objRs("phone")&"</phone>"
objString = objString & "<address>"&objRs("address")
&"</address>"
objString = objString & "<city>"&objRs("city")&"</city>"
objString = objString & "<state>"&objRs("state")&"</state>"
objString = objString & "<zip>"&objRs("zip")&"</zip>"
objString = objString & "<contract>"&objRs("contract")
&"</contract>"
objString = objString & "</author>"
objRs.MoveNext
Loop
objString = objString & "</root></xml>"
Response.Write objString
%>
</HEAD>
<BODY>
<TABLE ID="XMLControl" DATASRC="#xmlAuthor" DATAPAGESIZE="10">
<THEAD>
<TR>
<TD bgcolor=Lavender><B>au_id</B></TD>
<TD bgcolor=Lavender><B>au_lname</B></TD>
<TD bgcolor=Lavender><B>au_fname</B></TD>
<TD bgcolor=Lavender><B>phone</B></TD>
<TD bgcolor=Lavender><B>address</B></TD>
<TD bgcolor=Lavender><B>city</B></TD>
<TD bgcolor=Lavender><B>state</B></TD>
<TD bgcolor=Lavender><B>zip</B></TD>
<TD bgcolor=Lavender><B>contract</B></TD>
</TR>
</THEAD>
<TR>
<TD><FONT size="2"><DIV DATAFLD="au_id"></FONT></DIV></TD>
<TD><FONT size="2"><DIV DATAFLD="au_lname"></FONT></DIV></TD>
<TD><FONT size="2"><DIV DATAFLD="au_fname"></FONT></DIV></TD>
<TD><FONT size="2"><DIV DATAFLD="phone"></FONT></DIV></TD>
<TD><FONT size="2"><DIV DATAFLD="address"></FONT></DIV></TD>
<TD><FONT size="2"><DIV DATAFLD="city"></FONT></DIV></TD>
<TD><FONT size="2"><DIV DATAFLD="state"></FONT></DIV></TD>
<TD><FONT size="2"><DIV DATAFLD="zip"></FONT></DIV></TD>
<TD><FONT size="2"><DIV DATAFLD="contract"></FONT></DIV></TD>
</TR>
<TFOOT>
<TR>
<TD><INPUT type="button" value="맨처음으로"
onclick="XMLControl.firstPage()"></TD>
<TD><INPUT type="button" value="이전"
onclick="XMLControl.previousPage()"></TD>
<TD><INPUT type="button" value="다음"
onclick="XMLControl.nextPage()"></TD>
<TD><INPUT type="button" value="마지막으로"
onclick="XMLControl.lastPage()"></TD>
</TR>
</TFOOT>
</TABLE>
</BODY>
</HTML>
그렇게 하면 이런 방법은 사용할 필요가 없겠죠. 이 소스는 예제로서 XML의 이해와
테이블의 바인딩예제를 보여줍니다.
데이터베이스는 MSSQL에 기본으로 들어있는 Pubs를 사용했으며
수정하셔야 할것은 Data Source의 컴퓨터 명만수정하시고 asp를 실행시키면 됩니다.
간단한 예제이지만 의외로 이예제를 필요로 하시는 분이 계셔서 만들어 올립니다.
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<%
'데이터베이스 커넥션
Dim objConn, strConn
Set objConn = CreateObject("ADODB.Connection")
strConn = ""
strConn = strConn & "Provider = SQLOLEDB;"
strConn = strConn & "Data Source = JUNWORKS77;" '컴퓨터명 수정해주세요.
strConn = strConn & "Initial Catalog = pubs;"
strConn = strConn & "User Id = sa;"
strConn = strConn & "Password ="
objConn.ConnectionString = strConn
objConn.Open
objConn.CursorLocation = 3
'레코드셋 연결
Dim objRs, strQuery, strSupport
Set objRs = CreateObject("ADODB.Recordset")
objRs.CursorType = adOpenStatic
'쿼리
strQuery = "SELECT * FROM authors"
objRs.Open strQuery, objConn
objString = ""
objString = "<?xml version='1.0' encoding='euc-kr'?><xml
id='xmlAuthor'><root>"
'데이터베이스의 값만큼 XML을 만든다.
Do Until objRs.EOF
objString = objString & "<author>"
objString = objString & "<au_id >"&objRs("au_id")&"</au_id>"
objString = objString & "<au_lname>"&objRs("au_lname")
&"</au_lname>"
objString = objString & "<au_fname>"&objRs("au_fname")
&"</au_fname>"
objString = objString & "<phone>"&objRs("phone")&"</phone>"
objString = objString & "<address>"&objRs("address")
&"</address>"
objString = objString & "<city>"&objRs("city")&"</city>"
objString = objString & "<state>"&objRs("state")&"</state>"
objString = objString & "<zip>"&objRs("zip")&"</zip>"
objString = objString & "<contract>"&objRs("contract")
&"</contract>"
objString = objString & "</author>"
objRs.MoveNext
Loop
objString = objString & "</root></xml>"
Response.Write objString
%>
</HEAD>
<BODY>
<TABLE ID="XMLControl" DATASRC="#xmlAuthor" DATAPAGESIZE="10">
<THEAD>
<TR>
<TD bgcolor=Lavender><B>au_id</B></TD>
<TD bgcolor=Lavender><B>au_lname</B></TD>
<TD bgcolor=Lavender><B>au_fname</B></TD>
<TD bgcolor=Lavender><B>phone</B></TD>
<TD bgcolor=Lavender><B>address</B></TD>
<TD bgcolor=Lavender><B>city</B></TD>
<TD bgcolor=Lavender><B>state</B></TD>
<TD bgcolor=Lavender><B>zip</B></TD>
<TD bgcolor=Lavender><B>contract</B></TD>
</TR>
</THEAD>
<TR>
<TD><FONT size="2"><DIV DATAFLD="au_id"></FONT></DIV></TD>
<TD><FONT size="2"><DIV DATAFLD="au_lname"></FONT></DIV></TD>
<TD><FONT size="2"><DIV DATAFLD="au_fname"></FONT></DIV></TD>
<TD><FONT size="2"><DIV DATAFLD="phone"></FONT></DIV></TD>
<TD><FONT size="2"><DIV DATAFLD="address"></FONT></DIV></TD>
<TD><FONT size="2"><DIV DATAFLD="city"></FONT></DIV></TD>
<TD><FONT size="2"><DIV DATAFLD="state"></FONT></DIV></TD>
<TD><FONT size="2"><DIV DATAFLD="zip"></FONT></DIV></TD>
<TD><FONT size="2"><DIV DATAFLD="contract"></FONT></DIV></TD>
</TR>
<TFOOT>
<TR>
<TD><INPUT type="button" value="맨처음으로"
onclick="XMLControl.firstPage()"></TD>
<TD><INPUT type="button" value="이전"
onclick="XMLControl.previousPage()"></TD>
<TD><INPUT type="button" value="다음"
onclick="XMLControl.nextPage()"></TD>
<TD><INPUT type="button" value="마지막으로"
onclick="XMLControl.lastPage()"></TD>
</TR>
</TFOOT>
</TABLE>
</BODY>
</HTML>
'asp' 카테고리의 다른 글
[ASP] SQLHelper (1) | 2007.10.05 |
---|---|
ASP/테이블 너비 고정시키기 (0) | 2007.05.03 |
ASP 자동완성 (1) | 2007.05.03 |
Binary 데이타를 받아서 server에 화일로 저장시키는 (2) | 2007.05.03 |
PDF 파일로 변환 (1) | 2007.05.03 |