数据库 ADO 示例

下列示例完成相同的内容:显示 MySql 数据库 database 中的表 table 中的全部记录内容。

1、VBScript 服务端脚本

<script language=vbscript runat=server>
      set cn=Server.CreateObject("ADODB.Connection") 
      set rec=Server.CreateObject("ADODB.Recordset") 
      database="asteras" 
      table="talkuser" 
      cn.Open "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=" & database & ";UID=root;PWD=;OPTION=3" 
      rec.Open "select * from " & table,cn,1,1 
      do while not rec.eof 
         j = rec.Fields.Count 
         for k=0 to j-1 
            Response.write rec(k) & " " 
         next 
         Response.write "<br>" 
         rec.MoveNext 
      loop 
      rec.Close 
      cn.Close 
</script> 

2、JScript 服务端脚本

<script language=jscript runat=server>

var cn = Server.CreateObject("ADODB.Connection");

var rec = Server.CreateObject("ADODB.Recordset");

var database = "asteras";

var table = "talkuser";

cn.Open("DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=" + database + ";UID=root;PWD=;OPTION=3");

rec.Open("select * from " + table,cn,1,1);

while( !rec.eof ){

   j = rec.Fields.Count;

   for( k = 0 ; k < j ; k++)

      Response.write ( rec( k ) + " " );

   Response.write ( "<br>" );

   rec.MoveNext();

}

rec.Close();

cn.Close();

</script>

3、PHP 服务端脚本

<?PHP

// 必须将 php.ini 中的 [com] com.allow_dcom 设为 TRUE

$cn = new COM("ADODB.Connection");

$rec = new COM("ADODB.Recordset");

$database = "asteras";

$table = "talkuser";

$cn->Open("DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=$database;UID=root;PWD=;OPTION=3");


$rec->Open("select * from $table",$cn,1,1);

while( ! $rec->eof ){

   $j = $rec->Fields->Count;

   for($k=0;$k<$j;$k++)

   {

      echo $rec->Fields[$k]->value." " ;

   }

   echo "<br>";

   $rec->MoveNext();

}

$rec->Close();

$cn->Close();

?>

4、VB

Function CopyData()

' 此段程序在 EXCEL2000 中运行

Set cn = CreateObject("ADODB.Connection")

Set rec = CreateObject("ADODB.Recordset")

database = "asteras"

Table = "talkuser"

cn.Open "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=" & database & ";UID=root;PWD=;OPTION=3"

rec.Open "select * from " & Table, cn, 1, 1

i = 0

Do While Not rec.EOF

   j = rec.Fields.Count

   For k = 0 To j - 1

      ActiveSheet.Cells(i + 1, k + 1) = rec(k)

   Next

   rec.MoveNext

   i = i + 1

Loop

rec.Close

cn.Close

End Function

5、VB 客户端脚本

<script language=vbscript>

Function GetData

Set cn = CreateObject("ADODB.Connection")

Set rec = CreateObject("ADODB.Recordset")

database = "asteras"

Table = "talkuser"

cn.Open "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=" & database & ";UID=root;PWD=;OPTION=3"

rec.Open "select * from " & Table, cn, 1, 1

i = 0

do while not rec.eof

   j = rec.Fields.Count

   for k=0 to j-1

      document.writeln rec(k) & " "

   next

   document.writeln "<br>"

   rec.MoveNext

loop

rec.Close

cn.Close

end function

</script>

注意:

1、对于日期和时间字段,PHP 转成 1970 年以来的秒数,JS 的输出格式需作调整。

2、JS 的输出单词 Response 大小写敏感。

3、PHP 只能用 $rec->Fields[$k]->value 语句取记录内容。

拟虚路径转绝对路径的函数:

VB:mappath(string)

PHP: realpath(string)