抽奖程序:
思路整理,无非就是点一个按钮,然后一个图片旋转一会就出来个结果就行了,可这个程序的要求不是这样的,是需要从数据库中随机抽取用户,根据数据库中指定的等级和人数,一键全部抽出来结果就行了。同时需要存储到数据库。还需要一个导出的功能。
不能遗漏的是,如果通过随机数根据id来抽取的话,需要考虑id不连续的问题,如果全部取出id也不现实。尽量少的去读写数据库。
数据库:
复制代码 代码如下:
CREATE TABLE [dbo].[users](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](50) NOT NULL,
[phone] [nvarchar](50) NULL,
CONSTRAINT [PK_table1] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[result](
[id] [int] IDENTITY(1,1) NOT NULL,
[usersid] [int] NOT NULL,
[awardsid] [int] NOT NULL,
CONSTRAINT [PK_result] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[awards](
[id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[Number] [int] NOT NULL,
CONSTRAINT [PK_awards] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE VIEW [dbo].[view1]
AS
SELECT dbo.result.id AS resultid, dbo.users.id, dbo.users.name, dbo.users.phone, dbo.awards.Name AS awardname
FROM dbo.awards INNER JOIN
dbo.result ON dbo.awards.id = dbo.result.awardsid INNER JOIN
dbo.users ON dbo.result.usersid = dbo.users.id
CREATE PROCEDURE [dbo].[getranddata]
--这个地方的参数是后台调用传的参数,两个变量之间需要“,”号分开
@count int, --剩余奖项大小
@awards int --奖项的id
AS BEGIN
--这个地方定义的参数是存储过程内部用到的
DECLARE @minid int --最大id
DECLARE @maxid int --最小id
DECLARE @randnum int --随机数临时变量
DECLARE @exist int --查询结果
SET @minid =
(SELECT top 1 id
FROM users
ORDER BY id ASC) --查询最小id
SET @maxid =
(SELECT top 1 id
FROM users
ORDER BY id DESC) --查询最大id
--set @count = 100
--set @awards = 1
--嵌套语句begin开始,end结束
while @count>0 BEGIN
SELECT @randnum = ROUND(((@maxid - @minid -1) * RAND() + @minid), 0)
SET @exist =
(SELECT count(*)
FROM users
WHERE id=@randnum) IF @exist = 1 BEGIN
INSERT INTO result(usersid,awardsid)
VALUES(@randnum,
@awards)
SET @count = @count - 1 END END END
其中三张表,一个视图,一个存储过程。
后台代码:
复制代码 代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection sqlcon = new SqlConnection("server=.;database=test;uid=sa;pwd=123");
sqlcon.Open();
SqlDataAdapter sqlsda = new SqlDataAdapter("select * from awards", sqlcon);
ds = new DataSet();
sqlsda.Fill(ds);
DataTable dt = ds.Tables[0].Copy();
ds.Clear();
int count = dt.Rows.Count;
for (int i = 0; i < count; i++)
{
SqlCommand sqlcmd = new SqlCommand("getranddata", sqlcon);
SqlParameter pcount = new SqlParameter("@count", Convert.ToInt32(dt.Rows[i]["Number"]));
SqlParameter pawards = new SqlParameter("@awards", Convert.ToInt32(dt.Rows[i]["id"]));
sqlcmd.Parameters.Add(pcount);
sqlcmd.Parameters.Add(pawards);
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.ExecuteNonQuery();
sqlsda = new SqlDataAdapter("select top " + Convert.ToInt32(dt.Rows[i]["Number"]) + " * from view1 order by resultid desc", sqlcon);
sqlsda.Fill(ds, "t" + i.ToString());
switch (i)
{
case 0:
GridView1.DataSource = ds.Tables["t" + i.ToString()].Copy().DefaultView;
GridView1.DataBind();
break;
case 1:
GridView2.DataSource = ds.Tables["t" + i.ToString()].Copy().DefaultView;
GridView2.DataBind();
break;
case 2:
GridView3.DataSource = ds.Tables["t" + i.ToString()].Copy().DefaultView;
GridView3.DataBind();
break;
default:
break;
}
}
sqlcon.Close();
}
奖项设置:
抽奖结果:
=================================================================
知识点:
SQL - 生成指定范围内的随机数
复制代码 代码如下:
DECLARE @Result INT DECLARE @Upper INT DECLARE @Lower INT
SET @Lower = 1
SET @Upper = 10
SELECT @Result = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Result
ROUND()函数:返回按指定位数进行四舍五入的数值。
RAND()函数:生成随机数。
SQL循环语句嵌套
复制代码 代码如下:
DECLARE @i int
SET @i=1 while @i<8 BEGIN IF @i<5 print space(4-@i)+REPLICATE('*',2*@i-1) ELSE print space(@i-4)+REPLICATE('*',15-2*@i)
SET @i=@i + 1 END
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]