版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、基于C#的新闻投票系统的实现 作者: 日期:2 个人收集整理 勿做商业用途目录1引言12需求分析12。1 需求说明22。2功能说明22.3问题描述23设计说明。.。23.1简要分析23.2概要分析33。2.1功能图如下33.2.2数据库设计43.2。3代码文件部分54源程序与执行部分54.1 主页面部分54.2 添加投票主题74。3 添加投票选项部分94。4 投票界面部分124.5显示结果部分165设计心得186参考文献19基于C#的新闻投票系统的实现1引言C#是微软公司在二OOO年六月发布的一种新的编程语言,并定于在微软职业开发者论坛(PDC)上登台亮相。它是微软公司研究员Anders He
2、jlsberg的最新成果.C#看起来与Java有着惊人的相似;它包括了诸如单一继承、界面、与Java几乎同样的语法,和编译成中间代码再运行的过程。但是C与Java有着明显的不同,它借鉴了Delphi的一个特点,与COM(组件对象模型)是直接集成的,而且它是微软公司.NET windows网络框架的主角。C#是一种最新的、面向对象的编程语言。它使得程序员可以快速地编写各种基于MICROSOFT 。NET平台的应用程序,MICROSOFT 。NET提供了一系列的工具和服务来最大程度地开发利用计算与通讯领域.正是由于C面向对象的卓越设计,使它成为构建各类组件的理想之选-无论是高级的商业对象还是系统级
3、的应用程序。使用简单的C#语言结构,这些组件可以方便的转化为XML网络服务,从而使它们可以由任何语言在任何操作系统上通过INTERNET进行调用。SQL结构化查询语言.SQL语言的主要功能就是同各种数据库建立联系,进行沟通。按照ANSI(美国国家标准协会)的规定,SQL被作为关系型数据库管理系统的标准语言。SQL语句可以用来执行各种各样的操作,例如更新数据库中的数据,从数据库中提取数据等。SQL Server 是一个在Window平台上开发的关系数据库管理系统,用来保存数据并提供一套方法来操纵维护和管理这些数据,同时扮演服务器的角色,来响应来自客户端的连接和数据访问请求,开发好的数据客户端程序
4、接收用户数据输入查询请求,通过网络传给SQL Server,保存在数据库中或由SQL Server执行查询命令,前端程序等待接收数据并显示在终端界面.本次程序设计主要用c语言编写客户端,管理员管理后台,通过前端界面的操作把用户的相关投票数据存入数据库,并可以读取数据库在前端显示相应结果。主要是大量控件的应用及与数据库的连接.2需求分析2.1 需求说明从国际互联网到校园网,企业局域网,各种网上投票系统随处可见,意见调查,用户信息统计,经营情况调查都可以作为投票的内容,网上投票系统凭借其方便快捷等特点,已经成为互联网资源中不可缺少的一部分,并且,随着网络技术的发展,网上投票系统的作用将会越来越大.
5、在投票的过程中对网络的要求也越来越高。它要求信息安全保密性,投票者有保密的要求,如用户名及投票内容被人知悉,就对用户的隐私权构成了侵,这就要求对其存储的数据库进行加密。此外投票唯一性也是一个重要的方面,一个投票者,其投票次数应当只有一次。投票唯一性实质上也是投票者身份的有效性,只有在确认投票者身份有效的前提下,才能保证用户投票的唯一性。2。2功能说明此次设计应满足以下几个功能:增加投票,同一IP地址每天只能投票一次,并有相应的提示;必须选择观点,限制空投;此投票可以单选,也可以多选投票;用户可自行发起主题及选项投票;查看和统计投票信息:能够统计出不同主题对于不同观点的投票数。2.3问题描述在现
6、实生活中,为了使自己的网络营业及服务越来越迎合广大对象的口味,或者对自己进行一些测试,既安全又方便.因此实行投票制在现实生活中必不可少的一部分。在本次实训的目的是设计一个自由的网络投票系统。在投票过程中,用户可以自由选择开始投票,删除,并修改.这些是对投票系统客户端的基本要求.在设计的过程中,要考虑系统的安全性。因为投票系统必须保证它的完整性,不允许任何人篡改,否则就失去了它原有的目的和意义。3设计说明3。1简要分析在本次实验中主要应用了Web技术和超文本HTTP协议以及超文本标记语言HTML。采用了C#语言,结合HTML代码,使用VS2010编辑完成投票应用程序的编写。其中结合数据库才能完成
7、相应其功能,因此使用SQL Server建立一个数据库,里面存放相应用户信息,投票信息,管理信息等。然后又通过web控件及ADO组件访问数据库,将数据库和网页的相关操作联系起来,从而使投票的各功能得以实现。具体体现如下:投票界面返回结果数据库控件语句操作访问数据库返回相应的操作结果3。2概要分析本次设计中的主要部分,数据库和c#程序代码设计。3.2.1功能图如下3.2。2数据库设计T_ip :字段名称数据类型字段大小必填字段默认值字段说明IPid数字整型是无Ip地址编号qid数字整形是无问题编号ip文本是无Ip地址lasttime时间datetime是无投票时间T_question:字段名称数
8、据类型字段大小必填字段默认值字段说明qcontent文本40是无问题内容qid数字整形是无问题编号qtype数字整形是无投票类型T_answer :字段名称数据类型字段大小必填字段默认值字段说明acontent文本40是无选项内容qid数字整形是无问题编号aid数字整形是无选项编号count数字整形是0选项票数各表关系图:3.2.3代码文件部分index。aspx 进入后的主界面viewVote.aspx 用于进行投票的部分viewResult.aspx 用于显示投票结果addOption.aspx 用户添加投票选项部分addQuestion。aspx 添加投票主题部分4源程序与执行部分4。1
9、 主页面部分index。aspx主界面代码:body form id=”form1” runat=”server” diva href=”addQuestion。aspx?voteType=1”发起投票/abr /</div <asp:Panel ID=”Panel1" runat=”server" GroupingText=”投票列表” Font-Bold="True" div style=”float:left; width:400px" 单选投票:<br /> div style="padding-lef
10、t:20px”> asp:ObjectDataSourceID="ObjectDataSource1"runat="server” DeleteMethod="Delete”InsertMethod=”Insert”OldValuesParameterFormatString=”original_0”SelectMethod=”GetDataByType1"TypeName=”vote.DAL.questionTableAdapters。T_questionTableAdapter” UpdateMethod="Update&q
11、uot;> <DeleteParameters <asp:Parameter Name=”Original_qid” Type=”Int32" /> /DeleteParameters InsertParameters> asp:Parameter Name=”qcontent" Type=”String”/asp:ParameterName=”qtype"Type=”Int32”/InsertParameters> <UpdateParametersasp:ParameterName="qcontent”Typ
12、e="String”/> <asp:ParameterName=”qtype”Type=”Int32"/<asp:ParameterName="Original_qid" Type="Int32" />/UpdateParameters>/asp:ObjectDataSourceasp:RepeaterID="Repeater1"runat="server”DataSourceID=”ObjectDataSource1" >ItemTemplate>ah
13、ref='viewVote.aspx?qid=<%#Eval("qid")>&type=1*<#Eval("qContent")></abr / </ItemTemplate> /asp:Repeater> </div /div <div style=”float:left"> 多选投票<br / <div style=”padding-left:20px"> <DeleteParameters <asp:Parameter
14、Name="Original_qid” Type="Int32” /> /DeleteParameters InsertParametersasp:ParameterName="qcontent" Type="String" / asp:ParameterName=”qtype" Type=”Int32” /> </InsertParameters UpdateParameters>asp:ParameterName="qcontent" Type=”String" /&
15、gt; asp:ParameterName=”qtype” Type="Int32" / asp:Parameter Name="Original_qid” Type="Int32” / </UpdateParameters> </asp:ObjectDataSource>asp:RepeaterID=”Repeater2"runat=”server”DataSourceID=”ObjectDataSource2” ItemTemplate> a href=viewVote.aspx?qid=<%Eval(&
16、quot;qid")%type=2'>*<%#Eval("qContent")%></a<br /> /ItemTemplate /asp:Repeater> /div> /div></asp:Panel </form></body>执行界面如下:界面显示图4.2 添加投票主题addQuestion.aspx添加主题界面代码:<body>form id=”form1” runat=”server"<asp:Literal ID="Lit
17、eral1" runat="server”<b>发起投票/b>/asp:Literalasp:RadioButton ID=”RadioButton1" runat=”server” Text=”单选” GroupName="choseType” Checked=”true”/ asp:RadioButton ID=”RadioButton2" runat=”server" Text="复选” GroupName="choseType"/> <hr /> <p&g
18、t;请输入问题:br / asp:TextBox ID="question” runat=”server" Width=”315px” /asp:TextBox<div> <asp:Button ID=”Button1” runat=”server” onclick=”Button1_Click” Text=”Button” />nbsp;asp:Button ID="Button2" runat="server” onclick=”Button2_Click" Text="Button"
19、/ /div> /form>/body>添加主题界面相应控件触发代码及数据库连接部分:addQuestion.aspx.cs:public partial class addQuestion : System.Web。UI。Page protected void Page_Load(object sender, EventArgs e) if (!IsPostBack) Button1.Text = ”提交" Button2.Text = "清空”; protected void Button1_Click(object sender, EventArgs
20、 e) string qContent = question.Text; int qid = 0; /选择单选复选 int qType = 1; if (RadioButton2。Checked) qType = 2; SqlConnection conn = new SqlConnection(”Data Source=。SQLEXPRESS;AttachDBFilename=|DataDirectoryMyVote。mdf;Integrated Security=True;User Instance=True"); conn.Open(); SqlCommand cmd = co
21、nn。CreateCommand(); cmd。CommandText = "insert into T_question(qcontent,qtype) values(content,qType)" cmd。Parameters.AddWithValue(”content", qContent); cmd.Parameters。AddWithValue("qType", qType); cmd。ExecuteNonQuery(); SqlDataReader reader = cmd。ExecuteReader(); if (reader。R
22、ead()) qid = (int)reader。GetValue(0); cmd.Dispose();conn.Dispose(); Response。AppendCookie(cookie); Response.AppendCookie(cookie2); Response。Redirect("addOption.aspx”); protected void Button2_Click(object sender, EventArgs e) if (IsPostBack) question.Text = "" 执行界面图:用户填写主题界面用户提交后的界面4。3
23、 添加投票选项部分addOption。aspx添加投票选项界面代码:body form id=”form1” runat=”server"> div<asp:ObjectDataSourceID=”ObjectDataSource1” runat=”server” DeleteMethod=”Delete" InsertMethod=”Insert” OldValuesParameterFormatString="original_0” SelectMethod="GetDataByQid" TypeName=”vote。DAL.o
24、ptionDsTableAdapters.T_answerTableAdapter" UpdateMethod="Update" SelectParameters><asp:CookieParameter CookieName="qid" Name=”qid" Type=”String" / </SelectParameters asp:Literal ID=”Literal2" runat="server”>/asp:Literal> <asp:Literal ID=
25、”Literal1" runat=”server”b>/b/asp:Literal <div style=”float:right"a href=”index。aspx">首页</a>/div> <hr/ <p问题: asp:Literal ID=”question" runat="server">/asp:Literal> <br /> <p>请编辑选项:nbsp;nbsp; asp:ListView ID=”ListView1" runa
26、t=”server" DataKeyNames="aid" DataSourceID=”ObjectDataSource1" InsertItemPosition="LastItem"> AlternatingItemTemplate> span style=”>选项: <asp:Label ID=”acontentLabel" runat=”server” Text=< Eval("acontent") % /<br /> <asp:Button ID=”
27、EditButton” runat=”server” CommandName=”Edit” Text=”编辑" />asp:Button ID="DeleteButton" runat="server” CommandName="Delete" Text=”删除" /> <br / br /> /span> </AlternatingItemTemplate><asp:ButtonID=”Button1"runat=”server”onclick="Butt
28、on1_Click”Text="Button"/> asp:ButtonID=”Button2"runat=”server”onclick=”Button2_Click”Text=”Button”/ </div> </form>/body添加投票选项界面相应控件触发代码及数据库连接部分:addOption.aspx.cs: public partial class addOption : System。Web。UI。Page protected void Page_Load(object sender, EventA
29、rgs e) Button1。Text = "+添加选项”; Button2。Text = "发起投票”; String questionContent = Request。Cookies"con”.Values"qContent"; question。Text = questionContent; string qTypeStr = Request。Cookies”con”.Values”qType”; int qType = Convert。ToInt32(qTypeStr); if (qType = 1) Literal2。Text=&q
30、uot;<b单选投票</b”; else Literal2.Text = ”<b>复选投票/b”; protected void Button1_Click(object sender, EventArgs e) string aContent = option.Text; string qidStr = Request。Cookies"qid”.Value; int qid = Convert。ToInt32(qidStr); SqlConnection conn = new SqlConnection(”Data Source=.SQLEXPRESS;At
31、tachDBFilename=DataDirectoryMyVote.mdf;Integrated Security=True;User Instance=True"); conn。Open(); SqlCommand cmd = conn。CreateCommand(); cmd.CommandText = ”insert into T_answer(qid,acontent) values(qid,acontent)”; cmd.Parameters。AddWithValue("qid", qid); cmd.Parameters。AddWithValue(&
32、quot;acontent", aContent); cmd.ExecuteNonQuery(); cmd。Dispose();conn。Dispose(); Response。Redirect(”addOption。aspx"); protected void Button2_Click(object sender, EventArgs e) Response.Redirect("index.aspx”); 执行添加选项:添加选项后界面用户添加完选项可对其编辑删除,可添加多个选项,然后点发起投票回到主页面。4.4 投票界面部分viewVote.aspx投票界面代
33、码:<body><form id="form1" runat=”server" <div><asp:ObjectDataSourceID="ObjectDataSource1”runat=”server" DeleteMethod=”Delete” InsertMethod=”Insert" OldValuesParameterFormatString=”original_0" SelectMethod="GetDataByQid” TypeName="vote。DAL
34、。questionTableAdapters。T_questionTableAdapter” UpdateMethod=”Update”> <DeleteParameters asp:Parameter Name="Original_qid" Type=”Int32" / </DeleteParameters <InsertParameters <asp:Parameter Name=”qcontent" Type="String" / <asp:Parameter Name="qtype
35、" Type=”Int32" /> </InsertParameters /SelectParameters> </asp:ObjectDataSource <asp:Literal ID=”Literal1” runat=”server”</asp:Literal> div style="float:right"<a href=”index。aspx"首页</a></div <hr / 问题:<asp:Repeater ID="Repeater1"
36、; runat=”server” DataSourceID=”ObjectDataSource1”> ItemTemplateEval("qContent")>/ItemTemplate /asp:Repeater br /> InsertParameters <asp:Parameter Name="qid” Type="Int32" /<asp:Parameter Name="acontent” Type="String" /> <asp:Parameter Name=
37、”count" Type="Int32” / /InsertParameters> SelectParameters> asp:QueryStringParameter Name=”qid” QueryStringField="qid" Type="Int32" /> </SelectParameters> UpdateParameters> asp:Parameter Name=”qid” Type="Int32" / <asp:Parameter Name="
38、acontent” Type="String" / <asp:RadioButtonListID="RadioButtonList1" runat="server” DataSourceID=”ObjectDataSource2" DataTextField=”acontent" DataValueField=”aid” style="height: 26px" /asp:RadioButtonList> <asp:ButtonID="Button1"runat=&q
39、uot;server"onclick="Button1_Click" Text=”Button” /</div</form</body投票界面相应控件触发代码及数据库连接部分:viewVote.aspx。cs: public partial class viewVote : System。Web.UI。Page protected void Page_Load(object sender, EventArgs e) Literal1。Text = "b>请投票/b”; Button1。Text = ”确定”; string qTy
40、peStr=Request。QueryString”type"; int qType = Convert.ToInt32(qTypeStr); if (qType = 1) CheckBoxList1。Visible = false; else RadioButtonList1。Visible = false; protected void Button1_Click(object sender, EventArgs e) string qTypeStr = Request.QueryString"type”; int qType = Convert.ToInt32(qTy
41、peStr); int voted = 0; string qidStr = Request.QueryString"qid”; vote。DAL。ipDs。T_ipDataTable data = adapter.GetIpByQid(qid); for (int i = 0; i < data.Count; i+) vote。DAL.ipDs.T_ipRow userRow = datai; if (userRow。ip = ipStr) if (userRow。lasttime.AddHours(24) = DateTime.Now) voted = 1;/已经投过票 R
42、esponse.Write(”<scriptalert(您已经投过票了!!')</script”); return; if (voted = 0) DateTime lasttime = DateTime.Now; if (qType = 1) string selectStr = RadioButtonList1.SelectedValue; int aid = Convert。ToInt32(selectStr); SqlConnection conn = new SqlConnection("Data Source=。SQLEXPRESS;AttachDBF
43、ilename=|DataDirectoryMyVote.mdf;Integrated Security=True;User Instance=True"); conn。Open(); cmd.ExecuteNonQuery(); cmd.CommandText = ”insert into T_ip(qid,ip,lasttime) values(qid,ip,lasttime)"; cmd.Parameters。AddWithValue(”qid",qid);cmd.ExecuteNonQuery();cmd.Dispose();conn.Dispose();
44、 else SqlConnection conn = new SqlConnection("Data Source=。SQLEXPRESS;AttachDBFilename=|DataDirectory|MyVote。mdf;Integrated Security=True;User Instance=True"); conn。Open(); SqlCommand cmd = conn。CreateCommand(); for (int i = 0; i CheckBoxList1。Items.Count; i+) if (CheckBoxList1。Itemsi.Sele
45、cted = true) String selectStr= CheckBoxList1.Itemsi。Value; int aid = Convert。ToInt32(selectStr); cmd.CommandText = "update T_answer set count=count+1 where aid=aid2"; cmd.Parameters。AddWithValue(”aid2”, aid); cmd。ExecuteNonQuery(); cmd.CommandText = "insert into T_ip(qid,ip,lasttime)
46、values(qid,ip,lasttime)”; cmd。Parameters。AddWithValue("qid", qid); cmd。ExecuteNonQuery();cmd。Dispose();conn.Dispose(); Response。Redirect("viewResult.aspx?qid=”+qid); 执行投票界面:同一IP限制投一票4。5 显示结果部分viewResult。aspx显示结果界面代码:body><form id=”form1" runat="server” asp:ObjectDataSo
47、urce ID="ObjectDataSource1” runat="server” DeleteMethod="Delete" InsertMethod=”Insert” OldValuesParameterFormatString=”original_0" SelectMethod=”GetDataByQid" TypeName="vote.DAL。questionTableAdapters。T_questionTableAdapter” UpdateMethod="Update" DeletePar
48、ameters> asp:ParameterName=”Original_qid” Type=”Int32" /> /DeleteParameters<InsertParameters><asp:ParameterName=”qcontent”Type=”String" /> <asp:Parameter Name=”qtype" Type="Int32" /> /InsertParameters> SelectParameters><asp:QueryStringParamet
49、erName=”qid" QueryStringField=”qid” Type="Int32" /</SelectParameters <UpdateParameters> <asp:Parameter Name="qcontent" Type=”String” / <asp:Parameter Name=”qtype” Type=”Int32" /> <asp:Parameter Name="Original_qid” Type="Int32” /> </U
50、pdateParameters </asp:ObjectDataSource <asp:Literal ID="Literal1” runat="server"</asp:Literal> <div style=”float:right”a href=”index.aspx">首页/a>/div hr / 问题:<asp:Repeater ID="Repeater1" runat=”server" DataSourceID=”ObjectDataSource1"<
51、;ItemTemplate%#Eval("qContent”)>/ItemTemplate </asp:Repeaterasp:ObjectDataSourceID=”ObjectDataSource2”runat="server” DeleteMethod=”Delete” InsertMethod=”Insert" OldValuesParameterFormatString=”original_0” SelectMethod=”GetDataByQid"TypeName="vote.DAL.optionDsTableAdapters.T_answerTableAdapter"UpdateMethod=”Update"onselecting="ObjectDataSource2_Selecting” <DeleteParametersasp:ParameterName=”Original_aid” Type=&qu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年湖南商务职业技术学院单招职业技能考试题库完整版附答案详解
- 2024年成都职业技术学院单招综合素质考试模拟试卷及参考答案详解【完整版】
- 2026年永州新能源职业学院高职单招职业适应性测试考试模拟试卷含答案详解【新】
- 2026年山东外事职业大学高职单招职业适应性测试考试题库附答案详解
- 2026年咸阳嵯峨山技师学院高职单招职业技能考试模拟试卷及参考答案详解【完整版】
- 2027年河南省三门峡市高职单招职业技能考试模拟试卷带答案详解(夺分金卷)
- 2025年湖北生态工程职院单招综合素质考试题库及参考答案详解【B卷】
- 2026年许昌电气职业学院高职单招职业技能考试题库含答案详解【满分必刷】
- 初中三年级数学:从算术平方根到代数建模-配方法解一元二次方程的深度探究教案
- 外研版初中英语七年级上册Unit 1读写整合课教案
- 2026年铜陵安徽六国化工股份有限公司公开招聘57名员工笔试模拟试题及答案详解
- 农村改厕后续管护长效机制构建研究意义
- 母猪饲养管理中的健康管理
- 2026中国智慧交通系统建设进度与投融资模式报告
- 2026年阿里云ACP云计算工程师模拟题及答案
- 2026秋人教版九年级英语上册词汇表(全册)
- 2026年4月自考14199生产运作与管理试题
- DB23T 3999-2026 流态固化土填筑应用技术规程
- 口腔种植技术操作规范和诊疗指南(2026版)
- 2026年执业兽医资格道押题宝典模考模拟试题(满分必刷)附答案详解
- (新版!)2026年欧盟REACH法规第36批253项SVHC高度关注物质清单
评论
0/150
提交评论