如何从sql中获取问题 [英] how to get question from sql

查看:60
本文介绍了如何从sql中获取问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生我设计考试网站但现在的问题是如何在aspx页面上以不同的顺序从数据库中获取随机问题

解决方案

从表中选择随机问题:



使用 NEWID()



< pre lang =SQL> 选择 top 1 来自 订单 NEWID()


既然你我将使用随机问题,你已经掌握了我想要转发给MSDN的基础知识以获取更多细节,但是会给你基本的实现思路。



您可以从数据库中创建问题列表,而List是另一种数组,可以让您使用索引器从中获取问题,(例如,myList [0];获取第一个索引处的元素)。为了确保随机捕获这些,.NET将允许您使用Random对象来选择随机性。 随机 [ ^ ]让你做这个。



您可以创建此类的实例,并使用它在运行时创建一个随机数,以使用该数字来选择元素(是列表中包含问题的字符串(当您要查询数据库时,将返回 IEnumerable< object>< / object> )对象。



这样的示例代码就是这样,



  //  在0-99内创建索引 
var index = new Random()。Next( 100 );
// 假设,问题是db.Query(string)的结果;方法,然后
var question = questions [index];
// 现在问题将包含列表中的问题对象。





..然后你可以在HTML中使用它来像这样呈现它,



 <   div   >  
< h3 > @ question.Title < / h3 >
< p > @ question.Description < / p >
< / di v >





..这样做会对你而言。有关这方面的更多信息,并创建随机数,请阅读附带的MSDN文档。


sir i design exam site but now problem is how to get random question from database in always different sequence on aspx page

解决方案

Select Random Question From Your Table :

Use NEWID()

select top 1 column from Table order by NEWID()


Since you're going to work with Random questions, and you already have the basics I would like to forward you to MSDN for more details, but will give you the basic idea of implementation.

You can create a list of the questions from the database, and the List being another kind of array will let you get a question from it using the indexers, (eg, myList[0]; get the element at 1st index). Just to make sure that these are captured randomly, .NET would let you use a Random object to select randomness. The class Random[^] lets you do this.

You can create an instance of this class, and use it, to create a Random number on the run-time, to use that number to select the element (which is a string containing the question) from the list (which, when you're going to query the database will be returned as IEnumerable<object></object>) object.

The sample code for this would be like this,

// create index within 0-99
var index = new Random().Next(100);
// assume, questions is an outcome of db.Query(string); method, then
var question = questions[index];
// now the question will contain the question object from the list.



.. you can then use it inside the HTML to render it like this,

<div>
   <h3>@question.Title</h3>
   <p>@question.Description</p>
</div>



.. this will do the thing for you. For more on this, and creating the Random numbers, do read that MSDN document attached.


这篇关于如何从sql中获取问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆