此段代码无论是速度、均衡性,还是随机度都非常好,也不会出现抽到重复记录的可能。
一、在SQL Server2000中生成随机记录的最好办法:
withtk_querydo
begin
Close;
sql.clear;
sql.Add('selecttop'+inttostr
(st_count)+'tk.ID,标准答案from题库表tk');
sql.Add('wherepidisnullandtk.题型=
'+quotedstr(tx)+'andtk.知识点='+quotedstr(zsd));
sql.add('andtk.难易度='+quotedstr(nyd)+
'andtk.课程号='+quotedstr(kcdm)+'orderbynewid()');
Open;
end; |
注:关键就是orderbynewid()这条语句,随机因子就是从这里产生。
二、数据库为Access2000时生成随机记录的最好办法:
由于Access中没有newid()这一随机函数,故要想在Access中直接由SQL语句生成我们所希望的随机记录不太可能,因此我们只好在开发语言中生成合适SQL语句,让Access执行了。
//获取题库表中的随机ID,组成一个字符串,类似这样(3,8,2,25,49,1,7,10,6,83....)
//kcdm:课程代码,tx:题型,zsd:知识点,nyd:难易度,t_count:某一题型某一知识点某一难度下的要抽取的题量
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....
|