Web SQL查询未返回与SQL Enterprise Manager查询相同的行数

时间:2019-02-15 00:26:12

标签: internationalization

我想弄个脑筋急转弯。我的网页向本地SQL数据库发出查询,并返回2行。问题是,当我将完全相同的查询直接复制并粘贴到SQL Enterprise Manager中时,它(正确)返回3行! 我的问题是-网络版本的查询如何仅返回2行。 某些背景-所读取的表包含带有英语单词的varchar字段和一个保存与中文等效的nvarchar字段。我用于执行查询的代码是我使用多年的库中的通用代码,因此我认为不一定是用于执行查询的代码。

这是我的网页上加载记录的代码。在记录集最初被加载的那一刻,我检查了记录集的行计数,它返回的值是1(从零开始,因此记录集中有2条记录)。

================================================ ==================

public function LoadByQuery(ByVal sQueryType, ByVal sQueryValue, ByVal sSortOrder, ByVal sDatabase)
    if IsOnErrorResumeNextActive() then on error resume next
        LoadByQuery=false

    mytrace("LoadByQuery")

    ' free any existing elements
    Cleanup()
    'dbconnfix
    'dim sSQL,rsSQL
    dim SQLConn,sSQL,rsSQL,sDSN
    dim arrData,nMarketingColCount,nMarketingRowCount,x,bRet

    nMarketingColCount=-1
    nMarketingRowCount=-1

    select case sQueryType
        case MARKETINGMGR_PARMTYPE_BYCLAUSE _
                    sSQL = "SELECT id,guid,store,customerid,employeeid,filetype," & _
                                   "creationdate,filename,filesite,filesize,friendlyname,md5hash,folder,description,descriptionchinese,brand,brandchinese,productid,views,siteuploaded,approved,tags,tagschinese " & _
                   "FROM " & MARKETING_TABLE_NAME & " "
           if sQueryValue<>"" then sSQL = sSQL & " WHERE " & sQueryValue
           if sSortOrder<>"" then sSQL = sSQL & " ORDER BY " & sSortOrder
        case MARKETINGMGR_PARMTYPE_BYQUERY sSQL = sQueryValue
        case else _
            sLastErrorMsg="Error: invalid sQueryType passed to LoadByQuery: " & sQueryType
            mytrace(sLastErrorMsg)
            exit function
    end select

    err.clear
    'dbconnfix
    ' Create the ADO objects
    Set SQLConn = Server.CreateObject("ADODB.Connection")

    ' fetch the connection string to the SQL database
    select case lcase(sDatabase)
        case POWERMAX_DATABASE
            sDSN=GetPowerMaxDSNString()
        case else
            response.write("ERROR: Database not specied in CMarketingMgr::LoadByQuery")
            response.end
    end select

    ' Open the SQL database
    SQLConn.Open sDSN
    if Err.Number <> 0 then
        sLastErrorMsg = "Error opening SQL Connection using DSN " ' & sDSN & " Err.Number = " & CStrIfNull(Err.Number) & " Err.Description = " & Err.Description
        mytrace(sLastErrorMsg)
        set SQLConn=nothing
        exit function
    end if

    Set rsSQL = Server.CreateObject("ADODB.Recordset")

    mytrace(sSQL)

    rsSQL.Open sSQL,SQLConn
    ' able to open the recordset?
    if err.number<>0 then 
        sLastErrorMsg="Error: Unable to open recordset " & err.brand
        mytrace(sLastErrorMsg)
        exit function
    else
        if rsSQL.EOF=false then 
            ' items found: GetRows, and get row and column counts
            arrData = rsSQL.GetRows()
            nMarketingColCount=ubound(arrData,1)
            nMarketingRowCount=ubound(arrData,2)

        end if ' if rsSQL.EOF
    end if ' if err.number<>0

    rsSQL.close
    set rsSQL=nothing
    'dbconnfix
    SQLConn.close
    set SQLConn=nothing

    dim y

    ' customerid,employeeid, emailaddress, password
    ' Create new items from db
    dim s_id                        ' id of the db record
    dim s_guid
    dim s_store
    dim s_customerid                        ' Marketing customerid
    dim s_employeeid
    dim s_filetype
    dim s_creationdate          
    dim s_filename          
    dim s_filesite          
    dim s_filesize          
    dim s_friendlyname          
    dim s_md5hash           
    dim s_folder            
    dim s_description           
    dim s_descriptionchinese            
    dim s_brand         
    dim s_brandchinese          
    dim s_productid 
    dim s_views         
    dim s_siteuploaded          
    dim s_approved          
    dim s_tags  
    dim s_tagschinese   

    dim arr,sDate,sTime

    mytrace("nMarketingRowCount=" & nMarketingRowCount)
    ' Create new items and add to our array
    if nMarketingRowCount <>-1 then 
        ' for each record, add to our internal table
        for x = 0 to nMarketingRowCount
        ' add to internal table

            y=0

            ' globals
            s_id                        = CStrZeroIfNull(arrData(y,x)) : y=y+1 
            s_guid                      = CStrIfNull(arrData(y,x)) : y=y+1
            s_store                     = CStrIfNull(arrData(y,x)) : y=y+1
            s_customerid                = CStrZeroIfNull(arrData(y,x)) : y=y+1
            s_employeeid                = CStrZeroIfNull(arrData(y,x)) : y=y+1
            s_filetype                  = CStrIfNull(arrData(y,x)) : y=y+1
            s_creationdate              = CStrIfNull(arrData(y,x)) : y = y + 1 
            s_filename                  = CStrIfNull(arrData(y,x)) : y = y + 1 
            s_filesite                  = CStrIfNull(arrData(y,x)) : y = y + 1 
            s_filesize                  = CStrZeroIfNull(arrData(y,x)) : y = y + 1 
            s_friendlyname              = CStrIfNull(arrData(y,x)) : y = y + 1 
            s_md5hash                   = CStrIfNull(arrData(y,x)) : y = y + 1 
            s_folder                    = CStrIfNull(arrData(y,x)) : y = y + 1 
            s_description               = CStrIfNull(arrData(y,x)) : y = y + 1 
            s_descriptionchinese        = CStrIfNull(arrData(y,x)) : y = y + 1 
            s_brand                     = CStrIfNull(arrData(y,x)) : y = y + 1 
            s_brandchinese              = CStrIfNull(arrData(y,x)) : y = y + 1 
            s_productid                 = CStrZeroIfNull(arrData(y,x)) : y = y + 1 
            s_views                     = CStrZeroIfNull(arrData(y,x)) : y = y + 1 
            s_siteuploaded              = CStrIfNull(arrData(y,x)) : y = y + 1 
            s_approved                  = SetDefaultBitValue(arrData(y,x)) : y = y + 1 
            s_tags                      = CStrIfNull(arrData(y,x)) : y = y + 1 
            s_tagschinese               = CStrIfNull(arrData(y,x)) : y = y + 1 

            '-----------------------------------------------
            mytrace("s_creationdate: " & s_creationdate)
            arr = split(s_creationdate," ")
            sDate = arr(0)
            sTime="00:00:00" : if ubound(arr)>0 then sTime = arr(1)
            if isnumeric(Right(sDate,4)) then sDate = ConvertMMDDYYYYToYYYYMMDD(sDate)
            s_creationdate = sDate & " " & sTime
            mytrace("s_creationdate: " & s_creationdate)

            ' add the item to the array
            mytrace("AddToMarketingArray: id=" & s_id)
            if AddToMarketingArray(s_id,s_guid,s_store,s_customerid,s_employeeid,s_filetype,_
                s_creationdate,s_filename,s_filesite,s_filesize,s_friendlyname,s_md5hash,s_folder,s_description,s_descriptionchinese,s_brand,s_brandchinese,s_productid,_
                s_views,s_siteuploaded,s_approved,s_tags,s_tagschinese)=false then
                exit function
            end if

        next
    end if ' if nMarketingRowCount <>-1 

    LoadByQuery=true


end function

================================================ ================== 这是我从网页上的查询显示复制并粘贴到SQL Enterprise Manager中的实际查询:

SELECT id,guid,store,customerid,employeeid,filetype,creationdate,filename,filesite,filesize,friendlyname,md5hash,folder,description,descriptionchinese,brand,brandchinese,productid,views,siteuploaded,approved,tags,tagschinese FROM MarketingCenter WHERE approved=1 AND filetype=N'C' AND (brand='Aveeno' OR brandchinese=N'艾维诺') ORDER BY filetype

网站记录集仅包含2行,但是企业管理器查询返回了3行。查询的网站版本不包含将brandchinese设置为N'艾维诺'的记录,而企业管理器查询则包含。

我认为这可能与页面标题信息有关,但是页面显示汉字就好了。这是我页面上的标题:

<%@LANGUAGE="VBSCRIPT" CodePage = 65001%>
<%
Option Explicit
Session.CodePage = 65001
Response.charset ="utf-8"
Session.LCID     = 1033 'en-US
%>

有人想到即使企业管理器从同一查询返回记录时,为何在我的网页调用记录集中也不会返回包含汉字的记录?

1 个答案:

答案 0 :(得分:0)

好的,不确定这是否真的“回答了”问题-但是我发现通过Server.URLEncode将汉字传递到另一页时,在目标页读取并随后在SQL查询中使用时会改变汉字。 通过简单地将汉字附加到URL即可将汉字传递到目标页面(不使用Server.URLEncode),目标页可以正确读取汉字,并在SQL查询中使用时返回期望的行。 这是我第一次遇到这样的事情,我想知道为什么会这样,但是现在,对我来说,解决方法是在构建超链接时从中文字符中删除Server.URLEncode。