Delphi7 stores unicode's bug?

xiaoxiao2021-03-05  29

Delphi7 stores unicode's bug?

Recently, when you use Delphi7 to Unicode, you find this problem. When you use the TADOCOMMAND component to execute the SQL statement, if there is a Unicode character in the SQL statement, there is garbled in the database, using TTNTADOQUERY is also the same (using the parameter method There will be garbled, and only the pure SQL mode is discussed here). But Tadocommand itself is supported by WideString. The commandtext property is also a wideString type. Why do you have this problem? I tried to change several attribute values ​​of TadoCommand, discovered a strange phenomenon, as long as the Paramcheck property is set to false, you can store the Unicode character normally, and there is garbled when you are True. Why does this happen? This property doesn't matter with Unicode itself, what is the cause of garbled? I found the problem of the problem by studying the AdoDb.Pas file where Tadocommand is located, let's take a look at the process of BUG:

Procedure tadocommand.assigncommandtext (const value: widestring; loading: boolean);

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 (assoc "")).

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 dam

For i: = 0 to list.count - 1 do

Begin

List [i] .dattype: = 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] .attribute;

end

Finally

SetConnectionFlag (CFParameters, False);

END;

Except

{Ignore Error IF Server Cannot Provide Parameter Info}

IF list.count> 0 THEN

Parameters.Assign (List);

END;

Finally

List.free;

END;

END;

Begin

IF (CommandType = cmdtext) and (value <> ') and paramcheck then

INITPARAMETERS

Else

Begin

CommandObject.commandtext: = Value;

IF not loading kilion;

END;

END;

Take a look at this statement:

IF (CommandType = cmdtext) and (value <> ') and paramcheck then

INITPARAMETERS

That is, when paramcheck is True, the initParameters process will be executed, let's see what happened during this initparameters:

First it defines a variable: nativeCommand: string;, pay attention, stirng is not wideString; we look down:

NativeCommand: = list.parsesql (value, true);

{Preserve EXISTING VALUES}

List.assignvalues ​​(parameters);

CommandObject.commandtext: = nativeCommand;

Here, Value is a WideString type, and list.parsesql returns a string type, and NativeCommand is also a String type. In this way, a good WiDestring variable is placed in a string type variable, and then nativeCommand Give CommistObject.commandtext, thus causing CommandObject.commandtext and did not get the widESTING value that should be given to it, which eventually causes chaotic code when the Unicode is stored.

The solution is also very simple, (if you don't want to modify the Delphi source), you just need to set the paramcheck to false (Delphi defaults to TRUE).

转载请注明原文地址:https://www.9cbs.com/read-33825.html

New Post(0)