18-2 �����P��Ʈw��X���򥻽d��

¦bASPµ{¦¡³]­p¸Ì¡A¥Î¨Ó¦s¨ú¸ê®Æ®w©Îªí®æ¸ê®Æªºª«¥ó²ÎºÙ ADO¡]ActiveX Data Objects¡^¡A³o¬O¤@­Ó ASP ¤º«Øªº¸ê®Æ®w¦s¨ú¤¸¥ó¡A¥i¥H¸g¥Ñ JavaScript/JScript¡BVBScript µ¥»y¨¥¨Ó±±¨î¸ê®Æ®wªº¦s¨ú¡A¨Ã¥i³s±µ¦hºØ¸ê®Æ®w¡A¥]¬A SQL Server¡BOracle¡BAccess µ¥¤ä´©ODBCªº¸ê®Æ®w¡CADO ¥D­n¥]§t Connection¡BRecordset ¤Î Command ¤TºØª«¥ó¡A¥»¤p¸`±N¤¶²Ð»P Connection ¬ÛÃöªº¸ê®Æ®w¾Þ§@¡C

¨Ï¥Î ADO ªº Connection ª«¥ó¨Ó¶i¦æ¸ê®Æ®wªºÀ˵ø¬d¸ß¡A¥D­n¦³¥H¤U¥|­Ó¨BÆJ¡G

  1. «Ø¥ß¸ê®Æ®w³sµ²¡AµM«á¶}±Ò¸ê®Æ®w¡G
    ¨Ï¥Î¡uServer.CreateObject¡v©w¸q¤@­Ó ADO ªº Connection ª«¥ó¡AµM«á¨Ï¥Î¨ä¡uOpen¡vªº¤èªk¨Ó¶}±Ò¸ê®Æ®w¨Ó·½¡A½d¨Òµ{¦¡½X¦p¤U¡G conn = Server.CreateObject("ADODB.Connection"); ±µµÛ§Ú­Ì¥i¥H³]©w conn ª«¥óªº ConnectionString ©Ê½è¨Ó«ü©w¸ê®Æ®w¡A¦@¦³¥|ºØ§@ªk¡G
    1. ª½±µ«ü©w Access ¸ê®Æ®w¦b¥»¾÷µwºÐªº¸ô®|¡G conn.ConnectionString = "DBQ=¸ê®Æ®wÀÉ®×;Driver={Microsoft Access Driver (*.mdb)};Driverld=25;FIL=MS Access;UID=**;PWD=**";
    2. «ü©w DSN¡]¸ê®Æ¨Ó·½¦WºÙ¡^¡G conn.ConnectionString = "¸ê®Æ¨Ó·½¦WºÙ";
    3. ª½±µ³sµ²¦Ü SQL Server ¸ê®Æ®w¡G conn.ConnectionString = "Driver={SQL Server};Datebase=¸ê®Æ®w¦WºÙ;Server=¦ì§};UID=**;PWD=**";
    4. ª½±µ³sµ²¦Ü UNIX ªº MySQL ¸ê®Æ®w¡G conn.ConnectionString = "Driver={MySQL};Datebase=¸ê®Æ®w¦WºÙ;Server=¦ì§};UID=**;PWD=**";
    ¨Ï¥Î¥H¤W¥ô¤@¤èªk§Y¥i³sµ²¨ì§A·Q­n³sµ²ªº¥»¾÷©Î»·ºÝ¸ê®Æ®w¡C³Ì«á¦A¥Î conn ª«¥óªº Open ¤èªk¡A¨Ó¶}±Ò¸ê®Æ®w¡G conn.Open();

  2. °õ¦æSQL«ü¥O¡A¨Ã±N¬d¸ßµ²ªGÀx¦s©ó Recordset ¤¤¡G­Y¬OÀ˵ø¬d¸ß¡A§Ú­Ì¥i±Nµ²ªG¦s¦Ü RecordSet ª«¥óÅܼơurs¡v¤¤¡A¥H«K«áÄò¨ú¥Î¡A¨å«¬µ{¦¡½X¦p¤U¡G sql = "Select * from testTable"; rs = conn.Execute(sql); ¥H¤Wªºµ{¦¡½X±N SQL «ü¥O©Ò¬d¸ß¨ìªºµ²ªGÀx¦s¨ì Recordset ª«¥ó rs ¤¤¡C­Y¤£¬OÀ˵ø¬d¸ß¡A«h¤£»Ý­n±Nµ²ªG¦s©ñ©óÅÜ¼Æ rs¡C
  3. ¨ú±oÄæ¦ì¦WºÙ¤Î¤º®e¡G­Y¬OÀ˵ø¬d¸ß¡A§Ú­Ì¥i¥H¨Ï¥Î¤U¦Cªºªº¤è¦¡¨Ó¨ú±oÄæ¦ì¦WºÙ¤Î¤º®eµ¥¸ê°T¡G
    • rs.EOF¡G¬O§_¤w«ü¨ì³Ì«á¤@µ§¸ê®Æ¡A¬O¬°True¡A¤Ï¤§¬°False
    • rs.Fields.Count¡GRecordSetsªºÄæ¦ì¼Æ
    • rs(i).Name¡G²Äi­ÓÄæ¦ìªºÄæ¦ì¦WºÙ
    • rs("Äæ¦ì¦WºÙ")¡GŪ¨ú¬Y­Ó¯S©wÄæ¦ì¦WºÙªº¸ê®Æ
    • rs(i)¡G²Äi­ÓÄæ¦ìªº¸ê®Æ
    • rs.MoveNext¡G±N«ü¼Ð²¾¨ì¤U¤@µ§
    • rs.MovePrev¡G±N«ü¼Ð²¾¨ì¤W¤@µ§
    • rs.MoveFirst¡G±N«ü¼Ð²¾¨ì²Ä¤@µ§
    • rs.MoveLast¡G±N«ü¼Ð²¾¨ì³Ì«á¤@µ§
    ¨Ò¦p¡A­Y­n¦L¥XÄæ¦ì¦WºÙ¡A¥i¥H¨Ï¥Î¤U¦C¨å«¬µ{¦¡½X¡G for (i=0; i<rs.Fields.Count; i++) Response.write(rs(i).Name+"<br>"); ­Y­n¦L¥X¨C¤@µ§¸ê®Æªº¨C¤@­ÓÄæ¦ì­È¡A¥i¥H¨Ï¥Î¤U¦C¨å«¬µ{¦¡½X¡G while (!rs.EOF){ for (i=0; i<rs.Fields.Count; i++) Response.write(rs(i)+" "); Response.write("<br>\n"); rs.MoveNext(); } ¥H¤Wªºµ{¦¡½X¥Ñ rs(i) Ū¨ú¸ê®Æ®wÄæ¦ìªº¸ê®Æ¡Ars.MoveNext() ±N Recordset ªº¸ê®Æ«ü¼Ð²¾¨ì¤U¤@µ§¡A¸g¥Ñ rs.EOF ¨Ó§PÂ_¬O§_¤w¨ì¤F³Ì¥½µ§¸ê®Æ¡A¨Ã°t¦X while °j°é§Y¥i±o¨ì©Ò¦³¬d¸ßµ²ªGªº¸ê®Æ¡C

  4. Ãö³¬ RecordSet ¤Î¸ê®Æ®w³sµ²¡G½d¨Òµ{¦¡½X¦p¤U¡G rs.Close(); conn.Close();
¨Ò¦p¡A§Ú­Ì¥i¥H¦bºô­¶¤¤¦L¥X Access ¸ê®Æ®w asp/example/database/test.mdb ¸Ì­±ªº¸ê®Æªí testTable ªº¤º®e¡G

Example¡]database/listdb01.asp¡^¡G

¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

­ì©lÀÉ¡]database/listdb01.asp¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<%@ language="jscript" %>
<% title="¥H JScript ¶i¦æ¸ê®Æ®w¦Cªí¡G¨Ï¥Î¸ê®Æ®w¸ô®|" %>
<!--#include file="../head.inc"-->
<hr>

<%
//====== Step 1¡G«Ø¥ß¸ê®Æ®w³sµ²¡AµM«á¶}±Ò¸ê®Æ®w
conn = Server.CreateObject("ADODB.Connection");
conn.ConnectionString = "DBQ=" + Server.MapPath("test.mdb") + ";Driver={Microsoft Access Driver (*.mdb)};Driverld=25;FIL=MS Access;";
conn.Open();

//====== Step 2¡G°õ¦æSQL«ü¥O¡A¨Ã±N¬d¸ßµ²ªGÀx¦s©ó Recordset ¤¤
sql = "SELECT * FROM testTable";	//±q¸ê®Æªí testTable ¨ú¥X©Ò¦³¸ê®Æ
rs = conn.Execute(sql);
%>

<table border=1 align=center>
<tr bgcolor="cyan">
<%
//====== Step 3¡G³z¹L RecordSet ¶°¦X¨ú±oÄæ¦ìªº¤º®e
//¦L¥XÄæ¦ì¦WºÙ
for (i=0; i<rs.Fields.Count; i++)
	Response.write("<th>"+rs(i).Name+"</th>\n");
%>
</tr>
<%
//¦L¥X¨C¤@µ§¸ê®Æ
while (!rs.EOF) {
	Response.write("<tr>\n");
	for (i=0; i<rs.Fields.Count; i++)
		Response.write("<td>"+rs(i)+"&nbsp;</td>\n");
	rs.MoveNext();
}
%>
</table>

<%
//====== Step 4¡GÃö³¬ RecordSet ¤Î¸ê®Æ®w³sµ²
rs.Close();
conn.Close();
%>

<hr>
<!--#include file="../foot.inc"-->

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì¨Ï¥Î¡uª½±µ«ü©w¸ê®Æ®w¦b¥»¾÷µwºÐªº¸ô®|¡vªº¤è¦¡¨Ó³sµ²¸ê®Æ®w¡A¨ä¤¤ªº SQL «ü¥O¡uSELECT * FROM testTable¡v¥Nªí¡u±q¸ê®Æªí testTable ¨ú¥X©Ò¦³¸ê®Æ¡v¡C¨ä¥¦»¡©ú¬Ò¥Hµù¸Ñªº¤è¦¡¼g¦bµ{¦¡½X¤¤¡A©Ò¥H¦b¦¹¤£¦AÂØ­z¡C­YŪªÌ¹ï VBScript ¤ñ¸û¼ô±x¡A¤]¥i¥H¨Ï¥Î VBScript ¨Ó¶i¦æÃþ¦üªº¤u§@¡A¨ä¬yµ{§¹¥þ¤@¼Ë¡A¥i°Ñ¦Ò¦¹½d¨Ò¡Gasp/example/database/listdb01_vbs.asp¡C

Hint
¦b¤W­z½d¨Ò¤¤ªº³Ì«á¤@µ§¸ê®Æ¡A¨ä¤¤ªº NickName Äæ¦ì©M Percentage Äæ¦ì³£¥¼¶ñ¤J¸ê®Æ¡A¦ý¬O NickName Äæ¦ìªº¨ÃµL¹w³]­È¡A¦]¦¹¥Ñ¸ê®Æ®w§ì¦^¨Óªº¸ê®ÆÅã¥Ü¬° null¡F¥t¡APercentage Äæ¦ìªº¹w³]­È¬O 0¡A©Ò¥H¨S¦³¦L¥X 0¡C³o¨ÇÄæ¦ìªºÄݩʤΨä¹w³]­È³£¥i¥Ñ Access ¸ê®Æªíªº¡u³]­pÀ˵ø¡v¿ï³æ¨Ó³]©w¡C

­Y­n¨Ï¥Î DSN ³sµ²¸ê®Æ®w¡A­º¥ý§Ú­Ì¥²¶·¥ý¦b¦øªA¾¹³]©w DSN¡]¸Ô²Ó¬yµ{½Ð¨£¤W¤@¤p¸`¡^¡AµM«á´N¥i¥H¦b ASP ¤º¸g¥Ñ DSN ¨Ó«ü©w¸ê®Æ®w¡]¥i¥H¬OªñºÝ©Î¬O»·ºÝ¡^¡C¥H¤W¤@­Ó½d¨Ò¦Ó¨¥¡A­Y­n¥Ñ DSN ¨Ó³sµ²¸ê®Æ®w¡A¦Ó¤£ª½±µ«ü©w¸ê®Æ®w¡A¥u­n§â¤U¤@¦C±Ô­z¡G

Conn.ConnectionString = "DBQ=" + Server.MapPath("test.mdb") + ";Driver={Microsoft Access Driver (*.mdb)};Driverld=25;FIL=MS Access;"; §ï¦¨¤U¤@¦C§Y¥i¡G Conn.ConnectionString = "dsn4test"; ¨ä¤¤ dsn4test ¥²¶·¤w³Q³]©w¬°«ü¦V test.mdb ªº ODBC ¸ê®Æ¨Ó·½¡C¬ÛÃö JScript ½d¨Ò¥i¨£ asp/example/database/listdb02.asp¡AVBScript ½d¨Ò«h¥i¨£ asp/example/database/listdb02.asp¡C

¹ï©ó¸ê®Æ®wªºÀ˵ø©M¦C¦L¡A¬O±`³Q¥Î¨ìªº¥\¯à¡A¦]¦¹§Ú­Ì±N¦¹¥\¯à¼g¦¨¤@­Ó¨ç¼Æ listQueryResult()¡A¨Ã¦s©ñ©ó listQueryResult.inc ¤¤¡A¨ä¤º®e¦p¤U¡G

­ì©lÀÉ¡]listQueryResult.inc¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<!-- List a table in a given database -->
<!-- "database" is the full path to a database -->
<!-- "table" is the table to be listed -->

<script runat=server language=jscript>
function listQueryResult(database, sql){
var Conn = Server.CreateObject("ADODB.Connection");
Conn.ConnectionString = "DBQ=" + Server.MapPath(database) + ";Driver={Microsoft Access Driver (*.mdb)};Driverld=25;FIL=MS Access;";
Conn.Open();
var RS = Conn.Execute(sql);

Response.write("<table border=1 align=center>");
Response.write("<tr align=center bgcolor=cyan>");

for (var i=0; i<RS.Fields.Count; i++)
	Response.write("<th>"+RS(i).Name+"</th>\n");
Response.write("</tr>")

color=["#ffffdd", "#ffeeee", "#eeffee", "#e0e0f9", "#eeeeff"];	// ÃC¦â¯x°}
k=0;
while (!RS.EOF) {
	Response.write("<tr bgcolor=" + color[k] + ">");
	for (i=0; i<RS.Fields.Count; i++)
		Response.write("<td>" + RS(i) + "&nbsp;</td>");
	Response.write("</tr>");
	k=k+1;
	if (k==color.length)
		k=0;
	RS.MoveNext();
}
RS.Close();
Conn.Close();
Response.write("</table>");
}
</script>


<script runat=server language=vbscript>
Function listQueryResult(database, sql)
set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "DBQ=" & Server.MapPath(database) & ";Driver={Microsoft Access Driver (*.mdb)};Driverld=25;FIL=MS Access;"
Set RS = Conn.Execute(sql)

Response.Write("<table border=1 align=center>")
Response.Write("<tr align=center bgcolor=cyan>")
dim i
For i=0 to RS.Fields.Count-1
	Response.Write("<th>" & RS(i).Name & "</th>")
next
Response.Write("</tr>")

color=Array("#ffffdd", "#ffeeee", "#eeffee", "#e0e0f9", "#eeeeff")	' ÃC¦â¯x°}
k=0
Do While NOT RS.EOF
	Response.Write("<tr bgcolor=" & color(k) & ">")
	For i=0 to RS.Fields.Count-1
		Response.Write("<td>" & RS(i) & "&nbsp;</td>")
	next
	Response.Write("</tr>")
	k=k+1
	If k=ubound(color)+1 Then
		k=0
	End If
	RS.MoveNext
Loop
RS.Close
Conn.Close
Response.Write("</table>")
End Function
</script>

¦b¤W­z­ì©l½X¤¤¡A§Ú­Ì¤À§O¼g¤F¾A¥Î©ó JScript ©M VBScript ªº¨ç¼Æ¡A¦]¦¹µL½×¬O¨Ï¥Î JScript ©Î VBScript ªº ASP ºô­¶¡A³£¥i¥H¨Ï¥Î¦¹¥]§tÀɨӦC¥X¸ê®Æ®w¬d¸ßªºµ²ªG¡C ¨Ï¥Î¤W­zªº¨ç¼Æ¨Ó¶i¦æ¸ê®Æ®w¦Cªí¡Aµ{¦¡½X´N·|°®°®²b²b¡A½d¨Ò¦p¤U¡G

Example¡]database/listdb03.asp¡^¡G

¨ä­ì©l½X¦p¤U¡G

­ì©lÀÉ¡]database/listdb03.asp¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<%@ language="jscript" %>
<% title="¥H JScript ¶i¦æ¸ê®Æ®w¦Cªí¡G¨Ï¥Î listQueryResult() ¨ç¼Æ" %>
<!--#include file="../head.inc"-->
<hr>

<!--#include file="../listQueryResult.inc"-->
<%
database="test.mdb";
sql="select * from testTable";
listQueryResult(database, sql);
%>

<hr>
<!--#include file="../foot.inc"-->

­Y¨Ï¥Î VBScript¡A«h¥i¨£¦¹½d¨Ò¡Gasp/example/database/listdb03_vbs.asp

«e­zªº½d¨Ò»¡©ú¤F¦p¦ó¶i¦æ¸ê®Æ®wªºÀ˵ø¬d¸ß¡A¨Ã±Nµ²ªGÅã¥Ü©ó ASP ºô­¶¡C¤@¯ë¦Ó¨¥¡ASQL «ü¥O¤w¸g¨ã¦³¹ï¸ê®Æ®w¶i¦æÀ˵ø¡B·s¼W¡B­×§ï¡B§R°£µ¥¥\¯à¡]«áÄò¨â¤p¸`¦³¸Ô²Ó»¡©ú¡^¡A¦]¦¹¥u­n¨Ï¥Î¾A·íªº SQL «ü¥O¡A¦A¥[¤W«e­zªº¤èªk¡A§Y¥i¹ï¸ê®Æ®w¶i¦æ§¹¥þªº³B²z¡C


JScript µ{¦¡³]­p»PÀ³¥Î¡G¥Î©ó¦øªA¾¹ºÝªº ASP Àô¹Ò