【发布时间】:2017-09-10 10:37:49
【问题描述】:
请问,如何随机化收集的数据并随机化使用 SQL 选择的数据 我会将它集成到 c# winform 中
例如
主要内容-----content1-----content2-----content3-----content4
到
主要内容-----content1-----content4-----content2-----content3
就像选择题一样,随机选择问题及其选项。提前致谢。
编辑:
这是我的课。
public class qbank
{
string question, c1, c2, c3, c4, ans;
public string Ans
{
get { return ans; }
set { ans = value; }
}
public string C1
{
get { return c1; }
set { c1 = value; }
}
public string C2
{
get { return c2; }
set { c2 = value; }
}
public string C3
{
get { return c3; }
set { c3 = value; }
}
public string C4
{
get { return c4; }
set { c4 = value; }
}
public string Question
{
get { return question; }
set { question = value; }
}
这是我的winform
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace frmMain
{
public partial class frmPIPETest : Form
{
string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=C:\Users\rehpe\Documents\Visual Studio 2015\Projects\panibago\questionbank.accdb;
Persist Security Info=False";
string query = "";
OleDbConnection conn = null;
public frmPIPETest()
{
InitializeComponent();
}
private void frmPIPETest_FormClosing(object sender, FormClosingEventArgs e)
{
System.Media.SystemSounds.Beep.Play();
DialogResult dialog = MessageBox.Show("Do you really want to exit?",
"Exit Program",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
if (dialog == DialogResult.Yes)
{
Application.ExitThread();
}
else
{
e.Cancel = true;
}
}
private void btnBack_Click(object sender, EventArgs e)
{
System.Media.SystemSounds.Beep.Play();
DialogResult dialog = MessageBox.Show("Go back to topic selection?",
"Return",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
if (dialog == DialogResult.Yes)
{
frmPIPE frm = new frmPIPE();
frm.Show();
Hide();
}
}
int i = 0;
private void timer1_Tick(object sender, EventArgs e)
{
i++;
lblTimer.Text = i.ToString() + "s";
}
public qbank()
{
IEnumerable<qbank> content = new List<qbank>
var random = new Random();
var result = content
.Select(c =>
{
var strings = new[]
{
c.C1,
c.C2,
c.C3,
c.C4,
}.OrderBy(x => random.Next())
.ToArray();
return new qbank
{
Question = c.Question,
C1 = strings[0],
C2 = strings[1],
C3 = strings[2],
C4 = strings[3],
};
})
.OrderBy(x => random.Next());
}
}
}
我附上图片,因为我创建列表的位置有错误。
【问题讨论】:
-
为什么不在 C# 中随机化呢?
-
内容是通过SQL生成的吗?可以和我们分享一下代码吗?
-
@Odonno 我如何在 c# 中随机化?对不起
-
@sagi 我有一个连接到我的 winform 的数据库(访问)。我仍然没有代码,因为我不知道如何查询它。我试图查询随机数据。但我所能做的就是随机化它的行,我也想随机化选定的列。
-
@JepherJohnChang 这是一个例子 (stackoverflow.com/questions/5383498/…)。您可能需要创建一个包含所有选择的 List
,然后使用方法对其进行随机化。