VS2010 采用web方式发布新闻(上传到数据库)简单版_第1页
VS2010 采用web方式发布新闻(上传到数据库)简单版_第2页
VS2010 采用web方式发布新闻(上传到数据库)简单版_第3页
VS2010 采用web方式发布新闻(上传到数据库)简单版_第4页
VS2010 采用web方式发布新闻(上传到数据库)简单版_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实 验 报 告专业13网络工程 班级 1班 学号201324132153 姓名 刘煜明 课程名称 网络应用程序开发 学年2015 -2016 学期1 / 2 课程类别专业必修 限选 任选 实践 实验时间2016年5 月31 日实验名称 采用web方式发布新闻实验目的和要求 本实验设计是在Visual Studio 2010程序下,采用web方式把新闻信息发布到数据库中,以便网站引用该新闻内容。实验环境要求软件:Microsoft Visual Studio 2010 SQL Server 2008实验内容、方法和步骤(见附页1)1. 在SQL创建数据库和数据表2. 在VS2010新建一个C#语

2、言的web应用程序项目3. 设计发布新闻页面模板4. 实现后台功能,发布新闻到数据库中实验结果(见附页2)程序源代码 (见附页3)评定成绩: 批阅教师: 年 月 日实 验 报 告(附页1)1. 在SQL创建数据库和数据表如下图所示:2. 在VS2010新建一个C#语言的web应用程序项目如下图所示,按照实验要求,新建一个Newspublish的web项目,然后新建C#的web窗体,名字是NewsPublish:3. 设计页面模板如下图所示,拖动控件按如下的方式摆放:4. 实现后台功能,查询数据库(1) 使用ConfigurationManager和SqlConnection函数连接数据库<

3、;add name="NewsMessageConnectionString" connectionString="Data Source=REXREX;Initial Catalog=NewsMessage;Persist Security Info=True;User ID=sa;Password=a84898726" providerName="System.Data.SqlClient" />(2) 功能一,插入新的新闻到数据库中SqlConnection con = Getconn(); con.Open(); str

4、ing strsql = "INSERT INTO news VALUES('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','"+DateTime.Now.ToString()+"')" SqlCommand cmd = new SqlCommand(strsql, con); if (cmd.ExecuteNonQuery() >

5、 0) GridView1.DataBind(); Response.Write("<Script>alert('添¬¨ª加¨®成¨¦功|')</script>"); TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = ""(3) 功能二,删除发布错误的新闻信息SqlConnection con = Getconn(); con.Open(); str

6、ing strsql = "DELETE FROM news WHERE id='" + TextBox4.Text + "'" SqlCommand cmd = new SqlCommand(strsql, con); if (cmd.ExecuteNonQuery() > 0) GridView1.DataBind(); Response.Write("<Script>alert('删¦?除y成¨¦功|')</script>"); Text

7、Box1.Text = "" TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = ""(4) 功能三,修改所发布的新闻SqlConnection con = Getconn(); con.Open(); string strsql = "UPDATE news SET columns='" + TextBox2.Text + "',title='" + TextBox2.Text + "

8、',contents='" + TextBox3.Text + "',time='" + DateTime.Now.ToString() + "' WHERE id='" + TextBox4.Text + "'" SqlCommand cmd = new SqlCommand(strsql, con); if (cmd.ExecuteNonQuery() > 0) GridView1.DataBind(); Response.Write("<Sc

9、ript>alert('修T改?成¨¦功|')</script>");(5) 功能四,显示该ID的新闻内容到Textbox中,以便于修改处理SqlConnection con = Getconn(); con.Open(); string strsql = "select * FROM news WHERE id='" + TextBox4.Text + "'" SqlCommand cmd = new SqlCommand(strsql, con); SqlDataRead

10、er dr=cmd.ExecuteReader(); dr.Read(); TextBox1.Text = dr0.ToString(); TextBox2.Text = dr1.ToString(); ; TextBox3.Text = dr2.ToString(); TextBox4.Text = dr4.ToString();(详细见实验源代码)实 验 结 果(附页2)实验结果展示:实 验 源 代 码(附页2)页面设计代码:<% Page Language="C#" AutoEventWireup="true" CodeBehind=&quo

11、t;NewsPublish.aspx.cs" Inherits="Newspublish.NewsPublish" %><!DOCTYPE html PUBLIC "-/W3C/DTD XHTML 1.0 Transitional/EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/1999/xhtml"><head runat="

12、server"> <title></title></head><body> <form id="form1" runat="server"> <div > 采¨¦用®?web方¤?式º?发¤¡é布?新?闻?<br /> <br /> <asp:GridView ID="GridView1" runat="server"

13、AutoGenerateColumns="False" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" /> <asp:BoundField DataField="col

14、umns" HeaderText="columns" SortExpression="columns" /> <asp:BoundField DataField="title" HeaderText="title" SortExpression="title" /> <asp:BoundField DataField="contents" HeaderText="contents" SortExpression="

15、;contents" /> <asp:BoundField DataField="time" HeaderText="time" SortExpression="time" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Ne

16、wsMessageConnectionString %>" SelectCommand="SELECT * FROM news"></asp:SqlDataSource> <asp:Label ID="Label4" runat="server" Text="ID:" Width="100px"></asp:Label> <asp:TextBox ID="TextBox4" runat="server&

17、quot; Height="16px" Width="254px"></asp:TextBox> (必À?填¬?,ê?插?入¨?数ºy据Y时º¡À不?用®?填¬?)ê?<br /> <asp:Label ID="Label1" runat="server" Text="新?闻?栏¤?目?:êo" Width="100px&

18、quot;></asp:Label> <asp:TextBox ID="TextBox1" runat="server" Height="16px" Width="254px"></asp:TextBox> <asp:Button ID="Button5" runat="server" Text="显?示º?该?ID的Ì?内¨²容¨Y" onclick=&qu

19、ot;Button5_Click" /> <br /> <asp:Label ID="Label2" runat="server" Text="新?闻?标À¨º题¬a:êo" Width="100px"></asp:Label> <asp:TextBox ID="TextBox2" runat="server" Height="17px" Width

20、="254px"></asp:TextBox>&nbsp;<br /> <asp:Label ID="Label3" runat="server" Text="新?闻?内¨²容¨Y:êo" Width="100px"></asp:Label> <asp:TextBox ID="TextBox3" runat="server" Height="

21、;172px" Width="254px"></asp:TextBox> <br /> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="插?入¨?" />&nbsp; <asp:Button ID="Button2" runat="server" onclick="Button2_

22、Click" Text="删¦?除y" />&nbsp; <asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="修T改?" /> </div> </form></body></html>后台服务端代码:namespace Newspublish public partial class NewsPublish : Sy

23、stem.Web.UI.Page public SqlConnection Getconn() string constr = ConfigurationManager.ConnectionStrings"NewsMessageConnectionString".ConnectionString; SqlConnection con = new SqlConnection(constr); return con; protected void Page_Load(object sender, EventArgs e) protected void Button1_Click

24、(object sender, EventArgs e) try SqlConnection con = Getconn(); con.Open(); string strsql = "INSERT INTO news VALUES('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','"+DateTime.Now.ToString()+"')&quo

25、t; SqlCommand cmd = new SqlCommand(strsql, con); if (cmd.ExecuteNonQuery() > 0) GridView1.DataBind(); Response.Write("<Script>alert('添¬¨ª加¨®成¨¦功|')</script>"); TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text

26、= "" else Response.Write("<Script>alert('添¬¨ª加¨®失º¡ì败㨹')</script>"); con.Close(); catch (Exception a) Response.Write(a.Message.ToString(); protected void Button2_Click(object sender, EventArgs e) SqlConn

27、ection con = Getconn(); con.Open(); string strsql = "DELETE FROM news WHERE id='" + TextBox4.Text + "'" SqlCommand cmd = new SqlCommand(strsql, con); if (cmd.ExecuteNonQuery() > 0) GridView1.DataBind(); Response.Write("<Script>alert('删¦?除y成¨

28、66;功|')</script>"); TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" else Response.Write("<Script>alert('删¦?除y失º¡ì败㨹')</script>"); con.Close(); protect

29、ed void Button3_Click(object sender, EventArgs e) SqlConnection con = Getconn(); con.Open(); string strsql = "UPDATE news SET columns='" + TextBox2.Text + "',title='" + TextBox2.Text + "',contents='" + TextBox3.Text + "',time='" + DateTime.Now.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论