注册 登录
编程论坛 ASP技术论坛

请教ASP代码的一个小问题!!

zjcwg 发布于 2007-10-09 13:17, 1244 次点击

ASP代码的一个新问题!!


sql="select * from ship where NewDate='"&NewDate&"'"
rs.open sql,conn,1,3
if not rs.eof or NewDate=WebName then
errmsg="<br>"+"<li>对不起,此天的船舶动态表已存在,请您修改或重新建立。"
founderr=true
else

rs.addnew
rs("Duty")=Duty
rs("NewDate")=NewDate

以上代码是本人编写的《船舶动态表》模块,作用是经每天登记的“船舶动态表”以日期为文件名显示在网页上,点击该日期,即可显示当天的动态表,并且限制每天只能登记一张表,出现相同日期(名字)时,系统提示错误信息。
其中:Duty是岗位,NewDate 是登记日期(也是用于显示当天动态表的文件名),在数据库的表中属性为“日期格式”,WebName是在代码中定义的文件名。

现在的问题是,数据库ship的表中NewDate属性为文本时,一切正常,但改为“日期格式”时,登记新表过程系统出错。
请问各位高手赐教!!

非常感谢!!

22 回复
#2
yms1232007-10-09 13:26
rs("NewDate")=CDate(NewDate)
强制转换为日期格式呢?
#3
zjcwg2007-10-09 13:36

还是不行。显示以下错误信息:

-----------------------------------------------

HTTP 500.100 - 内部服务器错误 - ASP 错误
Internet 信息服务

技术信息(适用于支持人员)
· 错误类型:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E07)
/yewu/YewuRegPost.asp, 第 31 行
· 浏览器类型:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
· 页:
POST 1621 bytes to /yewu/YewuRegPost.asp
· POST 数据:
Year=2007&Month=9&Duty=%C0%EE%CE%C4%B3%AC&NewDate=2007-9-17&Port1=&name1=&Country1=&Cate1=&zd1=&Intime1=&Outtime1=&Port2=&name2=&Country2=&Cate2=&zd2=&Intime2=&Outtime2=&Port3=&name3=&Country3=&Cate3= . . .
· 时间:
2007年10月9日, 13:29:28
· 详细信息:
Microsoft 支持

----------------------------------------------


其中的 31行就是以上代码中的第一行:

sql="select * from ship where NewDate='"&NewDate&"'"

#4
zjcwg2007-10-09 13:40
如何将此段代码做如下修改,可以登记新表。但我要限制登记与当天日期相同的新表,目的是为了每天只登记一张。

sql="select * from ship"
rs.open sql,conn,1,3

rs.addnew
rs("Duty")=Duty
rs("NewDate")=NewDate
......

#5
yms1232007-10-09 13:40
sql="select * from ship where NewDate='"&NewDate&"'"
NewDate是什么类型的?Date型还是String型?
#6
zjcwg2007-10-09 13:44
是Date 型,ACCESS表中该字段属性是日期,以前我设为文本时正常,改为日期就不行了。
#7
zjcwg2007-10-09 13:45

是Date 型,ACCESS表中该字段属性是日期,以前我设为文本时正常,改为日期就不行了。

将此段代码做如下修改,可以登记新表。但我要限制登记与当天日期相同的新表,目的是为了每天只登记一张。

sql="select * from ship"
rs.open sql,conn,1,3

rs.addnew
rs("Duty")=Duty
rs("NewDate")=NewDate
......

#8
zjcwg2007-10-09 13:55
请告诉我如何在以上代码中增加限制登记与当天日期相同的新表的功能,目的是为了每天只登记一张。
多谢版主!
#9
yms1232007-10-09 14:01
sql="select * from ship where NewDate='"&CStr(NewDate)&"'"
再不行加
sql="select * from ship where NewDate='#"&CStr(NewDate)&"#'"
比较某个日期是否为当天日期用DateDiff函数比如
IF DateDiff("d",Now,CDate("2007/10/9"))=0 Then
Response.Write "2007年10月9日为当天日期"
End IF
#10
zjcwg2007-10-09 14:37
不好意思,可能我讲的意思不清楚,让版主误解了。我的意思是如果库中已经存在了某天的表,就要限制再新建与此天日期相同的表。以确保库中每天只登记一张表。
#11
yms1232007-10-09 15:25
NewDate是用户输入的时间?
#12
zjcwg2007-10-09 15:35
对,这个时间就是动态表在网页上显示的标题,点击该标题可以打开查看表的内容。为了防止出现两张相同标题(时间)的表,必须实行限制。
#13
yms1232007-10-09 15:37
WebName也是时间类型的变量?
#14
zjcwg2007-10-09 15:38
我在第一次提供的代码是NewDate在数据库中字段属性为"文本"时的代码,可以实现限制功能。但将NewDate改为"日期"属性后,就不行了。
#15
yms1232007-10-09 15:42
if not rs.eof or DateDiff("d",NewDate,CDate(WebName))=0 then
把判断语句改成这样试试。
#16
zjcwg2007-10-09 16:11

还是不行。如果不要WebName这个变量是否可以实现,因为WebName在数据库中不存在,只是在网页信息列表显示时定义为标题而已。我原来设计的原意是:如果NewDate与已有的信息标题相同时,阻止新表登记。现在的问题很明显,就是NewDate 与 WebName的属性已经不同(一个是日期型,一个是文本型),不能比较。

列表显示代码:
<a href="#" onClick="MM_openBrWindow('shiplist.asp?id=<%=rs("id")%>','','width=700,height=500,scrollbars=yes')"><%=rs("NewDate")%></a>

在页面显示:YYYY-MM-DD

点击日期,就可以显示出表shiplist.asp页面。

[此贴子已经被作者于2007-10-9 16:25:07编辑过]

#17
yms1232007-10-09 16:30
WebName存储的是什么样的字符串,字符串也可以转成日期型的变量
但是必须是如"2007/9/10"或"2007-9-10 12:00:00"这样的字符串能转换
而里面如果有其他字符比如"2007/09/10 日期"这种会报错。
#18
zjcwg2007-10-09 16:39
应该没有存储什么,因为在数据库中不存在这个变量。如果不要这个变量行不行?
#19
zjcwg2007-10-09 16:49
我将两个相关联的文件发给你看看。

[此贴子已经被作者于2007-10-11 14:27:46编辑过]

#20
天涯听雨2007-10-09 16:57
NewsDate=FormatDate(NewsDate,0)

'=================================================================================================
'该函数作用:按指定参数格式化显示时间。
'numformat=1:将时间转化为yyyy-mm-dd hh:nn格式。
'numformat=2:将时间转化为yyyy-mm-dd格式。
'numformat=3:将时间转化为hh:nn格式。
'numformat=4:将时间转化为yyyy年mm月dd日 hh时nn分格式。
'numformat=5:将时间转化为yyyy年mm月dd日格式。
'numformat=6:将时间转化为hh时nn分格式。
'numformat=7:将时间转化为yyyy年mm月dd日 星期×格式。
'numformat=8:将时间转化为yymmdd格式。
'numformat=9:将时间转化为mmdd格式。
Function FormatDate(shijian,numformat)
dim ystr,mstr,dstr,hstr,nstr '变量含义分别为年字符串,月字符串,日字符串,时字符串,分字符串

if isnull(shijian) then
numformat=0
else
ystr=DatePart("yyyy",shijian)

if DatePart("m",shijian)>9 then
mstr=DatePart("m",shijian)
else
mstr="0"&DatePart("m",shijian)
end if

if DatePart("d",shijian)>9 then
dstr=DatePart("d",shijian)
else
dstr="0"&DatePart("d",shijian)
end if

if DatePart("h",shijian)>9 then
hstr=DatePart("h",shijian)
else
hstr="0"&DatePart("h",shijian)
end if

if DatePart("n",shijian)>9 then
nstr=DatePart("n",shijian)
else
nstr="0"&DatePart("n",shijian)
end if

if DatePart("s",shijian)>9 then
sstr=DatePart("s",shijian)
else
sstr="0"&DatePart("s",shijian)
end if
end if

select case numformat
case 0
FormatDate=ystr&"-"&mstr&"-"&dstr&" "&hstr&":"&nstr&":"&sstr
case 1
FormatDate=ystr&"-"&mstr&"-"&dstr&" "&hstr&":"&nstr
case 2
FormatDate=ystr&"-"&mstr&"-"&dstr
case 3
FormatDate=hstr&":"&nstr
case 4
FormatDate=ystr&"年"&mstr&"月"&dstr&"日 "&hstr&"时"&nstr&"分"
case 5
FormatDate=ystr&"年"&mstr&"月"&dstr&"日"
case 6
FormatDate=mstr&"月"&dstr&"日 "&hstr&"时"&nstr&"分"
case 7
FormatDate=ystr&"年"&mstr&"月"&dstr&"日 "&WeekdayName(Weekday(shijian))
case 8
FormatDate=right(ystr,2)&mstr&dstr
case 9
FormatDate=mstr&dstr
end select
End Function
#21
zjcwg2007-10-09 17:09
谢谢你的帮助。但我现在的问题不是在日期的转换。而是如何将当前要登记的新表与库中已有的内容进行判断,确定是否可以登记。以确保每天只能登记一张表。判断的要素是日期(也是在页面中显示的信息标题)。
#22
天涯听雨2007-10-09 17:18
NewsDate=FormatDate(Date(),0)


'mssql2000
if not(Conn.exeCute("select top 1 * from [ship] where NewDate='"&NewsDate&"'").eof then
errmsg="<br>"+"<li>对不起,此天的船舶动态表已存在,请您修改或重新建立。"
end if

'Access
if not(Conn.exeCute("select top 1 * from [ship] where NewDate='#"&NewsDate&"#'").eof then
errmsg="<br>"+"<li>对不起,此天的船舶动态表已存在,请您修改或重新建立。"
end if
#23
zjcwg2007-10-11 14:23

这段代码的目的是为了判断数据库(ship)中的NewDate是否存在相同,如果存在,即提示“对不起,此天的船舶动态表已存在,请您修改或重新建立”的错误信息,如果没有,即写入新的登记表信息。目的是为了阻止与当天相同的表被写入库中,以达到限制一天内出现相同的登记表。现在老是出错,请问错在哪??NewDate是日期型字段,由用户输入的日期,同时也是在网页中显示的信息标题,通过点击Newdate,可显示一张数据报表。

<%
founderr=false

if request.form("Duty")="" then
errmsg=errmsg+"<br>"+"<li>请输入值班人员名字!"
founderr=true
else
Duty=trim(request.form("Duty"))
end if
if request.form("NewDate")="" then
errmsg=errmsg+"<br>"+"<li>请调整时间格式!"
founderr=true
else
NewDate=trim(request.form("NewDate"))
end if

if founderr=true then
call error()
else

sql="select * from ship where NewDate='"&NewDate&"'"
rs.open sql,conn,1,3
if not rs.eof or NewDate=NewDate then
errmsg="<br>"+"<li>对不起,此天的船舶动态表已存在,请您修改或重新建立。"
founderr=true
else

rs.addnew

rs("Duty")=Duty
rs("NewDate")=NewDate

........




[此贴子已经被作者于2007-10-11 14:39:38编辑过]

1