【问题标题】:Problems with runnig UTF-8 encoded sql files in Classic ASP在经典 ASP 中运行 UTF-8 编码的 sql 文件的问题
【发布时间】:2016-01-15 12:29:19
【问题描述】:

我有一堆带有查询的 sql 文件,每次这些文件发生一些更改时,这些文件不需要在我的本地数据库上运行。 我们使用经典的 ASP 代码在 VBScript 上编写的循环中运行这些文件,如下所示:

Dim arrSqlLines()
i = 0
set t = fso.OpenTextFile(filePath, 1, false)
Do Until t.AtEndOfStream
    Redim Preserve arrSqlLines(i)
    arrSqlLines(i) = t.ReadLine
    i = i + 1
Loop
t.close

For Each sqlLine in arrSqlLines
    sqlLine = Trim(sqlLine)
    sqlBatch = Trim(sqlBatch & sqlLine) & vbCrLf
    Call dbexecConnection(sqlBatch, objDbConnection)
Next

Function dbexecConnection(sql, objConnExec)
    dim cmdTemp
    on Error Resume Next
    set cmdTemp=Server.CreateObject("ADODB.Command")
    set cmdTemp.ActiveConnection=objConnExec
    cmdTemp.CommandText = sql

    cmdTemp.Execute
    if err.number<>0 Then
        if Session("SkipError") <> -1 Then
            response.write "Error in dbexecute: " & sql & "<br/>"
            response.write "Error=(" & err.description & ")"
            response.End
        end If
    end If
    on error goto 0
End Function

问题是,如果一个sql文件以UTF-8 without BOM编码,它运行正常,但是如果任何文件以UTF-8格式编码,它就会产生错误:

例如,这个非常 sql 文件的开头是这样的:

SET QUOTED_IDENTIFIER ON
GO

IF OBJECT_ID('RPM_GET_ACTUAL_COST_FOR_EPIC') IS NOT NULL
    DROP Function [RPM_GET_ACTUAL_COST_FOR_EPIC]
GO

CREATE Function [RPM_GET_ACTUAL_COST_FOR_EPIC]
(
    @EpicID as int,
    @RateType as Int, -- 1 for Blended, 2 for CostCenter
    @CalcType as Int -- 1 for by Hours or 2 for Points
)
returns float
BEGIN

declare @Cost float
declare @CostX float

declare @ItStorys TABLE (
    StorylD int,
    State int,
    DemoStatus int,
    Dfficulty int,
    Feature int,
    TeamID int,
    TrackType int,
    Iteration int
    )

insert into @tStorys(StoryID, State, DemoStatus, Dfficulty, Feature, TeamID, TrackType, Iteration) 

我不能保证所有文件都将以UTF-8 without BOM 编码,因此我必须找到使其正确运行的方法,以及UTF-8 文件。我怎么可能这样做?

【问题讨论】:

    标签: sql-server encoding utf-8 vbscript asp-classic


    【解决方案1】:

    可以肯定的是,FileSystemObject 不处理 UTF-8,而是处理 Unicode 和 ANSI。
    ADODB.Stream 可以处理很多字符集,包括 utf-8,因此您可以使用它来代替。
    将您的代码替换为第一个 For 以下内容。

    Dim arrSqlLines
    With Server.CreateObject("Adodb.Stream")
        .Charset = "utf-8"
        .Open
        .LoadFromFile filePath
        If .EOS Then
            'an empty array if file is empty
            arrSqlLines = Array()
        Else
            'to obtain an array of lines like you desire
            'remove carriage returns (vbCr) if exist 
            'and split the text by using linefeeds (vbLf) as delimiter
            arrSqlLines = Split(Replace(.ReadText, vbCr, ""), vbLf)
        End If
        .Close
    End With
    

    【讨论】:

      【解决方案2】:

      只需要告诉 Classic ASP 和 IIS 您想要使用 UTF-8 处理 ASP 页面。

      首先保存 ASP 页面,确保它是使用代码页 65001 保存的(如果使用 Visual Studio,这可以通过 Advanced Save Options 菜单选项完成)

      然后修改 ASP 页面以包含以下初始行。

      <%@Language="VBScript" CodePage = 65001 %>
      <%
      'Assuming this is the whole script the above line MUST always
      'be the very first line in the source file.
      'Tell ASP strings should be returned UTF-8 encoded.
      Response.CodePage = 65001
      'Tell the Browser to expect UTF-8 encoded data.
      Response.Charset = "UTF-8"
      
      Dim arrSqlLines()
      i = 0
      set t = fso.OpenTextFile(filePath, 1, false)
      Do Until t.AtEndOfStream
          Redim Preserve arrSqlLines(i)
          arrSqlLines(i) = t.ReadLine
          i = i + 1
      Loop
      t.close
      
      For Each sqlLine in arrSqlLines
          sqlLine = Trim(sqlLine)
          sqlBatch = Trim(sqlBatch & sqlLine) & vbCrLf
          Call dbexecConnection(sqlBatch, objDbConnection)
      Next
      
      Function dbexecConnection(sql, objConnExec)
          dim cmdTemp
          on Error Resume Next
          set cmdTemp=Server.CreateObject("ADODB.Command")
          set cmdTemp.ActiveConnection=objConnExec
          cmdTemp.CommandText = sql
      
          cmdTemp.Execute
          if err.number<>0 Then
              if Session("SkipError") <> -1 Then
                  response.write "Error in dbexecute: " & sql & "<br/>"
                  response.write "Error=(" & err.description & ")"
                  response.End
              end If
          end If
          on error goto 0
      End Function
      %>
      

      有用的链接

      【讨论】:

      • 问题在于以 BOM 开头的 sql 命令失败。更改 CodePage 或保存 UTF-8 编码的 ASP 文件不会防止 SQL 语法错误。
      猜你喜欢
      • 2010-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多