Delphi7中存储unicode的BUG

2016-02-19 19:56 45 1 收藏

今天图老师小编给大家精心推荐个Delphi7中存储unicode的BUG教程,一起来看看过程究竟如何进行吧!喜欢还请点个赞哦~

【 tulaoshi.com - 编程语言 】

 

Delphi7中存储unicodeBUG

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

近日,在用delphi7unicode的程序时发现了这样一个问题,就是使用TADOCommand组件执行sql语句时,如果sql语句中有unicode字符,存储在数据库里会出现乱码,使用TTntADOQuery也是一样(使用参数方式不会出现乱码,这里只讨论纯sql的方式)。但是TADOCommand本身是支持widestring的呀,CommandText属性也是widestring类型的,为什么会出现这个问题呢?我试着改变TADOCommand的几个属性值,发现了一个怪现象,只要把ParamCheck属性置为false就可以正常的存储unicode字符,而置为true时就出现乱码。为什么会出现这种情况?这个属性看起来和unicode本身没有任何关系,究竟是什么原因导致了乱码的发生呢?我通过研究TADOCommand所在的adodb.pas文件,发现了问题的所在,我们看一下bug所在的过程:

procedure TADOCommand.AssignCommandText(const Value: WideString; Loading: Boolean);

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  procedure InitParameters;

  var

    I: Integer;

    List: TParameters;

    NativeCommand: string;

  begin

    List := TParameters.Create(Self, TParameter);

    try

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

      NativeCommand := List.ParseSQL(Value, True);

      { Preserve existing values }

      List.AssignValues(Parameters);

      CommandObject.CommandText := NativeCommand;

      if not Loading and (Assigned(Connection) or (ConnectionString '')) then

      begin

        try

          SetConnectionFlag(cfParameters, True);

          try

            { Retrieve additional parameter info from the server if supported }

            Parameters.InternalRefresh;

            { Use additional parameter info from server to initialize our list }

            if Parameters.Count = List.Count then

              for I := 0 to List.Count - 1 do

              begin

                List[I].DataType := Parameters[I].DataType;

                List[I].Size := Parameters[I].Size;

                List[I].NumericScale := Parameters[I].NumericScale;

                List[I].Precision := Parameters[I].Precision;

                List[I].Direction := Parameters[I].Direction;

                List[I].Attributes := Parameters[I].Attributes;

              end

          finally

            SetConnectionFlag(cfParameters, False);

          end;

        except

          { Ignore error if server cannot provide parameter info }

        end;

        if List.Count 0 then

          Parameters.Assign(List);

      end;

    finally

      List.Free;

    end;

  end;

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

begin

  if (CommandType = cmdText) and (Value '') and ParamCheck then

    InitParameters

  else

  begin

    CommandObject.CommandText := Value;

    if not Loading then Parameters.Clear;

  end;

end;

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

看看这一条语句:

if (CommandType = cmdText) and (Value '') and ParamCheck then

    InitParameters

也就是当ParamChecktrue时,会执行InitParameters过程,我们看看这个InitParameters过程中发生了什么:

首先它定义个一个变量:NativeCommand: string;,注意,是stirng不是widestring;我们接着往下看:

NativeCommand := List.ParseSQL(Value, True);

      { Preserve existing values }

      List.AssignValues(Parameters);

CommandObject.CommandText := NativeCommand;

在这里,Valuewidestring类型的,而List.ParseSQL返回的是string类型的,同时NativeCommand也是string类型的,就这样,一个好好的widestring的变量被放到了string类型的变量当中,然后又把NativeCommand赋给了CommandObject.CommandText,因此导致了CommandObject.CommandText并没有得到应该赋给它的widesting值,这也就最终导致了存储unicode时乱码的发生。

       解决方法也很简单,(如果你不愿意修改delphi源程序的话)只需要把ParamCheck置为false就可以了(delphi默认把ParamCheck置为true)。

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

来源:https://www.tulaoshi.com/n/20160219/1622310.html

延伸阅读
  Delphi中两个BUG的分析与修复 在使用Delphi 7进行三层数据库开发时,遇到了两个小问题,通过反复试验,终于找出了Delphi 7中的两个小BUG并进行了修复(好像Delphi 6中也有相同的BUG),撰写此文与大家一起分享成功的喜悦。我也是初学Delphi,文中一定存在不少说的不对的地方,还请各位朋友多多指正。 BUG1.传参时中文...
  一、使用存储过程返回数据集 Oracle中存储过程返回数据集是通过ref cursor类型数据的参数返回的,而返回数据的参数应该是out或in out类型的。 由于在定义存储过程时无法直接指定参数的数据类型为:ref cursor,而是首先通过以下方法将ref cursor进行了重定义: create or replace package FuxjPackage is type Fuxj...
标签: Delphi
  Delphi作为一种面向对象的可视化开发工具,以其开发程序的高速度和编译代码的高效率越来越受到广大编程人员的喜爱。尽管Delphi已经提供了非常强大的开发组件(VCL),但灵活使用API函数一定可以使你的程序增色不少。 状态键的检查 当今不少流行软件的编辑窗口(包括Delphi的代码编辑窗口)的底部都有一个状态条...
从Delphi 5开始VCL中增加了一个新的Contnrs单元,单元中定义了8个新的类,全部都是基于标准的TList 类。 TList 类 TList 类实际上就是一个可以存储指针的容器类,提供了一系列的方法和属性来添加,删除,重排,定位,存取和排序容器中的类,它是基于数组的机制来实现的容器,比较类似于C++中的Vector和Java中的ArrayList,TLi...
Delphi中的线程类--之(1) Delphi 中的线程类 --之(1)    Raptor(原作)       关键字      Thread Event CriticalSection Synchronize     Delphi中的线程类 猛禽 [Menta...

经验教程

231

收藏

25
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部