我在一个 DataModule 放了一个 ADOQuery 控件。程序运行时在一个 Form 中用代码生成SQL语句并用其查询。 AdoQuery 会生成相应的字段。比如数据库中的字符串类型的列, AdoQuery 会自动生成 TStringField 的字段。
但是数据库中的字符串列是有其他外文的,非要用 TNT 控件中的 TTntWideStringField 字段才能正常显示。但是在动态查询时产生的字段的类型我不知道该怎么控制,怎么让其生成 TTntWideStringField 类型的字段。
再次向朋友们请教一下,我该如何?先谢过大家看我的问题!
查询时就转换
等来电了我试试楼上各位所说的办法。最好的办法是把数据库字段类型设为unicode类型,这样就自动了.我用的MYSQL,怎么设置某个字段为Unicode 呢?
我用这些代码测试不行
ADOConnection1.Connected := True;
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('SELECT cast(Field1 AS TTntWideStringField) FROM Table1');
ADOQuery1.Open;
执行这些代码后出现这样的错误提示。[MySQL][ODBC 3.51 Driver][mysqld-5.1.20-beta-community-nt-debug]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TTntWideStringField) FROM Table1' at line 1.
还有,请问楼上的两位朋友,怎么“把数据库字段类型设为unicode类型.”
TCustomADODataSet.InternalInitFieldDefs ,这个过程的代码可以在Delphi7自带的 ADODB 的单元文件中看到。帖子最后我也贴出来了。各位朋友能从我说的这些资料中找到解决的办法吗?就是没有,也请你给个参考意见。谢谢!procedure TCustomADODataSet.InternalInitFieldDefs;
const
SIsAutoInc: WideString = 'ISAUTOINCREMENT'; { do not localize }
var
HasAutoIncProp: Boolean; { Determine if the field's property list contains an ISAUTOINCREMENT entry }
procedure AddFieldDef(F: Field; FieldDefs: TFieldDefs);
var
FieldType: TFieldType;
FieldDef: TFieldDef;
I: Integer;
FName: string;
FSize: Integer;
FPrecision: Integer;
begin
FieldType := ADOTypeToFieldType(F.Type_, EnableBCD);
if FieldType <> ftUnknown then
begin
FSize := 0;
FPrecision := 0;
FieldDef := FieldDefs.AddFieldDef;
with FieldDef do
begin
FieldNo := FieldDefs.Count;
I := 0;
FName := F.Name;
while (FName = '') or (FieldDefs.IndexOf(FName) >= 0) do
begin
Inc(I);
if F.Name = '' then
FName := Format('COLUMN%d', [I]) else { Do not localize }
FName := Format('%s_%d', [F.Name, I]);
end;
Name := FName;
if (F.Type_ = adNumeric) and (F.NumericScale = 0) and
(F.Precision < 10) then
FieldType := ftInteger;
case FieldType of
ftString, ftWideString, ftBytes, ftVarBytes, ftFixedChar:
FSize := F.DefinedSize;
ftBCD:
begin
FPrecision := F.Precision;
FSize := ShortInt(F.NumericScale);
if FSize < 0 then FSize := 4;
end;
ftInteger:
if HasAutoIncProp and (F.Properties[SIsAutoInc].Value = True) then
FieldType := ftAutoInc;
ftGuid:
FSize := 38;
end; if ((adFldRowID and F.Attributes) <> 0) then
Attributes := Attributes + [faHiddenCol];
if ((adFldFixed and F.Attributes) <> 0) then
Attributes := Attributes + [faFixed];
if (((adFldUpdatable+adFldUnknownUpdatable) and F.Attributes) = 0) or
(FieldType = ftAutoInc) then
Attributes := Attributes + [faReadOnly];
DataType := FieldType;
Size := FSize;
Precision := FPrecision;
if (DataType = ftDataSet) and (Fields.Count = 0) then
ObjectView := True;
end;
end;
end;var
Count, I: Integer;
begin
FieldDefs.Clear;
Count := Recordset.Fields.Count;
if Count > 0 then
HasAutoIncProp := PropertyExists(Recordset.Fields[0].Properties, SIsAutoInc);
for I := 0 to Count - 1 do
AddFieldDef(Recordset.Fields[I], FieldDefs);
end;
//****************************
//返回SQL语句执行结果的第一行第一列的字符串
//参数:sqlstr,执行的SQL语句
//****************************
function TOperationData.ExecSqlString(sqlstr:String):String;
begin
with q do
begin
Close;
Sql.Clear;
Sql.Add(sqlstr);
Open;
if Fields[0].AsString = null then
Result:=''
else
Result:=Fields[0].AsString;
end;
end;