javascript:连接数据库
javascript:连接数据库
<script language="javascript">
//用 JavaScript 写服务器端连接数据库的代码示例
var conn = new ActiveXObject("ADODB.Connection");
conn.Open("Provider=SQLOLEDB.1; Data Source=localhost; User ID=sa; "
+"Password=; Initial Catalog=pubs");
var rs = new ActiveXObject("ADODB.Recordset");
var sql="select * from authors";
rs.open(sql, conn);
shtml = "<table width='100%' border=1>";
shtml +="<tr bgcolor='#f4f4f4'><td>au_id</td><td>au_lname</td><td>au_fname</td><td>phone</td><td>address</td><td> city</td><td>state</td><td>zip</td></tr>";
while(!rs.EOF)
{
shtml += "<tr><td>" + rs("au_id") + "</td><td>" + rs("au_lname") + "</td><td>" + rs("au_fname") + "</td><td>" + rs("phone") + "</td><td>" + rs("address") + "</td><td>" + rs("city") + "</td><td>" + rs("state") + "</td><td>" + rs("zip") + "</td></tr>";
rs.moveNext;
}
shtml += "</table>";
document.write(shtml);
rs.close();
rs = null;
conn.close();
conn = null;
</script>
//用 JavaScript 写服务器端连接数据库的代码示例
var conn = new ActiveXObject("ADODB.Connection");
conn.Open("Provider=SQLOLEDB.1; Data Source=localhost; User ID=sa; "
+"Password=; Initial Catalog=pubs");
var rs = new ActiveXObject("ADODB.Recordset");
var sql="select * from authors";
rs.open(sql, conn);
shtml = "<table width='100%' border=1>";
shtml +="<tr bgcolor='#f4f4f4'><td>au_id</td><td>au_lname</td><td>au_fname</td><td>phone</td><td>address</td><td> city</td><td>state</td><td>zip</td></tr>";
while(!rs.EOF)
{
shtml += "<tr><td>" + rs("au_id") + "</td><td>" + rs("au_lname") + "</td><td>" + rs("au_fname") + "</td><td>" + rs("phone") + "</td><td>" + rs("address") + "</td><td>" + rs("city") + "</td><td>" + rs("state") + "</td><td>" + rs("zip") + "</td></tr>";
rs.moveNext;
}
shtml += "</table>";
document.write(shtml);
rs.close();
rs = null;
conn.close();
conn = null;
</script>