2015-12-20 94 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()