版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、實(shí)驗(yàn)04簡(jiǎn)單考試頁(yè)面 設(shè)計(jì)并實(shí)現(xiàn)一個(gè)用于單項(xiàng)選擇題和多項(xiàng)選擇題的測(cè)試頁(yè)面 數(shù)據(jù)庫(kù)表的定義如下單項(xiàng)選擇題表question多項(xiàng)選擇題表questioncheck 列名數(shù)據(jù)類型屈序號(hào)int 題目名itchar (100)0選項(xiàng)Aachar回1選項(xiàng)BTtcliar $口)0 1選頂CjLcliair C50)0 1選頊Dnxhar C50)0選頂En-ctiar (SU)0 1分值i nt0 1Tichar (10)0正確答案iictiar (10)0成績(jī)表score1.vconnectionStrings >< add name=" shiti " provider
2、Name ="" connectionstring =" Data Source=.;lnitialCatalog=shiti;lntegrated Security=True;Pooling=False"/></ connectionStrings >或者下面的連接語(yǔ)句vconnectionStrings<add name="shitiConnectionString" connectionString="Data Source=.;lnitialCatalog=shiti;User ID=sa;P
3、ooling=False;Password=sa" providerName="System.Data.SqlClient" /></connectionStrings>2. 公共數(shù)據(jù)連接類代碼為: public class DataConnpublic DataConn() public static string strconn =ConfigurationManager .ConnectionStrings "shiticonn" .ConnectionString;public static SqlConnection
4、 CreateConn()SqlConnection conn = new SqlConnection (strconn);return conn;public static SqlDataReader ExecuterRead( string cmdtext, SqlConnection conn) SqlCommandcmd = new SqlComman(d);cmd.Connection = conn;cmd.CommandText = cmdtext;SqlDataReader dr = cmd.ExecuteReader();return dr;paramspublic stati
5、c SqlDataReader ExecuterRead( string cmdtext, SqlConnection conn, SqlParameter array1)SqlCommandcmd = new SqlComman(d);cmd.Connection = conn;cmd.CommandText = cmdtext;foreach ( SqlParameter para in array1)cmd.Parameters.Add(para);SqlDataReader dr = cmd.ExecuteReader();return dr;public static int Exe
6、cuteNonRead( string cmdtext)SqlConnection conn = CreateConn();conn.Open();SqlCommandcmd = new SqlComman(dcmdtext, conn);int i = cmd.ExecuteNonQuery();conn.Close();return i;public static int ExecuteNonRead( string cmdtext, params SqlParameter array) SqlConnection conn = CreateConn();conn.Open();SqlCo
7、mmancCmd = new SqlComman(tmdtext, conn);foreach ( SqlParameter para in array)cmd.Parameters.Add(para);int i = cmd.ExecuteNonQuery();conn.Close();return i;public static object ExecuteQueryValue( string cmdtext, params SqlParameter array) SqlConnection conn = CreateConn();conn.Open();SqlCommanctmd = n
8、ew SqlComman(tmdtext, conn);foreach ( SqlParameter para in array)cmd.Parameters.Add(para);object o = cmd.ExecuteScalar();conn.Close();return o;public static object ExecuteQueryValue( string cmdtext)SqlConnection conn = CreateConn();conn.Open();SqlCommanctmd = new SqlComman(tmdtext, conn);object o =
9、cmd.ExecuteScalar();conn.Close();return o;3.登錄頁(yè)面設(shè)計(jì)|查著刪|皆除考試信息用戶名 密碼 并始考試開始考試按鈕的事件代碼為:protected void ButtonStartExam_Click( object sender, EventArgs e)SqlConnection conn = DataConn.CreateConn();conn.Open();/ string cmdtext = "select * from usertable where name= '" + TextBoxName.Text + &
10、quot;"'/ SqlDataReader dr= dataconn.ExecuterRead(cmdtext, conn);string cmdtext = "select * from用戶信息 where 用戶名 =username "SqlDataReader dr = DataConn.ExecuterRead(cmdtext, conn, new SqlParameter ( "username", TextBoxName.Text);if (dr.Read()if (dr " 密碼 " .ToStrin
11、g().Trim() = TextBoxPassword.Text.Trim()dr.Close();cmdtext ="select * from score where用戶名 =username1"if ( DataConn.ExecuterRead(cmdtext, conn,new SqlParameter ( "username1TextBoxName.Text).Read()Response.Write("<script> alert('該用戶已經(jīng)參加過考試 ')</script>" );el
12、seSessionusername" = TextBoxName.Text;Response.Redirect("exam.aspx" );elseResponse.Write("<script> alert('密碼錯(cuò)誤 ')</script>");elseResponse.Write("<script> alert('不存在該用戶 ')</script>");conn.Close();注冊(cè)新用戶按鈕的事件代碼為:protected void B
13、uttonRegister_Click( object sender, EventArgs e)/string cmdtext = "insert into usertable(name,password) values( '" + TextBoxName.Text+ "','" + TextBoxPassword.Text + "')"SqlConnection conn = DataConn.CreateConn();conn.Open();string cmdtext = "select
14、 * from用戶信息 where 用戶名 =username "SqlParameter para = new SqlParameter ( "username", TextBoxName.Text); SqlDataReader dr = DataConn.ExecuterRead(cmdtext, conn, para);if (dr.Read()Response.Write( "<script> alert(' 該用戶名已經(jīng)被使用! ')</script>" ); conn.Close();els
15、ecmdtext ="insert into用戶信息 ( 用戶名 , 密碼 ) values(username,password)"SqlParameter p1 = new SqlParameter ( "username", TextBoxName.Text);SqlParameter p2 = new SqlParameter ( "password" , TextBoxPassword.Text); if ( DataConn.ExecuteNonRead(cmdtext, p1, p2) = 1)Response.Write
16、("<script> alert('注冊(cè)成功 ')</script>" );查看成績(jī)按鈕的事件代碼為:protected void ButtonLookOverScore_Click( object sender, EventArgs e)SqlConnection conn = DataConn.CreateConn();conn.Open();/ string cmdtext = "select * from usertable where name= '" + TextBoxName.Text + &
17、quot;'"/ SqlDataReader dr= dataconn.ExecuterRead(cmdtext, conn);string cmdtext = "select * from 用戶信息 ,score where 用戶信息 . 用戶名 =usernameand 用 戶信息 . 用戶名 =score. 用戶名 "SqlParameter para = new SqlParameter ( "username", TextBoxName.Text);SqlDataReader dr = DataConn.ExecuterRea
18、d(cmdtext, conn, para); if (dr.Read()if (dr " 密碼 " .ToString().Trim() = TextBoxPassword.Text.Trim()Session "username" = TextBoxName.Text;Response.Redirect(elseResponse.Write(elseResponse.Write("LookOverScore.aspx" );"<script> alert(' 密碼錯(cuò)誤 ')</scrip
19、t>" );"<script> alert(' 你還沒有參加過考試或者不存在該用戶')</script>" ); conn.Close();去除考試信息按鈕的事件代碼為:protected void ButtonClearExamInfo_Click(object sender,EventArgs e)用戶名 =usernamenew SqlParameterstring cmdtext = "select count(*)from score where object obj= DataConn .Exec
20、uteQueryValue (cmdtext ,( "username",TextBoxName .Text );if ( int .Parse(obj.ToString() = 1)/ 首先判斷該用戶是否參加過考試cmdtext ="drop table danxuandaan"DataConn.ExecuteNonRead(cmdtext); cmdtext ="drop table duoxuandaan"DataConn.ExecuteNonRead(cmdtext);+ TextBoxName.Text;/ 去除該考生的單項(xiàng)
21、選擇答案表+ TextBoxName.Text;/ 去除該考生的多項(xiàng)選擇答案表cmdtext ="delete from score where用戶名 =username"DataConn.ExecuteNonRead(cmdtext,new SqlParameter ( "username",TextBoxName.Text);/ 去除該考生的考試成績(jī)elseResponse.Write("<script> alert('該用戶沒有參加過考試 ')</script>" );4. 考試頁(yè)面設(shè)計(jì)在
22、設(shè)計(jì)界面上添加2個(gè)PlaceHolder,個(gè)用于添加單項(xiàng)選擇題,一個(gè)用于添加多項(xiàng)選擇題。放置一個(gè)label控件LabelUserName,用戶顯示用戶名信息考試頁(yè)面的初始化代碼為protected void Page_Load( object sender, EventArgs e)if (!lsPostBack)if (Session "username" = null )Response.Redirect("login.aspx" );LabelUserName.Text = LabelUserName.Text + Session"use
23、rname" .ToString();LoadDanXuanExam();LoadDuoXuanExam();加載單項(xiàng)選擇題private void LoadDanXuanExam()SqlConnection conn = DataConn.CreateConn();conn.Open();string cmdtext = "select * from question "SqlCommandcmd = new SqlComman(dcmdtext, conn);SqlDataReader dr = cmd.ExecuteReader();while (dr.R
24、ead()Label lblquestion = new Label (); lblquestion.ID = "lblquestiondan" + dr " 序號(hào) " .ToString(); lblquestion.Text = dr" 序號(hào) " .ToString() +". " + dr " 題目名 ".ToString().Trim() +" "PlaceHolder1.Controls.Add(lblquestion);RadioButtonList rbl =
25、new RadioButtonList ();rbl.Items.Add(newListItem("A."+ dr"選項(xiàng) A".ToString(),"A" );rbl.Items.Add(newListItem("B."+ dr"選項(xiàng) B".ToString(),"B" );rbl.Items.Add(newListItem("C."+ dr"選項(xiàng) C".ToString(),"C" );rbl.Items.Add
26、(newListItem("D."+ dr"選項(xiàng) D".ToString(),"D" );PlaceHolder1.Controls.Add(rbl);conn.Close();加載多項(xiàng)選擇題private void LoadDuoXuanExam()SqlConnection conn = DataConn.CreateConn();conn.Open();string cmdtext = "select * from questioncheck "SqlCommandcmd = new SqlComman(dc
27、mdtext, conn);SqlDataReader dr = cmd.ExecuteReader();while (dr.Read()Label lblquestion = new Label ();lblquestion.ID = "lblquestionduo" + dr " 序號(hào) " .ToString();lblquestion.Text = dr" 序號(hào) " .ToString() +". " + dr " 題目名 ".ToString().Trim()" "P
28、laceHolder2.Controls.Add(lblquestion);CheckBoxListcbl =new CheckBoxList ();cbl.Items.Add(newListItem("A."+ dr"選項(xiàng) A".ToString(),"A" );cbl.Items.Add(newListItem("B."+ dr"選項(xiàng) B".ToString(),"B" );cbl.Items.Add(newListItem("C."+ dr"
29、;選項(xiàng) C".ToString(),"C" );cbl.Items.Add(newListItem("D."+ dr"選項(xiàng) D".ToString(),"D" );cbl.Items.Add( new ListItem ("E." + dr " 選項(xiàng) E".ToString(),"E" );PlaceHolder2.Controls.Add(cbl);conn.Close();提交按鈕的事件代碼為protected void ButtonSubm
30、it_Click( object sender, EventArgs e)Submitdanxuan();Submitduoxuan();int duoxuanscore = 0; int danxuanscore = 0;string cmdtext = "select sum( 分值 ) from questioncheck ,duoxuandaan" + Session "username" .ToString() + " where 序號(hào) = xuhao and daan= 正確答案 "object obj = DataCon
31、n.ExecuteQueryValue(cmdtext);if (obj.ToString ()!="" ) duoxuanscore= int .Parse(obj.ToString (); / 通過判斷防止沒有一個(gè)正確答案時(shí), obj 的值為空字符串時(shí)產(chǎn)生錯(cuò)誤/ 計(jì)算多項(xiàng)選擇分值cmdtext = "select sum( 分值 ) from question ,danxuandaan" + Session "username" .ToString()+ " where 序號(hào) = xuhao and daan= 正確答案
32、 "obj =DataConn.ExecuteQueryValue(cmdtext);if (obj.ToString ()!="" ) danxuanscore= int .Parse(obj.ToString ();/ 計(jì)算單項(xiàng)選擇分值cmdtext ="insert into score (用戶名 , 成績(jī) )values (username,score)" ;SqlParameter p1 = new SqlParameter ( "username", Session "username" .T
33、oString();SqlParameter p2= new SqlParameter ( "score" , danxuanscore +duoxuanscore ); DataConn.ExecuteNonRead(cmdtext ,p1,p2);/ 存儲(chǔ)成績(jī)信息多項(xiàng)選擇題提交private void Submitduoxuan()string cmdtext = "create table duoxuandaan" + Session "username" .ToString() +"(xuhaoint,daan nc
34、har(10)"DataConn.ExecuteNonRead(cmdtext);int i = 1;foreach ( Control control in PlaceHolder2.Controls)if (control is Label )i = int .Parse( Label )control).ID.Substring(14);if (control is CheckBoxList )string daan = "" ;foreach ( ListItem cc in ( CheckBoxList )control).Items)if (cc.Se
35、lected) daan = daan + cc.Value.ToString();cmdtext ="insert into duoxuandaan"+ Session "username" .ToString() +"(xuhao,daan) values(xuhao,daan)" ;SqlParameter p1 = new SqlParameter ( "xuhao", i);SqlParameter p2 = new SqlParameter ( "daan", daan); DataConn.ExecuteNonRead(cmdtext, p1, p2);單項(xiàng)選擇題提交private void Submitdanxuan()"(xuhaostring cmdtext = "create table danxuandaan" + Session "username" .ToString() + int,daan nchar(10)
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 電力運(yùn)維臨時(shí)工勞動(dòng)合同樣本
- 水源地暖系統(tǒng)安裝協(xié)議
- 城市廣場(chǎng)安保人員招聘協(xié)議
- 數(shù)據(jù)中心資料員合同樣本
- 橋梁建設(shè)泵機(jī)租賃合同
- 餐飲店外墻涂料施工合同
- 軟件技術(shù)研發(fā)合作合同
- 冷鏈物流解決方案施工合同
- 酒吧主題派對(duì)策劃合同
- 銀行貸款延期協(xié)議
- 部編版歷史九年級(jí)上冊(cè)第六單元 第17課君主立憲制的英國(guó)【課件】y
- 2023年EHS工作年度總結(jié)及2024年工作展望(新版)
- 食品風(fēng)味研究專題智慧樹知到期末考試答案章節(jié)答案2024年中國(guó)農(nóng)業(yè)大學(xué)
- 16J914-1 公用建筑衛(wèi)生間
- 貸前調(diào)查工作指引
- 鐵路隧道安全風(fēng)險(xiǎn)評(píng)估
- 國(guó)有企業(yè)勞動(dòng)用工管理辦法模版
- ISO14001-2015內(nèi)審檢查表(完整記錄)
- yy娛樂頻道設(shè)計(jì)方案模板(簡(jiǎn)約版)
- 胃舒平藥片中Al2O3及MgO含量的測(cè)定
- 彌漫大b細(xì)胞淋巴瘤(初治)臨床路徑
評(píng)論
0/150
提交評(píng)論