以用户从ComboBox中的选择作为查询条件查询,为什么不行?
if(statinkindForm->ComboBox1->ItemIndex==-1){//如果什么也没选
statinkindForm->Caption="请您选择后确定!!" ;//statinkingForm是ComboBox所在的页面
}
else{//如果用户进行了选择
DM->Qforstat->Close();
DM->Qforstat->SQL->Clear();
DM->Qforstat->SQL->Add("select * from permit ");
DM->Qforstat->SQL->Add(" where mpkind = " + statinkindForm->ComboBox1->Items->Text[statinkindForm->ComboBox1->ItemIndex] );
DM->Qforstat->Open();
//statlistForm->Caption="按许可类别统计结果";
statlistForm->Show();//statlistForm是查询结果DBGrid所在页面
}
但查询结果没有受到ComboBox选择的限制,而是全部显示出来,这是为什么?请大家帮我看看,我的哪一步错啦?
问题点数:20、回复次数:8Top
1 楼MartinWang(小磋磋)回复于 2003-12-04 08:08:02 得分 0
DM->Qforstat->SQL->Add(" where mpkind = '" + statinkindForm->ComboBox1->Items->Text[statinkindForm->ComboBox1->ItemIndex] +"'");
试试!!:)
Top
2 楼ljianq(泉深水清)回复于 2003-12-04 08:20:22 得分 3
statinkindForm->ComboBox1->Items->Strings[statinkindForm->ComboBox1->ItemIndex];
Top
3 楼telescope(望远镜)回复于 2003-12-06 12:30:34 得分 0
ljianq(泉深水清) 的办法我试了:
DM->Qforstat->SQL->Add("select * from permit where mpkind = '");
DM->Qforstat->SQL->Add(statinkindForm->ComboBox1->Items->Strings[statinkindForm->ComboBox1->ItemIndex] );
DM->Qforstat->SQL->Add("'");
可以编译能过,并且在DBGrid所在的页里显示了一下SQL->Text,如果用户选择"餐饮类",则在DBGrid页得到的SQL->Text是select * from permit where mpkind = '餐饮类',但是DBGrid里什么值也没有 :( 没有得到预期结果(数据库里肯定有符合条件的记录),意料之外!!!
我自己又这样写了一下,结果就得到了结果:
DM->Qforstat->SQL->Add("select * from permit wherer mpkind = '餐饮类'");
这样去运行,就能得到正确结果!!!
这使我不解,难道是类型不匹配?statinkindForm->ComboBox1->Items->Strings[statinkindForm->ComboBox1->ItemIndex]是个什么类型的变量??应该怎么做才能使其匹配呢?谢谢!!!
Top
4 楼FallenAngel(发愣的安琪儿)回复于 2003-12-06 13:15:14 得分 2
你要不这样试试
一种办法是statinkindForm->ComboBox1->Items->Strings[statinkindForm->ComboBox1->ItemIndex].Trim()
第二种办法是select * from permit wherer mpkind like '%餐饮类%'Top
5 楼natasha(月不常满)回复于 2003-12-06 13:21:44 得分 10
DM->Qforstat->SQL->Add("select * from permit where mpkind = '");
DM->Qforstat->SQL->Add(statinkindForm->ComboBox1->Items->Strings[statinkindForm->ComboBox1->ItemIndex] );
DM->Qforstat->SQL->Add("'");
试试换成
DM->Qforstat->SQL->Add("select * from permit where mpkind = '" +statinkindForm->ComboBox1->Items->Strings[statinkindForm->ComboBox1->ItemIndex]+"'");Top
6 楼MartinWang(小磋磋)回复于 2003-12-06 19:01:38 得分 5
DM->Qforstat->SQL->Add(" where mpkind = '" + statinkindForm->ComboBox1->Items->Text[statinkindForm->ComboBox1->ItemIndex] +"'");这样试了吗??
如果是mssql,在查询分析器里把你的sql里语句执行试试:)Top
7 楼telescope(望远镜)回复于 2003-12-06 19:40:36 得分 0
我用了 natasha(月不常满) 的办法试验成功了,非常高兴, 同时非常感谢大家的帮助,以后有不懂的,我会常来请教....Top
8 楼telescope(望远镜)回复于 2003-12-06 19:43:44 得分 0
MartinWang(编程浪子)的办法我也试了,可是编译不通过,真是莫名其妙.....Top




