functionTTest_Srv_RDataForm.Get_Random_ID
(constkcdm,tx,zsd,nyd,t_count:string):string;
var
sl:TStrings;
i,ii,kk:integer;
begin
try
Result:="";
sl:=TStringList.Create;
withTADOQuery.Create(nil)do
begin
try
Connection:=Adoconnection1;
SQL.Text:="selectIDfrom题库表
wherepidisnulland题型="+quotedstr(tx)+
"and知识点="+quotedstr(zsd)+"and难易度="+quotedstr(nyd)+
"and课程号="+quotedstr(Kcdm);
Open;
whilenotEofdo
begin
sl.Add(Fields[0].AsString);
Next;
end;
Close;
finally
Free;
end;
end;//endwith....
ifsl.Count=0then
Exit;
fori:=0toStrToIntDef(t_count,0)-1do
begin
kk:=sl.Count;//随机因子
Randomize;
ii:=Random(kk);//取得随机数
ifResult=""then
Result:=sl.Strings[ii]
else
Result:=Result+","+sl.Strings[ii];
sl.Delete(ii);
//为了避免有可能出现的重复,此ID被抽取过后把它删了
ifsl.Count=0then
//如果无题可抽了退出循环
Break;
end;
Result:="("+Result+")";
//给结果串前后加上(......),最终形成(24,36,5,89,72,3,6,1....)的串样
finally
sl.Free;
end;
end;
//=================课程号,题型,知识点,难易度,题量
functionTTest_Srv_RDataForm.Get_Random_Sql
(constkcdm,tx,zsd,nyd,t_count:string):string;
begin
Result:=Get_Random_ID(kcdm,tx,zsd,nyd,t_count);
ifResult<>""then
Result:="selecttop"+t_count+"tk.ID,
标准答案from题库表tkwhereidin"+Result
else
Result:="selecttop"+t_count+"tk.ID,
标准答案from题库表tkwhere1=1";
end;
//以下为调用上述函数生成随机抽题的代码片断
.......
withtk_querydo
begin
Close;
sql.Clear;
sql.Text:=Get_Random_Sql(Kcdm,tx_str,zsd_str,nyd_str,txzsd_count_str);
Open;
end; |