All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
详细讲解PHP编程中分页显示的制作_[PHP教程]
通过PHP连接MYSQL数据库、创建数据库、创建表_[PHP教程]
C#进制转换,的记录_.net资料_编程技术
ASP.NET技巧:DataGridView,的分页处理_.net资料_编程技术
ASP.Net中MD5和SHA1加密的几种方法_[Asp.Net教程]
C++继承与派生
网页制作应掌握技术:随心所欲定制页面"弹出窗口"_JavaScript技术_编程技术
SQLServer2005中的XML选项详解(2)_[SQL Server教程]
判断临时表是否存在的新方法_[SQL Server教程]
ADO.NET打开数据库链接
Reading number is top 10 pictures
Chinese paper-cut grilles art appreciation1
Distribution of wealth in China survey status report
In the world the most mysterious 21 place landscape3
Summer is most suitable for young people to travel in China1
Embarrassing things comic collection2
Fury xp desktop theme
西游四格漫画(四)
NeedWallpaper7
七步解决性冷淡
Get girl by your hand
Download software ranking
变速齿轮3.26
apache-tomcat-6.0.33
Unix video tutorial14
Such love down(擒爱记)
Ashlynn Video2
艳兽都市
传奇私服架设教程
Ashlynn Video3
致我们终将逝去的青春
Call Of Duty2
归海一刀 published in(发表于) 2014/2/3 6:34:09 Edit(编辑)
MS SQL Server和Access分别取得随机记录(随机抽题)之完美篇_[SQL Server教程]

MS SQL Server和Access分别取得随机记录(随机抽题)之完美篇_[SQL Server教程]

MS SQL Server和Access分别取得随机记录(随机抽题)之完美篇_[SQL Server教程]

这是博主用在一个项目上的源码片断,无论是速度、均衡性,还是随机度都非常好,当然这更不可能抽到重复记录了。


一、在MS SQL Server 2000中生成随机记录的最好办法:


with tk_query do


begin


Close;


sql.clear;


sql.Add('select top '+inttostr(st_count)+' tk.ID,标准答案 from 题库表 tk');


sql.Add('where pid is null and tk.题型='+quotedstr(tx)+' and tk.知识点='+quotedstr(zsd));


sql.add('and tk.难易度='+quotedstr(nyd)+' and tk.课程号='+quotedstr(kcdm)+' order by newid()');


Open;


end;


注:关键就是 order by newid() 这条语句!随机因子就是从这里产生的。


二、数据库为Access 2000时生成随机记录的最好办法:



由于Access中没有newid()这一随机函数,故要想在Access中直接由SQL语句生成我们所希望的随机记录不太可能,因此我们只好在开发语言中生成合适SQL语句,让Access执行了(博主的开发工具为Delphi)。


//获取题库表中的随机ID,组成一个字符串,类似这样 (3,8,2,25,49,1,7,10,6,83....)


//kcdm:课程代码,tx:题型,zsd:知识点,nyd:难易度,t_count:某一题型某一知识点某一难度下的要抽取的题量


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 知识点='+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;//随机因子


Randomize;


ii := Random(kk); //取得随机数


if Result='' then


Result := sl.Strings[ii]


else


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


sl.Delete(ii); //为了避免有可能出现的重复,此ID被抽取过后把它删了


if sl.Count=0 then //如果无题可抽了退出循环


Break;


end;


Result := '('+Result+')'; //给结果串前后加上(......),最终形成(24,36,5,89,72,3,6,1....)的串样


finally


sl.Free;



end;


end;


//=============================================== 课程号,题型,知识点,难易度,题量


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,标准答案 from 题库表 tk where id in '+Result


else


Result := ' select top '+t_count+' tk.ID,标准答案 from 题库表 tk where 1=1 ';


end;


//以下为调用上述函数生成随机抽题的代码片断


.......


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;



来源:网络







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.