从datareader中选择随机记录并将其放入标签中 [英] Selecting random records from datareader and putting it in label

查看:62
本文介绍了从datareader中选择随机记录并将其放入标签中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友我正在尝试制作一个考试申请书,我希望在表格上的标签上显示随机问题。所以考生不应该按顺序得到每一个问题。

我应该如何实现它?请告诉我。

目前我只能选择标签中数据阅读器的第一条记录。这是代码

  Imports 系统
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
公开 Form1
' 创建ADO.NET对象。
公共 myConn 作为 SqlConnection
公开 myCmd As SqlCommand
公共 myReader 作为 SqlDataReader
公开 myDs 作为 DataSet
公共 myAdp 作为 SqlDataAdapter
公共结果作为 字符串
公共 n 作为 整数


私有 Sub Form1_Load( ByVal sender As System。 Object ByVal e As System.EventArgs)句柄 MyBase .Load

' 创建一个Connection对象。
myConn = SqlConnection( Data Source = testvm; Initial Catalog = ULSLLIVE; Integrated Security = True;
' 创建一个Command对象。
myCmd = myConn.CreateCommand
myCmd.CommandText = SELECT * FROM questions
' 打开连接。
myConn.Open()
myReader = myCmd.ExecuteReader()
myReader.Read()
.Label1.Text = myReader( q)
Me .RadioButton1.Text = myReader( a1
.RadioButton2.Text = myReader( a2
.RadioButton3.Text = myReader( a3
.RadioButton4.Text = myReader( a4

结束 Sub
结束

解决方案

如果将sql更改为是

 myCmd.CommandText =   SELEC T TOP 1 *来自问题订购NEWID() 

您将收到一个随机问题。

如果你想获得超过1,那么改变 TOP 1 。在这种情况下,您还需要使用

 继续读取其余的返回记录,而(myReader.Read())


Hello friends i am trying to make a examination application in which i want to show random questions in labels on form.so that the examinees should not get every question in sequence.
how should i achieve it?pls tell me.
currently i am able to select only the first record of the data reader in label.here's the code

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class Form1
    'Create ADO.NET objects.
    Public myConn As SqlConnection
    Public myCmd As SqlCommand
    Public myReader As SqlDataReader
    Public myDs As New DataSet
    Public myAdp As SqlDataAdapter
    Public results As String
    Public n As Integer
    

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
        'Create a Connection object.
        myConn = New SqlConnection("Data Source=testvm;Initial Catalog=ULSLLIVE;Integrated Security=True;")
        'Create a Command object.
        myCmd = myConn.CreateCommand
        myCmd.CommandText = "SELECT * FROM questions"
        'Open the connection.
        myConn.Open()
        myReader = myCmd.ExecuteReader()
        myReader.Read()
        Me.Label1.Text = myReader("q")
        Me.RadioButton1.Text = myReader("a1")
        Me.RadioButton2.Text = myReader("a2")
        Me.RadioButton3.Text = myReader("a3")
        Me.RadioButton4.Text = myReader("a4")

    End Sub
End Class

解决方案

If you change your sql to be

myCmd.CommandText = "SELECT TOP 1 * FROM questions ORDER BY NEWID()

you will get a single random question.
If you want to get more than 1 then change the TOP 1. In that instance you will also need to continue reading the rest of the returned records using

while(myReader.Read())


这篇关于从datareader中选择随机记录并将其放入标签中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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