国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > Access > 正文

MS SQL Server和Access分別取得隨機(jī)記錄

2024-09-07 19:03:00
字體:
供稿:網(wǎng)友

這是博主用在一個項(xiàng)目上的源碼片斷,無論是速度、均衡性,還是隨機(jī)度都非常好,當(dāng)然這更不可能抽到重復(fù)記錄了。

一、在MS SQL Server 2000中生成隨機(jī)記錄的最好辦法:

with tk_query do

begin

Close;

sql.clear;

sql.Add('select top '+inttostr(st_count)+' tk.ID,標(biāo)準(zhǔn)答案 from 題庫表 tk');

sql.Add('where pid is null and tk.題型='+quotedstr(tx)+' and tk.知識點(diǎn)='+quotedstr(zsd));

sql.add('and tk.難易度='+quotedstr(nyd)+' and tk.課程號='+quotedstr(kcdm)+' order by newid()');

Open;

end;

注:關(guān)鍵就是 order by newid() 這條語句!隨機(jī)因子就是從這里產(chǎn)生的。

二、數(shù)據(jù)庫為Access2000時生成隨機(jī)記錄的最好辦法:

由于Access中沒有newid()這一隨機(jī)函數(shù),故要想在Access中直接由SQL語句生成我們所希望的隨機(jī)記錄不太可能,因此我們只好在開發(fā)語言中生成合適SQL語句,讓Access執(zhí)行了(博主的開發(fā)工具為Delphi)。

//獲取題庫表中的隨機(jī)ID,組成一個字符串,類似這樣 (3,8,2,25,49,1,7,10,6,83....)

//kcdm:課程代碼,tx:題型,zsd:知識點(diǎn),nyd:難易度,t_count:某一題型某一知識點(diǎn)某一難度下的要抽取的題量

function TTest_Srv_RDataForm.Get_Random_ID(const kcdm,tx,zsd,nyd,t_count:string):string;

var

sl: TStrings;

i,ii,kk: integer;

begin

try

Result := '';

sl := TStringList.Create;

with TADOQuery.Create(nil) do

begin

try

Connection := Adoconnection1;

SQL.Text := ' select ID from 題庫表 where pid is null and 題型='+quotedstr(tx)+

' and 知識點(diǎn)='+quotedstr(zsd)+' and 難易度='+quotedstr(nyd)+

' and 課程號='+quotedstr(Kcdm);

Open;

while not Eof do

begin

sl.Add(Fields[0].AsString);

Next;

end;

Close;

finally

Free;

end;

end; //end with ....

if sl.Count=0 then

Exit;

for i := 0 to StrToIntDef(t_count,0)-1 do

begin

kk := sl.Count;//隨機(jī)因子

Randomize;

ii := Random(kk); //取得隨機(jī)數(shù)

if Result='' then

Result := sl.Strings[ii]

else

Result := Result+','+sl.Strings[ii];

sl.Delete(ii); //為了避免有可能出現(xiàn)的重復(fù),此ID被抽取過后把它刪了

if sl.Count=0 then //如果無題可抽了退出循環(huán)

Break;

end;

Result := '('+Result+')'; //給結(jié)果串前后加上(......),最終形成(24,36,5,89,72,3,6,1....)的串樣

finally

sl.Free;

end;

end;

//=============================================== 課程號,題型,知識點(diǎn),難易度,題量

function TTest_Srv_RDataForm.Get_Random_Sql(const kcdm,tx,zsd,nyd,t_count:string):string;

begin

Result := Get_Random_ID(kcdm,tx,zsd,nyd,t_count);

if Result <> '' then

Result := ' select top '+t_count+' tk.ID,標(biāo)準(zhǔn)答案 from 題庫表 tk where id in '+Result

else

Result := ' select top '+t_count+' tk.ID,標(biāo)準(zhǔn)答案 from 題庫表 tk where 1=1 ';

end;

//以下為調(diào)用上述函數(shù)生成隨機(jī)抽題的代碼片斷

.......

with tk_query do

begin

Close;

sql.Clear;

sql.Text := Get_Random_Sql(Kcdm,tx_str,zsd_str,nyd_str,txzsd_count_str);

Open;

end;

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 凤城市| 千阳县| 安仁县| 景宁| 阿图什市| 桂平市| 辽中县| 澄江县| 会宁县| 舒兰市| 金乡县| 岫岩| 镇雄县| 文成县| 翁源县| 双柏县| 义马市| 循化| 阿克苏市| 无为县| 安徽省| 昆明市| 循化| 眉山市| 蛟河市| 元阳县| 勃利县| 芮城县| 阳山县| 洱源县| 正镶白旗| 古浪县| 广宁县| 廉江市| 厦门市| 三都| 卓资县| 高阳县| 武冈市| 香河县| 大兴区|