有人小看js小脚本程序,认为那只是客户端,我不服!~~写了两条服务器端的出来作证明:
以下是进数据库accessr的,表名叫客户,数据库文件是asp\customer.mdb各字段自己看,
文件名:adddatabase.asp
注意页头声名脚本是javascript
var CompanyName=Request("CompanyName");
var CName=Request("CName");
var City=Request("City");
var Tel=Request("Tel");
if(CompanyName==""||CName==""||City==""||Tel==""){
Response.Write("<html><body><center><font size=5>请将资料填写完整。</font></center></body></html>");
Response.End;
};
var dbconnection = new ActiveXObject("ADODB.Connection");
dbconnection.ConnectionString="DRIVER={Microsoft Access Driver (*.mdb)};DBQ="+Server.MapPath("asp/customer.mdb");
dbconnection.open();
var rs = new ActiveXObject("ADODB.Recordset");
rs.Open ("客户",dbconnection,1, 3);
rs.AddNew;
rs("公司名称") = CompanyName;
rs("联络人姓名") = CName;
rs("所在城市") = City;
rs("电话号码") = Tel;
rs.Update;
Response.Write ("ok");
Response.End;
%>
然后提交的页是:
<form method="post" action="adddatabase.asp">
<input type="text" name="CompanyName"><br>
<input type="text" name="CName"><br>
<input type="text" name="City"><br>
<input type="text" name="Tel"><br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="reset" value="Reset">
</form>
然后,有一个显示页读出数据
注意页头声名是javascript
var conn = new ActiveXObject("ADODB.Connection");
var rs = new ActiveXObject("ADODB.RecordSet");
conn.ConnectionString = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + Server.MapPath("asp/customer.mdb");
conn.open();
var sql = "select * from 客户";
rs.open(sql, conn, 1, 1);
if(!rs.EOF) {
while(!rs.EOF) {
Response.Write("标题:" + rs("公司名称") + "<hr>");//这里只读一个,能证明js能读数据就ok~~~
rs.MoveNext();
}
}
同样当你要读取数据库某部份信息,就是用where
代码如下:
var dbconnection = new ActiveXObject("ADODB.Connection");
dbconnection.ConnectionString="DRIVER={Microsoft Access Driver (*.mdb)};DBQ="+Server.MapPath("asp/customer.mdb");
dbconnection.open();
var SQLQuery = "SELECT * FROM 客户 Where 公司名称='你好'";
var rsRecordset = new ActiveXObject("ADODB.Recordset");
rsRecordset.Open(SQLQuery,dbconnection,1,3);
while(!rsRecordset.EOF) {
Response.Write("标题:" + rsRecordset("公司名称") + "<hr>");
rsRecordset.MoveNext();
}//此代码在xp,iis5.0环境下运行通过
昨晚又改了一条用js珊除数据记录的js代码,加倍完善操作数据库的应用!分享一下:
var conn = new ActiveXObject("ADODB.Connection");
conn.ConnectionString="DRIVER={Microsoft Access Driver (*.mdb)};DBQ="+Server.MapPath("asp/customer.mdb");
conn.open();
var rs=new ActiveXObject("ADODB.Recordset");
var sqltext="delete from 客户 where 公司名称='"+Request("textfield")+"'"
rs.open(sqltext,conn,3,3);
rs.nothing;
conn.close;
Response.Write("珊除-->"+Request("textfield")+"<---完成");
[此贴子已经被作者于2006-7-13 11:59:09编辑过]