字段检索顺序改变数据

时间:2015-01-20 20:30:35

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

在这里有一个让我脱掉头发的人:

如果我使用以下代码:

SQL = "SELECT * from Incident WHERE"
SQL = SQL & " active=1 ORDER BY incidentdate"
Recordset.Open SQL,Connection
Do While NOT Recordset.Eof
    idate = FormatDateTime(Recordset("incidentdate"),vbShortDate)
    inumber = Recordset("incidentnum")

    Response.Write inumber & "<br />" & vbCrLf
    Response.Write idate & vbCrLf
    Recordset.MoveNext     

Loop

我得到以下输出:

2015-U1-FO-001
01/17/2015

如果我在Do While...行后面切换行,那么我的代码就是这样:

SQL = "SELECT * from Incident WHERE"
SQL = SQL & " active=1 ORDER BY incidentdate"
Recordset.Open SQL,Connection
Do While NOT Recordset.Eof
    inumber = Recordset("incidentnum")
    idate = FormatDateTime(Recordset("incidentdate"),vbShortDate)

    Response.Write inumber & "<br />" & vbCrLf
    Response.Write idate & vbCrLf
    Recordset.MoveNext     

Loop

我的输出更改为:

2015-U1-FO-001
12/30/1899

这似乎导致Recordset("incidentdate")返回一个空字符串。

关于我所忽视的任何想法?

1 个答案:

答案 0 :(得分:3)

我不相信上述任何内容。

  1. 光标可能会限制更改的移动,可写性和可见性,但绝不会是否可以读取当前记录的字段(按任何顺序)。
  2. .Fields集合可以使用数字名称编制索引。在第二种情况下,SELECT语句中的(可能的)字段名顺序并不重要。
  3. 证据:

    Option Explicit
    
    Dim sCS : sCS     = "Provider=SQLOLEDB;User ID=...;Password=...;Initial Catalog=Trials;Data Source=WINXPSP3\SQLEXPRESS;"
    Dim oDb : Set oDb = CreateObject("ADODB.Connection")
    oDb.Open sCS
    Dim oRs : Set oRs = CreateObject("ADODB.Recordset")
    oRs.Open "SELECT TOP 3 * FROM [Alpha]", oDb
    WScript.Echo oRs.CursorType, "adOpenForwardOnly 0 Default. Uses a forward-only cursor"
    WScript.Echo oRs.GetString(2, , vbTab, vbCrLf, "<Null>")
    oRs.Close
    WScript.Echo "-----------"
    oRs.Open "SELECT TOP 3 * FROM [Alpha]", oDb
    WScript.Echo oRs("StartDate")
    WScript.Echo oRs("Id")
    oRs.Close
    WScript.Echo "-----------"
    oRs.Open "SELECT TOP 3 * FROM [Alpha]", oDb
    WScript.Echo oRs("Id")
    WScript.Echo oRs("StartDate")
    oRs.Close
    WScript.Echo "-----------"
    oRs.Open "SELECT TOP 3 * FROM [Alpha]", oDb
    WScript.Echo oRs("StartDate")
    WScript.Echo oRs("Id")
    WScript.Echo oRs("Id")
    WScript.Echo oRs("StartDate")
    oRs.MoveNext
    WScript.Echo "..........."
    WScript.Echo oRs("StartDate")
    WScript.Echo oRs("Id")
    oRs.MoveNext
    WScript.Echo "..........."
    WScript.Echo oRs("Id")
    WScript.Echo oRs("StartDate")
    oRs.Close
    oDb.Close
    

    输出:

    cscript 28054592.vbs
    0 adOpenForwardOnly 0 Default. Uses a forward-only cursor
    209     10.06.2002      11.02.2003      2090
    418     28.04.2000      10.06.2000      4180
    627     03.09.2000      01.11.2000      6270
    
    -----------
    10.06.2002
    209
    -----------
    209
    10.06.2002
    -----------
    10.06.2002
    209
    209
    10.06.2002
    ...........
    28.04.2000
    418
    ...........
    627
    03.09.2000