您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān).net+mssql制作抽獎(jiǎng)程序思路及源碼怎么編寫,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
抽獎(jiǎng)程序:
思路整理,無非就是點(diǎn)一個(gè)按鈕,然后一個(gè)圖片旋轉(zhuǎn)一會(huì)就出來個(gè)結(jié)果就行了,可這個(gè)程序的要求不是這樣的,是需要從數(shù)據(jù)庫(kù)中隨機(jī)抽取用戶,根據(jù)數(shù)據(jù)庫(kù)中指定的等級(jí)和人數(shù),一鍵全部抽出來結(jié)果就行了。同時(shí)需要存儲(chǔ)到數(shù)據(jù)庫(kù)。還需要一個(gè)導(dǎo)出的功能。
不能遺漏的是,如果通過隨機(jī)數(shù)根據(jù)id來抽取的話,需要考慮id不連續(xù)的問題,如果全部取出id也不現(xiàn)實(shí)。盡量少的去讀寫數(shù)據(jù)庫(kù)。
數(shù)據(jù)庫(kù):
代碼如下: 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]ASSELECT dbo.result.id AS resultid, dbo.users.id, dbo.users.name, dbo.users.phone, dbo.awards.Name AS awardnameFROM 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] --這個(gè)地方的參數(shù)是后臺(tái)調(diào)用傳的參數(shù),兩個(gè)變量之間需要“,”號(hào)分開@count int, --剩余獎(jiǎng)項(xiàng)大小@awards int --獎(jiǎng)項(xiàng)的idAS BEGIN --這個(gè)地方定義的參數(shù)是存儲(chǔ)過程內(nèi)部用到的DECLARE @minid int --最大idDECLARE @maxid int --最小idDECLARE @randnum int --隨機(jī)數(shù)臨時(shí)變量DECLARE @exist int --查詢結(jié)果
SET @minid = (SELECT top 1 id FROM users ORDER BY id ASC) --查詢最小idSET @maxid = (SELECT top 1 id FROM users ORDER BY id DESC) --查詢最大id--set @count = 100--set @awards = 1--嵌套語句begin開始,end結(jié)束while @count>0 BEGINSELECT @randnum = ROUND(((@maxid - @minid -1) * RAND() + @minid), 0)SET @exist = (SELECT count(*) FROM users WHERE id=@randnum) IF @exist = 1 BEGININSERT INTO result(usersid,awardsid)VALUES(@randnum, @awards)SET @count = @count - 1 END END END
其中三張表,一個(gè)視圖,一個(gè)存儲(chǔ)過程。
后臺(tái)代碼:
代碼如下: 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();}
獎(jiǎng)項(xiàng)設(shè)置:
抽獎(jiǎng)結(jié)果:
=================================================================
知識(shí)點(diǎn):
SQL - 生成指定范圍內(nèi)的隨機(jī)數(shù)
代碼如下: DECLARE @Result INT DECLARE @Upper INT DECLARE @Lower INTSET @Lower = 1SET @Upper = 10SELECT @Result = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)SELECT @Result
ROUND()函數(shù):返回按指定位數(shù)進(jìn)行四舍五入的數(shù)值。
RAND()函數(shù):生成隨機(jī)數(shù)。
SQL循環(huán)語句嵌套
代碼如下: DECLARE @i intSET @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
上述就是小編為大家分享的.net+mssql制作抽獎(jiǎng)程序思路及源碼怎么編寫了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。