积极答复者
关于考试随机出试题具体救助!

问题
-
这里详细背景是这样的,请各位高手大神们帮帮忙了...
自己使用两个DropDownList,一个是试题类型(3种题型:编程、阅读程序与分析和单项选择题),没有绑定;另一个是试题数量,也没有绑定;一个Button;在数据库中也分别有对应的3种题型的表格(即3张,一种题型一张);数据库中还有一张是试卷表,用于把随机选出的题填入里面;页面中再添加两个SqlDataSource和两个GridView
教师在进入这个页面后,选择相应的试题类型和试题数量后,将随机在对应的表中选出试题,然后试题会填入那一张试卷表
在"组卷界面.aspx.vb"的Button_Click事件中添加了如下代码:
If DropDownList1.SelectedValue = "编程" Then
SqlDataSource1.ConnectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
SqlDataSource1.SelectCommand = "SELECT * FROM 编程 "
GridView1.DataSourceID = SqlDataSource1.ID ElseIf DropDownList1.SelectedValue = "阅读程序与分析" Then SqlDataSource1.ConnectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionStringSqlDataSource1.SelectCommand = "SELECT * FROM 阅读程序与分析"
GridView1.DataSourceID = SqlDataSource1.ID
ElseIf DropDownList1.SelectedValue = "单项选择题" Then
SqlDataSource1.ConnectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
SqlDataSource1.SelectCommand = "SELECT * FROM 单项选择题"
GridView1.DataSourceID = SqlDataSource1.ID
End If不知道如何让DropDownList2(即试题数量)选择的值与SELECT牵扯上关系呢?还要使用随机排列...
自己已经知道newid()可进行随机排列;ABS((CHECKSUM())什么的可以出现一个随机数;
Randomize之后Dim a=Int(Rnd*10)+1等,但还是不知道如何使用...
最后还需要使用Insert把随机出来的题插入到一张新的表中...实在是心有余而力不足...
如果能有高手大神解决所有问题,自己真愿拜师了...
qianghuishi
答案
-
你可以在T-SQL中利用Order by newid()的方式來隨機排序,詳細作法可以參考DotJum的文章。
http://www.dotblogs.com.tw/dotjum/archive/2008/10/23/5755.aspx
另外有關假設你要把隨機產生的題插入到試題的TABLE,可以先這樣做,之後再從試題這個TABLE把隨機產生的試題顯示到GRIDVIEW。
INSERT INTO 試題 SELECT * FROM 阅读程序与分析 ORDER BY NEWID()
以上說明若有錯誤請指教,謝謝。
http://www.dotblogs.com.tw/terrychuang/- 已标记为答案 强辉史 2012年3月30日 7:59
-
就我剛剛所說的作法,我以Northwind的Region資料表來模擬你的TABLE,並將隨機產生順序的資料儲存到RegionRandom資料表。
use Northwind go if exists (select * from sys.objects where type='U' and name='RegionRandom') drop table RegionRandom go create table RegionRandom ( RegionID int, RegionDesc nvarchar(50) ) select * from RegionRandom
你可以利用下列程式碼先做INSERT產生隨機的順序之後再將RegionRandom的內容顯示到GridView。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class GVSoring : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { SqlDataSource1.InsertCommand = "INSERT INTO [RegionRandom] SELECT * FROM Region ORDER BY NEWID()"; SqlDataSource1.Insert(); SqlDataSource1.SelectCommand = "SELECT * FROM [RegionRandom]"; GridView1.DataBind(); } } } }
提醒您,上述只是提供一個方向,詳細的做法或許得看你真正的需求才行。
以上說明若有錯誤請指教,謝謝。
http://www.dotblogs.com.tw/terrychuang/- 已标记为答案 强辉史 2012年3月30日 7:59
全部回复
-
你可以在T-SQL中利用Order by newid()的方式來隨機排序,詳細作法可以參考DotJum的文章。
http://www.dotblogs.com.tw/dotjum/archive/2008/10/23/5755.aspx
另外有關假設你要把隨機產生的題插入到試題的TABLE,可以先這樣做,之後再從試題這個TABLE把隨機產生的試題顯示到GRIDVIEW。
INSERT INTO 試題 SELECT * FROM 阅读程序与分析 ORDER BY NEWID()
以上說明若有錯誤請指教,謝謝。
http://www.dotblogs.com.tw/terrychuang/- 已标记为答案 强辉史 2012年3月30日 7:59
-
非常感谢,但是以下是自己在尝试您所指导的方法之后仍然存在的问题:
首先是以阅读程序与分析题为例,
DropDownList1.SelectedValue = "阅读程序与分析" Then
SqlDataSource1.ConnectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
SqlDataSource1.SelectCommand = "INSERT INTO 试题 SELECT * FROM 阅读程序与分析 ORDER BY NEWID()"
GridView1.DataSourceID = SqlDataSource1.ID可在试题表里没有插入数据的痕迹,还是一张空表,当然阅读程序与分析表的所有列都包含在试题表中...不知道您所给的命令该怎么写呢?自己也尝试过SqlDataSource1.InsertCommand="INSERT INTO 试题 SELECT * FROM 阅读程序与分析 ORDER BY NEWID()"但还是不行...
其次是请问如何使DropDownList2(即教师所选择的试题数量)的SelectedValue与T-SQL的SELECT命令挂上钩呢?
qianghuishi
-
INSERT INTO 试题 SELECT * FROM 阅读程序与分析 ORDER BY NEWID()
這段T-SQL建議寫在InsertCommand或UpdatCommand中,搭配Insert或Update來執行。
執行完之後試題資料表就會有資料了,你再BIND給GRIDVIEW。
有關DropDownlist與SqlDataSource的整合,可以參考下面這篇文章。
http://msdn.microsoft.com/en-us/library/z72eefad.aspx
以上說明若有錯誤請指教,謝謝。
http://www.dotblogs.com.tw/terrychuang/ -
自己想到了这么一种方式,只限于构想,还请指教:
若使用FOR...NEXT语句,即
If DropDownList1.SelectedValue = "阅读程序与分析" Then
SqlDataSource1.ConnectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionStringFOR int i=0 To DropDownList2.SelectedValue
SqlDataSource1.SelectCommand = “SELECT TOP 1 * FROM 阅读程序与分析 ORDER BY NEWID()" ‘每次只选择一道题’然后Insert into试卷表
Next
GridView1.DataSourceID = SqlDataSource1.ID 不知道这样可不可以呢?又该如何消除选出的题会相同的问题?以及INSERT的T-SQL该如何写呢?望高手再现了!
qianghuishi
-
你好!实在是又没有资料所以又来向您请教了:
这是自己的代码
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If DropDownList1.SelectedValue = "阅读程序与分析" Then
SqlDataSource1.ConnectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
SqlDataSource1.SelectCommand = "SELECT TOP 4 * FROM 阅读程序与分析 ORDER BY NEWID()"
SqlDataSource1.InsertCommand = "INSERT INTO 试卷 SELECT TOP 4 * FROM 阅读程序与分析 ORDER BY NEWID()"
GridView1.DataSourceID = SqlDataSource1.ID问题是在随后出现的Web页面中,选择DropDownList1里的阅读程序与分析,单击Button确实会有4道随机试题出现,但自己一检查数据库中的试卷表,却还是没有任何插入的资料,自己在想试卷表与阅读程序与分析表是两张不同的表,两者一张表中的选择插入到另一张表中,不会这么简单吧?
望高手再次指教!拜托了!
qianghuishi
-
就我剛剛所說的作法,我以Northwind的Region資料表來模擬你的TABLE,並將隨機產生順序的資料儲存到RegionRandom資料表。
use Northwind go if exists (select * from sys.objects where type='U' and name='RegionRandom') drop table RegionRandom go create table RegionRandom ( RegionID int, RegionDesc nvarchar(50) ) select * from RegionRandom
你可以利用下列程式碼先做INSERT產生隨機的順序之後再將RegionRandom的內容顯示到GridView。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class GVSoring : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { SqlDataSource1.InsertCommand = "INSERT INTO [RegionRandom] SELECT * FROM Region ORDER BY NEWID()"; SqlDataSource1.Insert(); SqlDataSource1.SelectCommand = "SELECT * FROM [RegionRandom]"; GridView1.DataBind(); } } } }
提醒您,上述只是提供一個方向,詳細的做法或許得看你真正的需求才行。
以上說明若有錯誤請指教,謝謝。
http://www.dotblogs.com.tw/terrychuang/- 已标记为答案 强辉史 2012年3月30日 7:59
-
太感谢你了,又不辞辛苦地给自己回复,很感激!...
自己目前使用
If DropDownList1.SelectedValue = "阅读程序与分析" Then
SqlDataSource1.ConnectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
SqlDataSource1.SelectCommand = "SELECT TOP " + DropDownList2.SelectedValue + " * FROM 编程 ORDER BY NEWID()"GridView1.DataSourceID = SqlDataSource1.ID
实现了DropDownList2的SelectedValue与SELECT挂上了钩,已经可以随机出教师们在DropDownList2中所选择的试题数量了。
至于插入到新表的方法,自己还想弱弱地问下:你使用的
use Northwind go if exists (select * from sys.objects where type='U' and name='RegionRandom') drop table RegionRandom go create table RegionRandom ( RegionID int, RegionDesc nvarchar(50) ) select * from RegionRandom 这是在哪编写的呢?是在创建存储过程吗?还是在 SQL Server Management Studio中?又或者是别的?
qianghuishi
-
假設我t1有兩個欄位,t2有一個欄位,你要把t2亂數產生順序之後插入到t1,可以在insert的時候指定要插入那些不能為null的欄位。
if exists (select * from sys.objects where type='U' and name='t1') drop table t1 go if exists (select * from sys.objects where type='U' and name='t2') drop table t2 go create table t1 ( col1 int not null, col2 int not null ) create table t2 ( col1 int ) go declare @i int = 1 while @i <= 10 begin insert into t2 values (@i) set @i = @i +1 end insert into t1(col1,col2) select col1,-1 from t2 order by NEWID() select * from t1
以上說明若有錯誤請指教,謝謝。
http://www.dotblogs.com.tw/terrychuang/