您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > VS2010采用web方式发布新闻(上传到数据库)简单版
实验报告专业13网络工程班级1班学号201324132153姓名刘煜明课程名称网络应用程序开发学年2015-2016学期1/2√课程类别专业必修√限选任选实践实验时间2016年5月31日实验名称采用web方式发布新闻实验目的和要求本实验设计是在VisualStudio2010程序下,采用web方式把新闻信息发布到数据库中,以便网站引用该新闻内容。实验环境要求软件:MicrosoftVisualStudio2010SQLServer2008实验内容、方法和步骤(见附页1)1.在SQL创建数据库和数据表2.在VS2010新建一个C#语言的web应用程序项目3.设计发布新闻页面模板4.实现后台功能,发布新闻到数据库中实验结果(见附页2)程序源代码(见附页3)评定成绩:批阅教师:年月日实验报告(附页1)1.在SQL创建数据库和数据表如下图所示:2.在VS2010新建一个C#语言的web应用程序项目如下图所示,按照实验要求,新建一个Newspublish的web项目,然后新建C#的web窗体,名字是NewsPublish:3.设计页面模板如下图所示,拖动控件按如下的方式摆放:4.实现后台功能,查询数据库(1)使用ConfigurationManager和SqlConnection函数连接数据库addname=NewsMessageConnectionStringconnectionString=DataSource=REX\REX;InitialCatalog=NewsMessage;PersistSecurityInfo=True;UserID=sa;Password=a84898726providerName=System.Data.SqlClient/(2)功能一,插入新的新闻到数据库中SqlConnectioncon=Getconn();con.Open();stringstrsql=INSERTINTOnewsVALUES('+TextBox1.Text+','+TextBox2.Text+','+TextBox3.Text+','+DateTime.Now.ToString()+');SqlCommandcmd=newSqlCommand(strsql,con);if(cmd.ExecuteNonQuery()0){GridView1.DataBind();Response.Write(Scriptalert('添¬¨ª加¨®成¨¦功|')/script);TextBox1.Text=;TextBox2.Text=;TextBox3.Text=;(3)功能二,删除发布错误的新闻信息SqlConnectioncon=Getconn();con.Open();stringstrsql=DELETEFROMnewsWHEREid='+TextBox4.Text+';SqlCommandcmd=newSqlCommand(strsql,con);if(cmd.ExecuteNonQuery()0){GridView1.DataBind();Response.Write(Scriptalert('删¦?除y成¨¦功|')/script);TextBox1.Text=;TextBox2.Text=;TextBox3.Text=;TextBox4.Text=;(4)功能三,修改所发布的新闻SqlConnectioncon=Getconn();con.Open();stringstrsql=UPDATEnewsSETcolumns='+TextBox2.Text+',title='+TextBox2.Text+',contents='+TextBox3.Text+',time='+DateTime.Now.ToString()+'WHEREid='+TextBox4.Text+';SqlCommandcmd=newSqlCommand(strsql,con);if(cmd.ExecuteNonQuery()0){GridView1.DataBind();Response.Write(Scriptalert('修T改?成¨¦功|')/script);(5)功能四,显示该ID的新闻内容到Textbox中,以便于修改处理SqlConnectioncon=Getconn();con.Open();stringstrsql=select*FROMnewsWHEREid='+TextBox4.Text+';SqlCommandcmd=newSqlCommand(strsql,con);SqlDataReaderdr=cmd.ExecuteReader();dr.Read();TextBox1.Text=dr[0].ToString();TextBox2.Text=dr[1].ToString();;TextBox3.Text=dr[2].ToString();TextBox4.Text=dr[4].ToString();(详细见实验源代码)实验结果(附页2)实验结果展示:实验源代码(附页2)页面设计代码:%@PageLanguage=C#AutoEventWireup=trueCodeBehind=NewsPublish.aspx.csInherits=Newspublish.NewsPublish%!DOCTYPEhtmlPUBLIC-//W3C//DTDXHTML1.0Transitional//EN==servertitle/title/headbodyformid=form1runat=serverdiv采¨¦用®?web方¤?式º?发¤¡é布?新?闻?br/br/asp:GridViewID=GridView1runat=serverAutoGenerateColumns=FalseDataSourceID=SqlDataSource1Columnsasp:BoundFieldDataField=idHeaderText=idInsertVisible=FalseReadOnly=TrueSortExpression=id/asp:BoundFieldDataField=columnsHeaderText=columnsSortExpression=columns/asp:BoundFieldDataField=titleHeaderText=titleSortExpression=title/asp:BoundFieldDataField=contentsHeaderText=contentsSortExpression=contents/asp:BoundFieldDataField=timeHeaderText=timeSortExpression=time//Columns/asp:GridViewasp:SqlDataSourceID=SqlDataSource1runat=serverConnectionString=%$ConnectionStrings:NewsMessageConnectionString%SelectCommand=SELECT*FROM[news]/asp:SqlDataSourceasp:LabelID=Label4runat=serverText=ID:Width=100px/asp:Labelasp:TextBoxID=TextBox4runat=serverHeight=16pxWidth=254px/asp:TextBox(必À?填¬?,ê?插?入¨?数ºy据Y时º¡À不?用®?填¬?)ê?br/asp:LabelID=Label1runat=serverText=新?闻?栏¤?目?:êoWidth=100px/asp:Labelasp:TextBoxID=TextBox1runat=serverHeight=16pxWidth=254px/asp:TextBoxasp:ButtonID=Button5runat=serverText=显?示º?该?ID的Ì?内¨²容¨Yonclick=Button5_Click/br/asp:LabelID=Label2runat=serverText=新?闻?标À¨º题¬a:êoWidth=100px/asp:Labelasp:TextBoxID=TextBox2runat=serverHeight=17pxWidth=254px/asp:TextBox br/asp:LabelID=Label3runat=serverText=新?闻?内¨²容¨Y:êoWidth=100px/asp:Labelasp:TextBoxID=TextBox3runat=serverHeight=172pxWidth=254px/asp:TextBoxbr/asp:ButtonID=Button1runat=serveronclick=Button1_ClickText=插?入¨?/ asp:ButtonID=Button2runat=serveronclick=Button2_ClickText=删¦?除y/ asp:ButtonID=Button3runat=serveronclick=Button3_ClickText=修T改?//div/form/body/html后台服务端代码:namespaceNewspublish{publicpartialclassNewsPublish:System.Web.UI.Page{publicSqlConnectionGetconn(){stringconstr=ConfigurationManager.ConnectionStrings[NewsMessageConnectionString].ConnectionString;SqlConnectioncon=newSqlConnection(constr);returncon;}protectedvoidPage_Load(objectsender,EventArgse){}protectedvoidButton1_Click(objectsender,EventArgse){try{SqlConnectioncon=Getconn();con.Open();stringstrsql=INSERTINTOnewsVALUES('+TextBox1.Text+','+TextBox2.Text+','+TextBox3.Text+','+DateTime.Now.ToString()+');SqlCommandcmd=newSqlCommand(strsql,con);if(cmd.ExecuteNonQuery()0){GridView1.DataBind();Response.Write(Scriptalert('添¬¨ª加¨®成¨¦功|')/script);TextBox1.Text=;TextBox2.Text=;TextBox3.Text=;}else{Response.Write(Scriptalert('添¬¨ª加¨®失º¡ì败㨹')/script);}con.Close();}catch(Exceptiona){Response.Write(a.Message.ToString());}}protectedvoidButton2_Click(objectsen
本文标题:VS2010采用web方式发布新闻(上传到数据库)简单版
链接地址:https://www.777doc.com/doc-2866773 .html