嗨創建一個程序,當表單加載隨機數據將從數據庫中檢索,我的問題是不重複數據的代碼。用vb 2008和SQL數據庫 IM, 編程語言:C#從sql數據庫檢索隨機數據不重複
例如:
Data in Database
Word_ID | Word
1 | eye
2 | cheese
3 | mouse
然後形成其加載將檢索隨機數據並顯示在標籤
示例輸出:
奶酪
所以奶酪現在不會顯示在下一個隨機
我的代碼:
SqlConnection conn =
new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\sony\Documents\Visual Studio 2008\Projects\Hangman_Final\Hangman_Final\hangman_db.mdf;Integrated Security=True;User Instance=True");
conn.Open();
SqlCommand command = conn.CreateCommand();
command.CommandText = "SELECT TOP 1 Words from word order by NEWID()";
command.CommandType = CommandType.Text;
SqlDataReader reader = command.ExecuteReader();
// display the results
while (reader.Read())
{
string output = reader["Words"].ToString();
label5.Text = output;
}
// close the connection
reader.Close();
conn.Close();
而你的問題是......? –
這是什麼問題? –
數據庫中有多少個單詞?根據條目的不同,緩存單詞並在需要時彈出它們會更加可靠,從而確保您永遠不會重複。否則,您將需要測試'WHERE word_id NOT IN(已找到ID)' –