Delphi7中存储unicode的BUG

风景线cxy

风景线cxy

2016-02-19 19:56

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

Delphi7中存储unicodeBUG

  

(本文来源于图老师网站,更多请访问https://m.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://m.tulaoshi.com/bianchengyuyan/)

  

  

  

  procedure InitParameters;

  var

    I: Integer;

    List: TParameters;

    NativeCommand: string;

  begin

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

    try

      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://m.tulaoshi.com/bianchengyuyan/)

  

  

  

begin

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

    InitParameters

  else

  begin

    CommandObject.CommandText := Value;

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

    if not Loading then Parameters.Clear;

  end;

end;

  

(本文来源于图老师网站,更多请访问https://m.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)。

  


展开更多 50%)
分享

猜你喜欢

Delphi7中存储unicode的BUG

编程语言 网络编程
Delphi7中存储unicode的BUG

在Delphi7中调试COM+

编程语言 网络编程
在Delphi7中调试COM+

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

在delphi7中利用mscomm控件编程

编程语言 网络编程
在delphi7中利用mscomm控件编程

Delphi7对XML的支持分析

编程语言 网络编程
Delphi7对XML的支持分析

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

用Delphi7设计FTP上传软件

Delphi
用Delphi7设计FTP上传软件

Delphi7的WebService与数据库

Delphi
Delphi7的WebService与数据库

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

DBGrid单元格画圆圈

DBGrid单元格画圆圈

Word与Access数据交流技巧

Word与Access数据交流技巧
下拉加载更多内容 ↓