¦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 ¥Dn¥]§t Connection¡BRecordset ¤Î Command ¤TºØª«¥ó¡A¥»¤p¸`±N¤¶²Ð»P Connection ¬ÛÃöªº¸ê®Æ®w¾Þ§@¡C¨Ï¥Î ADO ªº Connection ª«¥ó¨Ó¶i¦æ¸ê®Æ®wªºÀ˵ø¬d¸ß¡A¥Dn¦³¥H¤U¥|Ó¨BÆJ¡G
¨Ò¦p¡A§ÚÌ¥i¥H¦bºô¶¤¤¦L¥X Access ¸ê®Æ®w asp/example/database/test.mdb ¸Ì±ªº¸ê®Æªí testTable ªº¤º®e¡G
- «Ø¥ß¸ê®Æ®w³sµ²¡AµM«á¶}±Ò¸ê®Æ®w¡G
¨Ï¥Î¡uServer.CreateObject¡v©w¸q¤@Ó ADO ªº Connection ª«¥ó¡AµM«á¨Ï¥Î¨ä¡uOpen¡vªº¤èªk¨Ó¶}±Ò¸ê®Æ®w¨Ó·½¡A½d¨Òµ{¦¡½X¦p¤U¡Gconn = Server.CreateObject("ADODB.Connection"); ±µµÛ§ÚÌ¥i¥H³]©w conn ª«¥óªº ConnectionString ©Ê½è¨Ó«ü©w¸ê®Æ®w¡A¦@¦³¥|ºØ§@ªk¡G¨Ï¥Î¥H¤W¥ô¤@¤èªk§Y¥i³sµ²¨ì§A·Qn³sµ²ªº¥»¾÷©Î»·ºÝ¸ê®Æ®w¡C³Ì«á¦A¥Î conn ª«¥óªº Open ¤èªk¡A¨Ó¶}±Ò¸ê®Æ®w¡G
- ª½±µ«ü©w Access ¸ê®Æ®w¦b¥»¾÷µwºÐªº¸ô®|¡G
conn.ConnectionString = "DBQ=¸ê®Æ®wÀÉ®×;Driver={Microsoft Access Driver (*.mdb)};Driverld=25;FIL=MS Access;UID=**;PWD=**"; - «ü©w DSN¡]¸ê®Æ¨Ó·½¦WºÙ¡^¡G
conn.ConnectionString = "¸ê®Æ¨Ó·½¦WºÙ"; - ª½±µ³sµ²¦Ü SQL Server ¸ê®Æ®w¡G
conn.ConnectionString = "Driver={SQL Server};Datebase=¸ê®Æ®w¦WºÙ;Server=¦ì§};UID=**;PWD=**"; - ª½±µ³sµ²¦Ü UNIX ªº MySQL ¸ê®Æ®w¡G
conn.ConnectionString = "Driver={MySQL};Datebase=¸ê®Æ®w¦WºÙ;Server=¦ì§};UID=**;PWD=**"; conn.Open(); - °õ¦æSQL«ü¥O¡A¨Ã±N¬d¸ßµ²ªGÀx¦s©ó Recordset ¤¤¡GY¬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 ¤¤¡CY¤£¬OÀ˵ø¬d¸ß¡A«h¤£»Ýn±Nµ²ªG¦s©ñ©óÅÜ¼Æ rs¡C- ¨ú±oÄæ¦ì¦WºÙ¤Î¤º®e¡GY¬OÀ˵ø¬d¸ß¡A§ÚÌ¥i¥H¨Ï¥Î¤U¦Cªºªº¤è¦¡¨Ó¨ú±oÄæ¦ì¦WºÙ¤Î¤º®eµ¥¸ê°T¡G
¨Ò¦p¡AYn¦L¥XÄæ¦ì¦WºÙ¡A¥i¥H¨Ï¥Î¤U¦C¨å«¬µ{¦¡½X¡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«ü¼Ð²¾¨ì³Ì«á¤@µ§
for (i=0; i Yn¦L¥X¨C¤@µ§¸ê®Æªº¨C¤@ÓÄæ¦ìÈ¡A¥i¥H¨Ï¥Î¤U¦C¨å«¬µ{¦¡½X¡G"); while (!rs.EOF){ for (i=0; i ¥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\n"); rs.MoveNext(); } - Ãö³¬ RecordSet ¤Î¸ê®Æ®w³sµ²¡G½d¨Òµ{¦¡½X¦p¤U¡G
rs.Close(); conn.Close(); ¤Wz½d¨Òªºì©lÀɦp¤U¡G
¦b¤Wz½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¡CYŪªÌ¹ï VBScript ¤ñ¸û¼ô±x¡A¤]¥i¥H¨Ï¥Î VBScript ¨Ó¶i¦æÃþ¦üªº¤u§@¡A¨ä¬yµ{§¹¥þ¤@¼Ë¡A¥i°Ñ¦Ò¦¹½d¨Ò¡Gasp/example/database/listdb01_vbs.asp¡C
Yn¨Ï¥Î 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¨Ò¦Ó¨¥¡AYn¥Ñ DSN ¨Ó³sµ²¸ê®Æ®w¡A¦Ó¤£ª½±µ«ü©w¸ê®Æ®w¡A¥un§â¤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¡GConn.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
¦b¤Wzì©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 ¨Ï¥Î¤Wzªº¨ç¼Æ¨Ó¶i¦æ¸ê®Æ®w¦Cªí¡Aµ{¦¡½X´N·|°®°®²b²b¡A½d¨Ò¦p¤U¡G
¨äì©l½X¦p¤U¡G
Y¨Ï¥Î VBScript¡A«h¥i¨£¦¹½d¨Ò¡Gasp/example/database/listdb03_vbs.asp
«ezªº½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¦]¦¹¥un¨Ï¥Î¾A·íªº SQL «ü¥O¡A¦A¥[¤W«ezªº¤èªk¡A§Y¥i¹ï¸ê®Æ®w¶i¦æ§¹¥þªº³B²z¡C
JScript µ{¦¡³]p»PÀ³¥Î¡G¥Î©ó¦øªA¾¹ºÝªº ASP Àô¹Ò