小弟在数据库编程中遇到这样一个问题, 我想实现汉字的拼音查询功能而以下的代码在BDE的Ttable中是好用的为什么换到ADO的adotable就总提示project aaa.exe raised exception class EvariantError with message 'Invalid variant type conversion',process stopped.
代码如下:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, DBCtrls, DB, DBTables, Grids, DBGrids;type
TForm1 = class(TForm)
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Table1: TTable;
DBNavigator1: TDBNavigator;
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function GetPY(hzchar:string):char;//返回汉字的拼音首字符
procedure LocatebyPY(t1:Tadotable;Fieldname:string;PYstr:string);//检查符合条件的记录
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function TForm1.GetPY(hzchar:string):char;
begin
case WORD(hzchar[1])shl(8)+WORD(hzchar[2]) of
$B0A1..$B0C4 : result:='A';
$B0C5..$B2C0 : result:='B';
$B2C1..$B4ED : result:='C';
$B4EE..$B6E9 : result:='D';
$B6EA..$B7A1 : result:='E';
$B7A2..$B8C0 : result:='F';
$B9C1..$B9FD : result:='G';
$B9FE..$BBF6 : result:='H';
$BBF7..$BFA5 : result:='J';
$BFA6..$C0AB : result:='K';
$C0AC..$C2E7 : result:='L';
$C2E8..$C4C2 : result:='M';
$C4C3..$C5B5 : result:='N';
$C5B6..$C5BD : result:='O';
$C5BE..$C6D9 : result:='P';
$C6DA..$C8BA : result:='Q';
$C8BB..$C8F5 : result:='R';
$C8F6..$CBF9 : result:='S';
$CBFA..$CDD9 : result:='T';
$CDDA..$CEF3 : result:='W';
$CEF4..$D188 : result:='X';
$D189..$D4D0 : result:='Y';
$D4D1..$D7F9 : result:='Z';
else
result:=char(0);
end;
end;
procedure TForm1.LocatebyPY(t1:Tadotable;Fieldname:string;PYstr:string);
label NotFound;
var
j:integer;
hzchar:string;
hzstr:array[0..100] of char;
begin
while not t1.eof do
begin
strcopy(hzstr,pchar(t1.fieldbyname(fieldname).asstring));
for j:=0 to length(PYstr)-1 do
begin
hzchar:=hzstr[2*j]+hzstr[2*j+1];
if (PYstr[j+1]<>'?')and(uppercase(PYstr[j+1])<>GetPY(hzchar))
then
goto NotFound;
end;
if messageDlg('已经找到,继续查找?',mtConfirmation,[mbYes,mbNo],0)<>mrYes then
exit;
NotFound:
t1.next;
end;
showmessage('查找结束,没有找到!');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
LocateByPY(adotable1,'SZXB',edit1.text);
end;end.
代码如下:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, DBCtrls, DB, DBTables, Grids, DBGrids;type
TForm1 = class(TForm)
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Table1: TTable;
DBNavigator1: TDBNavigator;
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function GetPY(hzchar:string):char;//返回汉字的拼音首字符
procedure LocatebyPY(t1:Tadotable;Fieldname:string;PYstr:string);//检查符合条件的记录
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function TForm1.GetPY(hzchar:string):char;
begin
case WORD(hzchar[1])shl(8)+WORD(hzchar[2]) of
$B0A1..$B0C4 : result:='A';
$B0C5..$B2C0 : result:='B';
$B2C1..$B4ED : result:='C';
$B4EE..$B6E9 : result:='D';
$B6EA..$B7A1 : result:='E';
$B7A2..$B8C0 : result:='F';
$B9C1..$B9FD : result:='G';
$B9FE..$BBF6 : result:='H';
$BBF7..$BFA5 : result:='J';
$BFA6..$C0AB : result:='K';
$C0AC..$C2E7 : result:='L';
$C2E8..$C4C2 : result:='M';
$C4C3..$C5B5 : result:='N';
$C5B6..$C5BD : result:='O';
$C5BE..$C6D9 : result:='P';
$C6DA..$C8BA : result:='Q';
$C8BB..$C8F5 : result:='R';
$C8F6..$CBF9 : result:='S';
$CBFA..$CDD9 : result:='T';
$CDDA..$CEF3 : result:='W';
$CEF4..$D188 : result:='X';
$D189..$D4D0 : result:='Y';
$D4D1..$D7F9 : result:='Z';
else
result:=char(0);
end;
end;
procedure TForm1.LocatebyPY(t1:Tadotable;Fieldname:string;PYstr:string);
label NotFound;
var
j:integer;
hzchar:string;
hzstr:array[0..100] of char;
begin
while not t1.eof do
begin
strcopy(hzstr,pchar(t1.fieldbyname(fieldname).asstring));
for j:=0 to length(PYstr)-1 do
begin
hzchar:=hzstr[2*j]+hzstr[2*j+1];
if (PYstr[j+1]<>'?')and(uppercase(PYstr[j+1])<>GetPY(hzchar))
then
goto NotFound;
end;
if messageDlg('已经找到,继续查找?',mtConfirmation,[mbYes,mbNo],0)<>mrYes then
exit;
NotFound:
t1.next;
end;
showmessage('查找结束,没有找到!');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
LocateByPY(adotable1,'SZXB',edit1.text);
end;end.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货