鲸刀 价格:ASP连接数据库问题~

来源:百度文库 编辑:高考问答 时间:2024/04/29 15:39:34
这是我的代码
<!--#include file="conn.inc"-->
<html>
<head>
<link rel="stylesheet" type="text/css" href="css.css">
</head>
<body>
<table border="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
<tr>
<td width="100%" height="30" style="border-left-style: solid; border-left-width: 0; border-right-style: solid; border-right-width: 0; border-top-style: solid; border-top-width: 0; border-bottom: 1px solid #000000">
<img border="0" src="images/selectsubject.jpg"></td>
</tr>
</table>
<%
response.cookies("downloadok")=""
sql1="select * from exam_testuser where havetest=0 and userid="& request.cookies("userid")
set rs1=server.createobject("adodb.recordset")
rs1.open sql1,conn,3,2
if rs1.eof then
%>
<table border="0" cellspacing="0" bordercolor="#111111" width="480">
<tr>
<td width="100%" height="30"> 现在您还没有需要参加考试的科目! </td>
</tr>
</table>
<%
else
i=1
%>
<table border="1" cellspacing="1" style="border-collapse: collapse" bordercolor="#C0C0C0" id="AutoNumber2" width="480" cellpadding="0">
<tr>
<td height="22" width="20" bgcolor="#E1E1E1"></td>
<td bgcolor="#E1E1E1"> <b>科目名称</b></td>
<td bgcolor="#E1E1E1" width="80">
<p align="center"><b>考试时间</b></td>
<td bgcolor="#E1E1E1" width="80">
<p align="center"><b>操作</b></td>
</tr>
<%
do while not rs1.eof
sql="select * from exam_test where '"&Date()&"' between starttime and endtime and not passuserid is null and testid="& rs1("testid")
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,3,2
if not rs.eof then
%>
<tr>
<td align="center" height="20" width="20" bgcolor="#E1E1E1"><%=i%></td>
<td bgcolor="#FFFFFF">
<p align="left"> <%=rs("subject")%></td>
<td bgcolor="#FFFFFF">
<p align="center"> <%=rs("testtime")%>小时</td>
<td bgcolor="#FFFFFF">
<p align="center"><a href="paper.asp?testid=<%=rs1("testid")%>">进入考场</a></td>
</tr>
<%
end if
rs1.movenext
i=i+1
loop
%>
</table>
<%
end if
%>
</body>
</html>
总是出现错误
Microsoft OLE DB Provider for SQL Server (0x80040E14)
第 1 行: '=' 附近有语法错误。
/exam/selectsubject.asp, 第 17 行

SQL语句的语法错误,这个程序中,由于你请求的cookies内容为空。你的SQL查询语句就成了sql1=\"select * from exam_testuser where havetest=0 and userid=\"。这个语句在SQL里面是错误的,等号两端必须要有参数或者表达式,等号右边如果为空则用一对单引号代替。你可以修改成sql1=\"select * from exam_testuser where havetest=0 and userid=\'\"& request.cookies(\"userid\") & \"\'\" 这样的话。即使cookies里请求的参数为空SQL语句则是:sql1=\"select * from exam_testuser where havetest=0 and userid=\'\'\"。而select * from exam_testuser where havetest=0 and userid=\'\'这条语句是被SQL承认的正确语法,因此不会报错。
另外,对于SQL语句来说,比较整形时,等号右边的值可以用单引号包含,也可以不用,没有比较值则用单引号单体;而比较字符型字段的时候,右边的值必须包含在一对单引号内,不然比较会出错,造成即使有匹配的数据,都检索不出来的情况

sql1="select * from exam_testuser where havetest=0 and userid="& request.cookies("userid")
应该是由于request.cookies("userid") 是空值引起的。
你应该在之前判断一下是不是空值再做处理
另外把语句改成如下的样子应该不会出错
sql1="select * from exam_testuser where havetest=0 and userid='"& request.cookies("userid") & "'"