纯HTML也能访问数据库
久学培训网--国内最大的培训平台! |
今天第一次看到,觉得很惊讶,感觉自己见识太少了:
Client.Html页面源码如下:
<html>
<head>
<title>get data from database at client</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<style type="text/css">
<!--
body{font-family:Times New Roman;font-size:9pt;}
table{font-family:Times New Roman;font-size:9pt;}
button{color:blue;}
//-->
</style>
<script language="Javascript">
<!--
function connectDB()
{
try{
var count=0;
var conn=new ActiveXObject("ADODB.Connection");
var rs=new ActiveXObject("ADODB.Recordset");
var connectString="provider=sqloledb.1;user id=sa;password=;data source=coolbug;Initial Catalog=pubs";
var selectString="select top 5 * from sales";
var tableString="";
conn.open(connectString);
rs=conn.execute(selectString);
while(!rs.EOF)
{
if(count++==0)
{
tableString+="<table border='1'><tr><td>"+rs(0).name+"</td>";
tableString+="<td>"+rs(1).name+"</td></tr>";
}
tableString+="<tr><td>"+rs(0).value+"</td>";
tableString+="<td>"+rs(1).value+"</td></tr>";
rs.moveNext();
}
tableString+="</table><br>";
document.getElementById("dataArea").innerHTML=tableString;
rs.close();
conn.close;
}
catch(e)
{
document.write(e.toString());
}
}
//-->
</script>
</head>
<body>
<div id="dataArea"></div>
<button onclick="connectDB();">retrieve data</button>
</body>
</html>
<head>
<title>get data from database at client</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<style type="text/css">
<!--
body{font-family:Times New Roman;font-size:9pt;}
table{font-family:Times New Roman;font-size:9pt;}
button{color:blue;}
//-->
</style>
<script language="Javascript">
<!--
function connectDB()
{
try{
var count=0;
var conn=new ActiveXObject("ADODB.Connection");
var rs=new ActiveXObject("ADODB.Recordset");
var connectString="provider=sqloledb.1;user id=sa;password=;data source=coolbug;Initial Catalog=pubs";
var selectString="select top 5 * from sales";
var tableString="";
conn.open(connectString);
rs=conn.execute(selectString);
while(!rs.EOF)
{
if(count++==0)
{
tableString+="<table border='1'><tr><td>"+rs(0).name+"</td>";
tableString+="<td>"+rs(1).name+"</td></tr>";
}
tableString+="<tr><td>"+rs(0).value+"</td>";
tableString+="<td>"+rs(1).value+"</td></tr>";
rs.moveNext();
}
tableString+="</table><br>";
document.getElementById("dataArea").innerHTML=tableString;
rs.close();
conn.close;
}
catch(e)
{
document.write(e.toString());
}
}
//-->
</script>
</head>
<body>
<div id="dataArea"></div>
<button onclick="connectDB();">retrieve data</button>
</body>
</html>
如果要访问别的数据库,就请修改,连接方式。
希望此次抛砖,能引玉。
阿里云优惠券领取地址:https://partner.aliyun.com/shop/35783975 ,需要更多优惠请联系QQ和微信:891115,热线:400-968-4968