<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>js连接数据库</title>
<script language="javascript" type="text/javascript">
var Num ;
var conn="provider=Sqloledb;server=.;uid=sa;pwd=sa;database=ZhuFTest";
var rs=new ActiveXObject("ADODB.recordset");
var sql="select GoodsID,GoodsName,Price,Num from Goods";
rs.Open(sql,conn);
function ConnectionSql(){
rs.Open(sql,conn,2,2);
document.getElementById("GID").value=rs("GoodsID");
document.getElementById("GName").value=rs("GoodsName");
document.getElementById("Pri").value=rs("Price");
document.getElementById("Num").value=rs("Num");
}
function Next(){//'下一条'
rs.MoveNext();
if(rs.EOF == true){
rs.MovePrevious();
alert("最后一条记录!");
return;
}
else{
document.getElementById("GID").value=rs("GoodsID");
document.getElementById("GName").value=rs("GoodsName");
document.getElementById("Pri").value=rs("Price");
document.getElementById("Num").value=rs("Num");
}
}
function First(){//'第一条'
rs.MoveFirst();
document.getElementById("GID").value=rs("GoodsID");
document.getElementById("GName").value=rs("GoodsName");
document.getElementById("Pri").value=rs("Price");
document.getElementById("Num").value=rs("Num");
}
function Last(){//'最后一条'
rs.MoveLast();
document.getElementById("GID").value=rs("GoodsID");
document.getElementById("GName").value=rs("GoodsName");
document.getElementById("Pri").value=rs("Price");
document.getElementById("Num").value=rs("Num");
}
function Per(){//'上一条'
rs.MovePrevious();
if(rs.BOF == true){
rs.MoveNext();
alert("第一条记录!");
return;
}
else {
document.getElementById("GID").value=rs("GoodsID");
document.getElementById("GName").value=rs("GoodsName");
document.getElementById("Pri").value=rs("Price");
document.getElementById("Num").value=rs("Num");
}
}
rs.Close();
</script>
</head>
<body onload="return ConnectionSql();">
<table>
<tr><td>商品编号:</td><td><input type="text" id="GID" /></td></tr>
<tr><td>商品名称:</td><td><input type="text" id="GName" /></td></tr>
<tr><td>商品价格:</td><td><input type="text" id="Pri" /></td></tr>
<tr><td>商品数量:</td><td><input type="text" id="Num" /></td></tr>
</table>
<input type="button" id="Next" value="下一条" onclick="return Next();"/>
<input type="button" id="First" value="First" onclick="return First();"/>
<input type="button" id="Button1" value="Last" onclick="return Last();"/>
<input type="button" id="Per" value="上一条" onclick="return Per();"/>
</body>
</html>这是操作ACCESS ,我想都差不多吧,个人觉得用JS操作数据库意义不大
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>js连接数据库</title>
<script language="javascript" type="text/javascript">
var Num ;
var conn="provider=Sqloledb;server=.;uid=sa;pwd=sa;database=ZhuFTest";
var rs=new ActiveXObject("ADODB.recordset");
var sql="select GoodsID,GoodsName,Price,Num from Goods";
rs.Open(sql,conn);
function ConnectionSql(){
rs.Open(sql,conn,2,2);
document.getElementById("GID").value=rs("GoodsID");
document.getElementById("GName").value=rs("GoodsName");
document.getElementById("Pri").value=rs("Price");
document.getElementById("Num").value=rs("Num");
}
function Next(){//'下一条'
rs.MoveNext();
if(rs.EOF == true){
rs.MovePrevious();
alert("最后一条记录!");
return;
}
else{
document.getElementById("GID").value=rs("GoodsID");
document.getElementById("GName").value=rs("GoodsName");
document.getElementById("Pri").value=rs("Price");
document.getElementById("Num").value=rs("Num");
}
}
function First(){//'第一条'
rs.MoveFirst();
document.getElementById("GID").value=rs("GoodsID");
document.getElementById("GName").value=rs("GoodsName");
document.getElementById("Pri").value=rs("Price");
document.getElementById("Num").value=rs("Num");
}
function Last(){//'最后一条'
rs.MoveLast();
document.getElementById("GID").value=rs("GoodsID");
document.getElementById("GName").value=rs("GoodsName");
document.getElementById("Pri").value=rs("Price");
document.getElementById("Num").value=rs("Num");
}
function Per(){//'上一条'
rs.MovePrevious();
if(rs.BOF == true){
rs.MoveNext();
alert("第一条记录!");
return;
}
else {
document.getElementById("GID").value=rs("GoodsID");
document.getElementById("GName").value=rs("GoodsName");
document.getElementById("Pri").value=rs("Price");
document.getElementById("Num").value=rs("Num");
}
}
rs.Close();
</script>
</head>
<body onload="return ConnectionSql();">
<table>
<tr><td>商品编号:</td><td><input type="text" id="GID" /></td></tr>
<tr><td>商品名称:</td><td><input type="text" id="GName" /></td></tr>
<tr><td>商品价格:</td><td><input type="text" id="Pri" /></td></tr>
<tr><td>商品数量:</td><td><input type="text" id="Num" /></td></tr>
</table>
<input type="button" id="Next" value="下一条" onclick="return Next();"/>
<input type="button" id="First" value="First" onclick="return First();"/>
<input type="button" id="Button1" value="Last" onclick="return Last();"/>
<input type="button" id="Per" value="上一条" onclick="return Per();"/>
</body>
</html>这是操作ACCESS ,我想都差不多吧,个人觉得用JS操作数据库意义不大
JavaScript直接访问XML,SQL2005的XML 或者自己用动态WEB语言生成XML供JavaScript访问。