日期:2014-05-16  浏览次数:20427 次

如何用JS实现访问sqlsever2005数据库?
请教,在网页中如何用JS实现访问sqlsever2005数据库里的信息?
JS   和数据库都在本地机器上。

------解决方案--------------------
<!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>