2015-12-20 47 views
1

是否有可能从SQL表中获取数据并传递到随机顺序和随机数据的标签(或变量)?是这样的:从SQL随机顺序检索随机数据到一个标签

SQL数据:

1 Nik 
2 Steve 
3 John 
4 Denny 
5 Joe 
6 Mike 
7 Elena 
8 Michel 

输出应该是每按一下按钮随机:

Joe, Elena, Denny 

下一页点击类似:

Nik, Mike, Steve, Joe, Elen 

顺便说一句数据没有按”必须来自SQL。这在VB.NET中可以做到吗?

回答

0

将所有名称加载到Load事件表单中并将它们存储在成员字段中,然后您可以使用此函数以随机顺序获取随机数的名称。

Public Function Shuffle(source As List(Of String)) As List(Of String) 
    Dim rnd = New Random(Environment.TickCount) 
    Return source.OrderBy(Function(item) rnd.Next()) _ 
       .Take(rnd.Next(1, source.Count)).ToList() 
End Function 

这里是用法:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim list = Shuffle(ListOfNames) 
    MessageBox.Show(String.Join(", ", list)) 
    'Me.Label1.Text = String.Join(", ", list) 
End Sub 

而且得到的数据并将其存储在一个成员字段:

'Store names here 
Dim ListOfNames As List(Of String) 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    Dim connection = "Your Connection String" 
    'Your Select Command, For example: 
    Dim command = "SELECT Name From People" 
    Dim table As New System.Data.DataTable 
    Dim adapter As New System.Data.SqlClient.SqlDataAdapter(command, connection) 
    adapter.Fill(table) 

    'Store names for later use 
    ListOfNames = table.Select().Select(Function(row) row.Field(Of String)(0)).ToList() 
End Sub 
+0

很好的答案,除了打破一个基本的规则:避免使用Load事件处理程序,共同操作](http://stackoverflow.com/questions/4933958/vs2010-does-not-show-unhandled-exception-message-in-a-winforms-application-on-a) - 使用构造函数或显示()处理程序,而不是 – miroxlav

+0

它不起作用,我得到错误:类型'列表(字符串)'的值不能转换为'字符串()'...:P – LazaBre

+0

检查您的代码,您可能定义了'String()'而不是'列表(字符串)'。在我的代码中没有'String()',我使用'List(of String)' –

-1

我已经创建了一个数据在List中的示例。这种定制类Person仅仅是为了获取你原来上面的例子:

public class Person 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
} 

这种方法随机化的名字在List<Person>顺序:

public string GetRandomNames(List<Person> people) 
{ 
    int numberOfPeople = people.Count; 
    string nameLabel; 

    string[] names = new string[numberOfPeople]; 
    Random r = new Random(); 

    for(int i = 0; i<numberOfPeople; i++) 
    { 
     int randomIndex = r.Next(0, people.Count); 
     names[i] = people[randomIndex].Name; 
     people.RemoveAt(randomIndex); 
    } 
    foreach(string name in randomNames) 
    { 
     nameLabel += name + ", "; 
    } 

    return nameLabel; 
} 

对于这个例子的目的,我有创建列表如下。当然,您的列表将来自其他来源,例如您提到的SQL数据库。然后

List<Person> people = new List<Person>(); 
people.Add(new Person() { ID = 1, Name = "Nik" }); 
people.Add(new Person() { ID = 2, Name = "Steve" }); 
people.Add(new Person() { ID = 3, Name = "John" }); 
people.Add(new Person() { ID = 4, Name = "Denny" }); 
people.Add(new Person() { ID = 5, Name = "Joe" }); 
people.Add(new Person() { ID = 6, Name = "Mike" }); 
people.Add(new Person() { ID = 7, Name = "Elena" }); 
people.Add(new Person() { ID = 8, Name = "Michel" }); 

用法是沿着线的东西:

string nameLabel = GetRandomNames(people); 

请让我知道这不回答你的问题。

+0

我所看到的,这是C++代码...我需要VB.NET:P – LazaBre

0

如果每次查询的数据库,你可以添加按条款排序。 假设MS SQL,像

select * 
from people 
order by newid()