.net+mssql制作抽奖程序思路及源码

抽奖程序:


思路整理,无非就是点一个按钮,然后一个图片旋转一会就出来个结果就行了,可这个程序的要求不是这样的,是需要从数据库中随机抽取用户,根据数据库中指定的等级和人数,一键全部抽出来结果就行了。同时需要存储到数据库。还需要一个导出的功能。


不能遗漏的是,如果通过随机数根据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

原创文章,作者:bd101bd101,如若转载,请注明出处:https://blog.ytso.com/234729.html

(0)
上一篇 2022年1月23日
下一篇 2022年1月23日

相关推荐

发表回复

登录后才能评论