|
众里寻她千百度,原来官方坛子里早就有人给出了,只怪自己没早发现。
一。新建一UnitGetSetText.pas文件,当然你也可以你女朋友的名字命名:- unit UnitGetSetText;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, Forms, LConvEncoding, db;
- type
- { TGetSetTextClass }
- TGetSetTextClass = class
- private
- procedure GetText(Sender: TField; var aText: string;
- DisplayText: Boolean);
- procedure SetText(Sender: TField; const aText: string);
- public
- procedure AssignGetTextToQueries(F: TCustomForm);
- end;
- var
- GetSetTextClass: TGetSetTextClass;
- implementation
- { TGetSetTextClass }
- procedure TGetSetTextClass.GetText(Sender: TField; var aText: string;
- DisplayText: Boolean);
- begin
- aText := CP936ToUTF8(Sender.AsString);
- end;
- procedure TGetSetTextClass.SetText(Sender: TField; const aText: string);
- begin
- Sender.AsString := UTF8ToCP936(aText);
- end;
- procedure TGetSetTextClass.AssignGetTextToQueries(F: TCustomForm);
- var
- I, J: Integer;
- Q: TDataSet; // doesn't matter if it's TQuery or TTable or TZQuery...
- SF: TStringField;
- begin
- for I := 0 to F.ComponentCount - 1 do
- if F.Components[I] is TDataSet then begin
- Q := TDataSet(F.Components[I]);
- for J := 0 to Q.FieldCount - 1 do
- if Q.Fields[J] is TStringField then begin
- SF := TStringField(Q.Fields[J]);
- SF.OnGetText := @GetText;
- SF.OnSetText := @SetText;
- end;
- end;
- end;
- initialization
- GetSetTextClass := TGetSetTextClass.Create;
- finalization
- GetSetTextClass.Free;
- end.
-
复制代码 二。在连接MS Access 数据库的窗体的onCreate事件下添加以下代码,前提是引用前面一的文件。- procedure tform1.formcreate(sender: tobject);
- begin
- GetSetTextClass.AssignGetTextToQueries(Self);
- end;
复制代码 这样应该就可以了,中文记录现在应该能正确显示了。
三。添加新记录要保存成功的话,还得在保存按钮事件添加与Update有关的代码。以下例子是在DGNavigator的单击事件下判断单击的是保存按钮时的代码:- procedure tform1.dbnavigator1click(sender: tobject; button: tdbnavbuttontype);
- begin
- if button=nbPost then
- begin
- sqlquery1.edit;
- sqlquery1.ApplyUpdates;
- sqltransaction1.CommitRetaining;
- end;
- end;
复制代码
|
评分
-
查看全部评分
|