新手求救

解决方案 »

  1.   

    你的问题呢?如果光是随机抽题的话在你的SQL语句里面这样写
    select top 20 * from tablename order by newid()
    这样是能随机抽取二十条记录
      

  2.   

    怎么没分啊?
    select * from 题库 order by newid()   试试
      

  3.   

    select top 数量 from 题库 order by newid()
    不好意思写错了
      

  4.   

    在线答题系统的随即抽题目这里有两种方法,我写List的方法:不知道你分了三层写没有?三层的写法现在Models层建立对应的题目获取的表Question表,
    把表内的字段全部封装好,中间的封装过程省略。然后在DAL层(数据访问层)写如下代码//下面根据传送的科目id编号来查询所选科目下的题目的总数        public int length(int SubjectId)
            {
                //构建查询语句,通过科目id编号来查询所选科目下的题目总数,这个将变成你随即抽取范围值
                string sql = string.Format("select count(*) from Question where SubjectId={0}", SubjectId);
                int length= 0;
                //我的连接字符串语句是单独写在一个类里面的,你可以把 DbHelp.connStr替换成你的连接字符串
                using (SqlConnection connection = new SqlConnection(DbHelp.connStr))
                {
                    using (SqlCommand command = new SqlCommand(sql, connection))
                    {
                        connection.Open();
                        length =(int)command.ExecuteScalar();
                    }
                }
                return length;
            }          //下面根据传送过来的科目id 把所有信息获取装到传入的数组中间        public List<Question> AllQuestionIds(int SubjectId)
            {
                
                String sql = string.Format("select * from question where SubjectId={0} ",SubjectId);
                using (SqlConnection connection = new SqlConnection(DbHelp.connStr))
                {
                    using (SqlCommand command = new SqlCommand(sql, connection))
                    {
                        connection.Open();
                        SqlDataReader dataReader = command.ExecuteReader();
                        //把获取的信息 一个个通过实体类封装 然后存储到List里面
                        while (dataReader.Read())
                        {
                            Question  question=new Question();
                            question.QuestionId=(int)dataReader[0];
                            question.Question1 = dataReader[1].ToString();
                            //底下省略了,你根据你的表实际情况封装 .......
                            //然后加入到List集合中去,List对象和Question对象自己去new
                            list.Add(question);
                        }
                       
                    }
                }
                return list;
            }
    然后你在业务层做随即数获取就行了:public class QuestionManager
        {
            QuestionService qs = new QuestionService();
            List<Question> list = new List<Question>();
            Question question = new Question();        public List<Question> SelectQuestion(int SubjectId) 
            {
                int length = qs.length(SubjectId);
                List<Question> list = qs.AllQuestionIds(SubjectId);
                List<Question> Alist = new List<Question>();
                Random random = new Random();
                bool[] isTure = new bool[length];
                int i = 0;
                //循环来获取20个随机题目
                while (i < 20)
                {
                    //首先先获取随机下标
                    int questionIndex = random.Next(length);
                     
                    //然后判断这个题目是否以选中,选中是 true, 没有是false
                    if (isTure[questionIndex] ==false)
                    {
                        //如果没有选中 就把list装到一个新的list集合中去
                        Alist.Add(list[questionIndex]);                    //然后计数器自加
                        i++;                    //然后把这个下表元素在数据里面的属性改变为true;
                        isTure[questionIndex] = true;                }
                }
                return Alist;        }
    最后在表示层调用就行了。另外一种就是没有分层,直接用数组保存的,方法其实一样,只是要多获取一次题目,并且要装入到数组里面去
    这个方法我就不写了,不过你需要数组的随机抽取方法,我再发好了。个人觉得那个方法没有多大含义。