文本框输入数字,回车后,在文本框显示数据库字段内容
数据库就只有两个字段,
表名postman
数据库为如下:
haoshuxingming
01钟天能
02周杰伦
03黄华云
04梁兴柏
05赵华丽
06吴金水
07张天仑
08韦雨
09丁西兰
10曾林
11张水生
代码如下:
void __fastcall TForm1::Edit1Click(TObject *Sender)
{
AnsiString a_ed1,asq;
a_ed1 = Edit1->Text;
ADOTable1->Active = false;
try
{
ADOTable1->Close();
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
asq = " select xingming from postman ";
asq += " where [haoshu] = ";
asq += "'";
asq += Trim(Edit1->Text);
asq += "'";
ShowMessage(asq);
ADOTable1->Open();
ADOQuery1->SQL->Add(asq);
ADOQuery1->ExecSQL();
Edit1->Text = ADOTable1->FieldByName("xingming")->AsString;
}
catch(...)
{
}
}
能查询,但是只能显示"钟天能"的名字,
请教各位高手,
我该怎么办,才能实现我想要的结果,就是在文本框,输入一个编号,然后,在数据库中,这个编号对应的人就要显示出来
谢谢各位帮助,在下不胜感激,
[解决办法]
void __fastcall TForm1::Edit1Click(TObject *Sender) { AnsiString a_ed1,asq; a_ed1 = Edit1->Text; try { ADOQuery1->Close(); ADOQuery1->SQL->Clear(); asq = " select xingming from postman "; asq += " where [haoshu] = "; asq += "'"; asq += Trim(Edit1->Text); asq += "'"; ShowMessage(asq); ADOQuery1->SQL->Add(asq); //ADOQuery1->ExecSQL(); [color=#FF0000]ADOQuery1->Open(); [/color] Edit1->Text = ADOQuery1->FieldByName("xingming")->AsString; } catch(...) { } }
[解决办法]
void __fastcall TForm1::Edit1Click(TObject *Sender) { AnsiString a_ed1,asq; a_ed1 = Edit1->Text; try { ADOQuery1->Close(); ADOQuery1->SQL->Clear(); asq = " select xingming from postman "; asq += " where [haoshu] = "; asq += "'"; asq += Trim(Edit1->Text); asq += "'"; ShowMessage(asq); ADOQuery1->SQL->Add(asq); //ADOQuery1->ExecSQL(); ADOQuery1->Open(); Edit1->Text = ADOQuery1->FieldByName("xingming")->AsString; } catch(...) { } }