【问题标题】:VBScript - Database - Recordset - How to pass DateDiff value in the databaseVBScript - 数据库 - 记录集 - 如何在数据库中传递 DateDiff 值
【发布时间】:2012-05-17 19:46:12
【问题描述】:

我正在使用 ASP 的 DateDiff() 函数来查找两个日期之间的日期差。

该函数可以正常工作并显示两个日期之间的确切日期差异,但是在将这个值插入数据库时​​,它需要 9 作为值,而不考虑任何日期差异。

假设两个日期之间的差异超过 15 或 20 天,在数据库中需要“9”。

我使用 INT 作为显示日期差异的列的数据类型。

DATA TYPE 是否在这里造成问题? 我什至尝试使用会话变量来存储值,但没有运气 - 下面是我的代码:

if request.Form("sub") <> "" then
        sql = "Select * from emp_leave_details"
        rs.open sql , con, 1, 2
        dim diff
        dim todate
        dim fromdate            
        fromdate= rs("leave_from")
        todate= rs("leave_to")
        session("date_diff")=datediff("d",fromdate,todate)
        rs.addnew



        rs("emp_name") = request.Form("name")
        rs("emp_no") = request.Form("number")
        rs("address") = request.Form("address")
        rs("contact_no") = request.Form("contact")
        rs("mobile_no") = request.Form("mobile")
        rs("contact_onleave") = request.Form("contact_details")
        rs("leave_type") = request.Form("rad")
        rs("other_leave_details") = request.Form("PS")
        rs("leave_from") = request.Form("from")
        rs("leave_to") = request.Form("to")
        rs("applied_by") = request.Form("apply")
        rs("accepted_by") = request.Form("accept")
        rs("approved_by") = request.Form("approve")
        rs("no_of_leave_taken")= session("date_diff")
        rs.update

        response.Write("<script language='javascript'>{update();}</script>")

        rs.close
        end if

【问题讨论】:

  • 您正在从SELECT * FROM emp_leave_details 选择的第一个记录集中读取fromdatetodate ...这真的是有意的吗?看起来你总是使用相同的数据(只要 select * from 是一致的)。
  • @Filburt 谢谢你的回复......我不明白你想说什么......你能详细说明一下......
  • 现在如果我把我的查询作为这个' sql = "Select * from emp_leave_details where emp_name='"&session("Username")&"'" '进入数据库的值为 "14" .....无论日期差异如何......是
  • 当您进行查询时,您的leave_datetodate 始终取自数据集的第一条记录。 fromdate= rs("leave_from")todate= rs("leave_to") 应该改为 fromdate= request.form("leave_from")todate= request.form("leave_to") 吗?
  • @oracle 认证专业人士,你说得对,它占用了数据集中存在的第一条记录。我交叉检查了它,第一个条目的日期差异为“9”......这就是每次我提交新记录时它显示 9 的原因......但现在不是'fromdate = rs(“leave_from”)'和 'todate=rs("leave_to")' 如果我使用 'request.form' 代替 rs ...传递给数据库的值为零。

标签: asp-classic vbscript sql-server-2000


【解决方案1】:

数据类型与此无关。在会话中存储值不是解决方案。您可以使用常规变量。

从您的代码看来,您总是对fromdatetodate 使用相同的值。这是因为您没有迭代结果集中的行。

if not rs.bof and not rs.eof then
    do while not rs.eof

        '' code to execute for each row            

        rs.moveNext
    loop
end if

在您当前的脚本中,rs 将始终返回查询返回的第一行的结果。

您遇到的第二个问题可能是Date 数据类型。使用 cDate 将您的值转换为日期,并使用它来计算差异。

【讨论】:

  • 我怀疑迭代所有记录是 OP 正在寻找的 - 似乎他试图为当前登录的用户/员工创建新记录。
  • 在 OP 的 cmets 之后,这一点变得清晰起来。起初,这似乎是问题所在,根据 OP 的代码,这仍然是解决方案的一部分。
【解决方案2】:

您的问题是,您搜索“Select * from emp_leave_details”,它总是给出该表中的所有记录。您检索第一条记录的值并对它们进行比较,结果总是相同的值,这是正常的。从您的问题来看,您还不清楚您真正想要做什么。我想所以想选择一个像

这样的记录
Select * from emp_leave_details where emp_name=<%=request.Form("name")%>

并在此基础上添加一条带有计算出的 no_of_leave_taken 的新记录。

【讨论】:

    【解决方案3】:

    对不起,我的坏人... 这是我调用的数据库字段名称,而不是

    fromdate= request.form("from")
    todate= request.form("to")
    

    我叫这个

    fromdate= request.form("leave_from")
    todate= request.form("leave_to")
    

    再次抱歉..但我真的很感谢你们为我提供了所有可能的解决方案。

    谢谢。

    【讨论】:

      猜你喜欢
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-04
      • 1970-01-01
      • 2018-08-26
      相关资源
      最近更新 更多