在ODAC的所有版本中,一直存在着这样的问题:当字段名以X开头而且以后的字母能够组成一组16进制的数时,很可能会引起数值类型“漂移”,比如我们常用XB来做性别的字段名,当使用fieldbyname('xb')函数时经常会出错。这个问题是出在DBACCESS.PAS中以下的一段: function TCustomDADataSet.FieldByParamName(var ParamName: string; var Old: boolean; var AFieldNo: integer): TField; // Returns field that corresponds to ParamName function FindFieldByFieldNo(FieldNo: integer): TField; var i: integer; begin for i := 0 to Fields.Count - 1 do begin Result := Fields[i]; if Result.FieldNo = FieldNo then Exit; end; Result := nil; end;
var e: integer; begin Old := CompareText(Copy(ParamName, 1, 4), 'OLD_') = 0; Result := nil; if Old then begin Result := FindField(ParamName); if Result <> nil then Old := False // fieldname is starting with OLD_ else ParamName := Copy(ParamName, 5, Length(ParamName) - 4); end;
if Result = nil then begin Val(ParamName, AFieldNo, e); //问题就出在这里,如果字段名是XB,则会被转换成数值11,因而会导致下面出现问题
//modify by xiucai 以下两行是我加的,加了以后就解决了这个问题 if not ParamName[1] in ('0'..'9') then e :=-1;
if e = 0 then Result := FindFieldByFieldNo(AFieldNo) else AFieldNo := -1; end else begin AFieldNo := -1; end;
if Result = nil then Result := FindField(ParamName); end;