目前,我正在使用幸运抽奖系统。
系统的过程是当用户点击绘图按钮时,它会从attendance="Present"
的数据库中选择随机ID。
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
string constr = ConfigurationManager.ConnectionStrings["lucky"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string query = "SELECT EMP_ID FROM EMPLOYEES WHERE 'Attendance' = 'Present' ORDER BY RAND() LIMIT 1 ";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
Label1.Text = dt.Rows[0]["EMP_ID"].ToString();
}
}
}
}
}
}
}
答案 0 :(得分:2)
根据您的评论,您似乎正在使用SQL Server Express LocalDB,因此您应该可以使用以下内容:
SELECT TOP 1 [EMP_ID]
FROM EMPLOYEES
WHERE [Attendance] = 'Present'
ORDER BY NEWID()
通过使用NEWID()
,每行将被赋予GUID,然后表格将相应地进行排序。
NEWID()
创建uniqueidentifier类型的唯一值。