当然,如果从数据库取数据,我们只能用Recordset,但是增加或者修改或者删除数据,我们可以不用Recordset而选用Execute()方法。
<%@ language=javascript %>
var strConnect="Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
strConnect += Server.MapPath("//GOP") + "//datastores//gop.mdb;"
<!-- METADATA TYPE="typelib"
FILE="C:/Program Files/Common Files/System/ado/msado15.dll" -->
<HTML>
<HEAD>
<TITLE>Administrator Page - Changing the Mailing List</TITLE>
</HEAD>
<BODY LINK="red" VLINK="red" ALINK="crimson">
<H2>Administrator Page</H2>
<H3>Changing a the Mailing List</H3>
<%
if (Request.Form("Delete") > "")
{
var sql="DELETE FROM Address WHERE ID = " + Request.Form("ID") + ";"
}
else
{
var firstName = new String(Request.Form("firstName"))
var lastName = new String(Request.Form("lastName"))
var Address = new String(Request.Form("Address"))
var City = new String(Request.Form("City"))
var myRegExp = /[']/g;
firstName = firstName.replace(myRegExp, ''');
lastName = lastName.replace(myRegExp, ''');
Address = Address.replace(myRegExp, ''');
City = City.replace(myRegExp, ''');
var sql="UPDATE Address SET firstName= '" + firstName + "' , lastName='"
sql += lastName + "' , Address='" + Address + "' , City='"
sql += City + "' , State='" + Request.Form("State") + "' , Zip='"
sql += Request.Form("Zip") + "' WHERE ID = " + Request.Form("ID") + ";"
}
var objConn=Server.CreateObject("ADODB.Connection");
objConn.Open(strConnect)
objConn.Execute(sql)
objConn.Close()
objConn = null;
Response.Write("The member has been updated in the database.")
Response.Write("<A HREF=/"../files/committee.asp/">")
Response.Write("Click here to see it.</A>")
%>
也许已经注意到了,我们把单引号替换成了html编码的等同符号。这是因为出于安全考虑,单引号是唯一不能通过asp提交到数据库的符号。如果不替换掉单引号,那么不要接受用户提交的任何的文本文件。单引号可能是打开你整个数据库的关键。objConn.Execute(sql)是需要注意的一点。在这种情况下sql变量完成你我们需要做的一切。
另外需要注意的一点是,javascript是一种类型限制不严格的语言,而数据库则严格的多。尤其是时间参数。对于数据库来说时间格式一定是"mm/dd/yyyy hh:mm:ss"所以我们如果要把时间写入数据库,必须把时间改为这种形式,否则,数据库将不能按照时间顺序排列。一般的变化如下
function whatTimeIsIt()
{
var m=new Date()
var minute=m.getMinutes()
var second=m.getSeconds()
var ampm=false
if (minute >=0 && minute < 10)
{ minute=("0" + minute) }
if (second >= 0 && second < 10 )
{ second=("0" + second) }
var hours=m.getHours()
if (hours > 12)
{ ampm=true
hours=hours-12 }
if (hours==12)
{ ampm=true }
if (hours == 0)
{ hours=hours+12
ampm=false }
if (ampm)
{ ampm=" PM" }
else
{ ampm=" AM" }
var myTime=hours + ":" + minute + ":" + second + ampm
return myTime;
}
var DateTime = new Date();
var Month = (DateTime.getMonth() + 1) + "/";
var Day = DateTime.getDate() + "/";
var Year = DateTime.getFullYear();
var JustTheDate = Month + Day + Year;
var JustTheTime = whatTimeIsIt();
var DateTime = Month + Day + Year + " " + whatTimeIsIt();