我在做一个题库管理系统,在做随机生成题目的时候遇到这样的问题.选择题目大类,显示一个DataGrid,DataGrid放置的是该大类对应的小类和用于输入该小类的题目数量的文本框.那么应该如何生成对应的题目,我的想法是遍历整个DataGrid,如果文本框非空则执行一条SQL语句,select top +文本框的值+ * from tablename where 类别=选定类别 order by newid(),然后再把所有sql语句进行批量执行,请问有没有更好的方法?
调试欢乐多
{
string typeID = this.SendBoxDataGrid.Items[i].Cells[0].Text;//第一列绑定类别id,不可见也行
TextBox txt = (TextBox)this.SendBoxDataGrid.Items[i].FindControl("TextBox1");
if(txt.Text.Trim() != "")//除了判断非空,还要判断是否正整数,这个楼主自己补充了
{
sb.Append(" select top "+txt.Text+" * from table1 where typeid ='' order by newid() UNION ");
}
} if(sb.Length>5) sb.Remove(sb.Length-6,5);//去掉结束处的 UNION System.Data.OleDb.OleDbConnection cnn = new System.Data.OleDb.OleDbConnection("......");
System.Data.OleDb.OleDbCommand cm = new System.Data.OleDb.OleDbCommand(sb.ToString(),cnn);
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(cm);
//.........
select top 3 * from table1 where typeid =1 order by newid()
UNION
select top 3 * from table1 where typeid =2 order by newid()
是错误的. 如果去掉order by newid() 才可以,但去掉以后就不能实现随机的功能了.
union
select * from (select top 3 *,newid() as a from table1 where typeid =2 order by newid() ) b
order by a