数据库结构
数据库名:mydb,数据表名:news
news表结构
id(int 4) 递增ID
biaoti(nvarchar 50) 新闻标题
zhaizi(nvarchar 50) 出自
neirong(ntext 16) 新闻内容
shijian(datatime 8)发布时间
img(nvarchar 50)图片路径
counter(int 4)点击次数
程序中连接SqlServer数据库,其中netfiresoft是我的机子的名称。
网站新闻发布系统源程序:(数据库用的是SqlServer)
增加页面(addnews.aspx)
<%@Page language="C#" Debug="True"%>
<%@Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.SqlClient"%>
<html>
<head>
<title>网站新闻信息添加页面</title>
<style type="text/css">
<!--
table { font-size: 9pt}
body { font-size: 9pt}
-->
</style>
<Script Language="C#" runat="server">
void submit_Click(Object sender,EventArgs e)
{
//当单击提交按钮之后执行下面的代码
SqlConnection MyConnection;
SqlCommand MyCommand;
String ConnStr;
DateTime now= DateTime.Now;
//链接SQL Server数据库
MyConnection =new SqlConnection("Initial Catalog=mydb;Data Source=netfiresoft;Integrated Security=SSPI;");
MyConnection.Open();
String neirong2;
neirong2=Server.HtmlEncode(neirong.Text);
String zh="";
String tmpstr="";
for (int i=0; i<neirong2.Length;i++) {
zh=neirong2.Substring(i,1);
if (zh==" ") { zh=" ";}
if (zh=="\n") { zh="<br>"; }
if (zh=="\t") { zh=" ";}
tmpstr=tmpstr+zh; }
neirong2=tmpstr;
if ((biaoti.Text=="")||(neirong2==""))
{
Label1.Text="标题或内容不能为空!";
}
else if (biaoti.Text.Length>=100)
{
Label1.Text="你的标题太长了!";
}
else if (img.Text.Length>=100)
{
Label1.Text="你的图片路径太长了!";
}
else
{
//将新记录插入到数据库中
ConnStr="insert into news(biaoti,zhaizi,neirong,shijian,img,counter) VALUES(‘"+biaoti.Text+"‘,
‘"+zhaizi.Text+"‘, ‘"+neirong2+"‘, ‘"+now.ToString()+"‘, ‘"+img.Text+"‘,0)";
MyCommand=new SqlCommand(ConnStr,MyConnection);
MyCommand.ExecuteNonQuery();
Label1.Text="增加成功!";
}
biaoti.Text="";
zhaizi.Text="";
neirong.Text="";
img.Text="";
}
void reset_Click(Object sender,EventArgs e)
{
//当单击取消按钮之后执行下面代码
biaoti.Text="";
zhaizi.Text="";
neirong.Text="";
img.Text="";
}
</Script>
</head>
<body >
<table width="550" border="1" cellspacing="0" cellpadding="0" align="center"
bordercolorlight="#666666" bordercolordark="#FFFFFF">
<form runat=server>
<tr bgcolor="#CCCCCC">
<td colspan="2">网站新闻信息添加页面</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td width="78">标题:</td>
<td width="416"> <asp:TextBox id="biaoti" size="50" runat="server"/>
</td>
</tr>
<tr>
<td width="78">摘自:</td>
<td width="416"> <asp:TextBox id="zhaizi" size="30" runat="server"/> </td>
</tr>
<tr>
<td width="78">图片:</td>
<td width="416"><asp:TextBox id="img" size="30" runat="server"/></td>
</tr>
<tr>
<td width="78">内容:</td>
<td width="416"> <asp:TextBox id="neirong" Columns="60" Rows="15" TextMode="MultiLine" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2">
<div align="center">
<asp:Button id="submit" onClick=submit_Click runat="server" Text="提交"></asp:Button>
<asp:Button id="reset" onClick=reset_Click runat="server" Text="取消"></asp:Button>
</div>
</td>
</tr>
</form>
</table>
<p align="center"><asp:Label id="Label1" runat="server"/>
</body>
</html>
<%@Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.SqlClient"%>
<html>
<head>
<title>网站新闻信息添加页面</title>
<style type="text/css">
<!--
table { font-size: 9pt}
body { font-size: 9pt}
-->
</style>
<Script Language="C#" runat="server">
void submit_Click(Object sender,EventArgs e)
{
//当单击提交按钮之后执行下面的代码
SqlConnection MyConnection;
SqlCommand MyCommand;
String ConnStr;
DateTime now= DateTime.Now;
//链接SQL Server数据库
MyConnection =new SqlConnection("Initial Catalog=mydb;Data Source=netfiresoft;Integrated Security=SSPI;");
MyConnection.Open();
String neirong2;
neirong2=Server.HtmlEncode(neirong.Text);
String zh="";
String tmpstr="";
for (int i=0; i<neirong2.Length;i++) {
zh=neirong2.Substring(i,1);
if (zh==" ") { zh=" ";}
if (zh=="\n") { zh="<br>"; }
if (zh=="\t") { zh=" ";}
tmpstr=tmpstr+zh; }
neirong2=tmpstr;
if ((biaoti.Text=="")||(neirong2==""))
{
Label1.Text="标题或内容不能为空!";
}
else if (biaoti.Text.Length>=100)
{
Label1.Text="你的标题太长了!";
}
else if (img.Text.Length>=100)
{
Label1.Text="你的图片路径太长了!";
}
else
{
//将新记录插入到数据库中
ConnStr="insert into news(biaoti,zhaizi,neirong,shijian,img,counter) VALUES(‘"+biaoti.Text+"‘,
‘"+zhaizi.Text+"‘, ‘"+neirong2+"‘, ‘"+now.ToString()+"‘, ‘"+img.Text+"‘,0)";
MyCommand=new SqlCommand(ConnStr,MyConnection);
MyCommand.ExecuteNonQuery();
Label1.Text="增加成功!";
}
biaoti.Text="";
zhaizi.Text="";
neirong.Text="";
img.Text="";
}
void reset_Click(Object sender,EventArgs e)
{
//当单击取消按钮之后执行下面代码
biaoti.Text="";
zhaizi.Text="";
neirong.Text="";
img.Text="";
}
</Script>
</head>
<body >
<table width="550" border="1" cellspacing="0" cellpadding="0" align="center"
bordercolorlight="#666666" bordercolordark="#FFFFFF">
<form runat=server>
<tr bgcolor="#CCCCCC">
<td colspan="2">网站新闻信息添加页面</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td width="78">标题:</td>
<td width="416"> <asp:TextBox id="biaoti" size="50" runat="server"/>
</td>
</tr>
<tr>
<td width="78">摘自:</td>
<td width="416"> <asp:TextBox id="zhaizi" size="30" runat="server"/> </td>
</tr>
<tr>
<td width="78">图片:</td>
<td width="416"><asp:TextBox id="img" size="30" runat="server"/></td>
</tr>
<tr>
<td width="78">内容:</td>
<td width="416"> <asp:TextBox id="neirong" Columns="60" Rows="15" TextMode="MultiLine" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2">
<div align="center">
<asp:Button id="submit" onClick=submit_Click runat="server" Text="提交"></asp:Button>
<asp:Button id="reset" onClick=reset_Click runat="server" Text="取消"></asp:Button>
</div>
</td>
</tr>
</form>
</table>
<p align="center"><asp:Label id="Label1" runat="server"/>
</body>
</html>
显示新闻标题页面(newsshow.aspx):(用到DataGrid控制元件)
<%@Page language="C#" Debug="True"%>
<%@Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.SqlClient"%>
<Html>
<Head>
<Title>网站新闻发布系统</Title>
<script language="C#" runat="server" >
//当刚载入页面时候执行下面的程序。
int startIndex;
void Page_Load(Object Src,EventArgs E)
{
if (!IsPostBack)
{
startIndex =0;
}
//绑定DataGrid
Binding();
}
//链接数据库,绑定DataGrid
void Binding()
{
SqlConnection MyConnection;
String ConnStr;
MyConnection =new SqlConnection("Initial Catalog=mydb;Data Source=netfiresoft;Integrated Security=SSPI;");
MyConnection.Open();
String strCom= "SELECT id ,biaoti ,zhaizi ,counter ,shijian FROM news ORDER BY id DESC";
DataSet myDataSet= new DataSet();
SqlDataAdapter myCommand=new SqlDataAdapter(strCom,MyConnection);
//填充DataSet
myCommand.Fill(myDataSet,"news");
//关闭链接
DataGrid1.DataSource = myDataSet.Tables["news"].DefaultView;
//绑定 DataGrid
DataGrid1.DataBind();
}
void ChangePage(Object sender,DataGridPageChangedEventArgs e)
{
startIndex = e.NewPageIndex*DataGrid1.PageSize;
DataGrid1.CurrentPageIndex = e.NewPageIndex;
Binding();
}
</script>
</Head>
<Body runat=server>
<p align="Center"><font size="+3">新闻浏览</font>
<form runat=server>
<asp:DataGrid id=DataGrid1
runat="server"
ForeColor="Black"
PagerStyle-Mode="NumericPages"
Headerstyle-BackColor="#AAAADD"
AlternatingItemStyle-BackColor="#FFFFCD"
OnPageindexChanged="ChangePage"
PageSize="10"
AllowPaging="True"
Width="80%"
Font-Name="Verdana"
Font-Size="8pt"
autogeneratecolumns="False">
<Columns>
<asp:HyperLinkColumn
HeaderText="标题"
DataNavigateUrlField="id"
DataNavigateUrlFormatString="show.aspx?id={0}"
DataTextField="biaoti"
Target="_new"
/>
<asp:BoundColumn HeaderText="摘自" itemstyle-width=20% DataField=zhaizi/>
<asp:BoundColumn HeaderText="点击次数" itemstyle-width=10% DataField=counter/>
<asp:BoundColumn HeaderText="发表日期" itemstyle-width=20% DataField=shijian/>
</Columns>
</asp:DataGrid>
</form>
</Body>
</Html>
<%@Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.SqlClient"%>
<Html>
<Head>
<Title>网站新闻发布系统</Title>
<script language="C#" runat="server" >
//当刚载入页面时候执行下面的程序。
int startIndex;
void Page_Load(Object Src,EventArgs E)
{
if (!IsPostBack)
{
startIndex =0;
}
//绑定DataGrid
Binding();
}
//链接数据库,绑定DataGrid
void Binding()
{
SqlConnection MyConnection;
String ConnStr;
MyConnection =new SqlConnection("Initial Catalog=mydb;Data Source=netfiresoft;Integrated Security=SSPI;");
MyConnection.Open();
String strCom= "SELECT id ,biaoti ,zhaizi ,counter ,shijian FROM news ORDER BY id DESC";
DataSet myDataSet= new DataSet();
SqlDataAdapter myCommand=new SqlDataAdapter(strCom,MyConnection);
//填充DataSet
myCommand.Fill(myDataSet,"news");
//关闭链接
DataGrid1.DataSource = myDataSet.Tables["news"].DefaultView;
//绑定 DataGrid
DataGrid1.DataBind();
}
void ChangePage(Object sender,DataGridPageChangedEventArgs e)
{
startIndex = e.NewPageIndex*DataGrid1.PageSize;
DataGrid1.CurrentPageIndex = e.NewPageIndex;
Binding();
}
</script>
</Head>
<Body runat=server>
<p align="Center"><font size="+3">新闻浏览</font>
<form runat=server>
<asp:DataGrid id=DataGrid1
runat="server"
ForeColor="Black"
PagerStyle-Mode="NumericPages"
Headerstyle-BackColor="#AAAADD"
AlternatingItemStyle-BackColor="#FFFFCD"
OnPageindexChanged="ChangePage"
PageSize="10"
AllowPaging="True"
Width="80%"
Font-Name="Verdana"
Font-Size="8pt"
autogeneratecolumns="False">
<Columns>
<asp:HyperLinkColumn
HeaderText="标题"
DataNavigateUrlField="id"
DataNavigateUrlFormatString="show.aspx?id={0}"
DataTextField="biaoti"
Target="_new"
/>
<asp:BoundColumn HeaderText="摘自" itemstyle-width=20% DataField=zhaizi/>
<asp:BoundColumn HeaderText="点击次数" itemstyle-width=10% DataField=counter/>
<asp:BoundColumn HeaderText="发表日期" itemstyle-width=20% DataField=shijian/>
</Columns>
</asp:DataGrid>
</form>
</Body>
</Html>
容浏览页面(show.aspx):
<%@Page language="C#" Debug="True"%>
<%@Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.SqlClient"%>
<html>
<head>
<title>新闻发布系统</title>
<style type="text/css">
<!--
TABLE { FONT-SIZE: 9pt }
INPUT { FONT-SIZE: 9pt }
SELECT{ FONT-SIZE: 9pt }
BODY { FONT-SIZE: 9pt }
a:link { color: #000099; text-decoration: none}
a:visited { color:#000099; text-decoration: none}
a:hover { color: #990000; text-decoration: underline}
td {font-size:9pt;line-height:13pt;}
.p1 { font-family: "宋体"; font-size: 9pt}
.p2 { }
.p3 { font-family: "宋体"; font-size: 12pt}
-->
</style>
<script Language="C#" runat="server">
DataSet ds;
DataRow dr;
String newsid;
void Page_Load(Object sender,EventArgs e)
{
SqlConnection MyConnection;
String ConnStr,strCon;
newsid = Request.Params["id"];
MyConnection =new SqlConnection("Initial Catalog=mydb;Data Source=netfiresoft;Integrated Security=SSPI;");
MyConnection.Open();
String strCom= "SELECT biaoti ,zhaizi ,neirong,img,counter ,shijian FROM news where id="+newsid;
ds= new DataSet();
SqlDataAdapter myCommand=new SqlDataAdapter(strCom,MyConnection);
myCommand.Fill(ds,"news");
dr = ds.Tables["news"].Rows[0];
strCon ="SELECT counter FROM news WHERE id = "+newsid;
SqlCommand myCommand2= new SqlCommand(strCon, MyConnection);
SqlDataReader reader = myCommand2.ExecuteReader();
reader.Read();
int i = reader.GetInt32(0);
i++;
reader.Close();
strCon ="UPDATE news SET counter = "+i.ToString()+" WHERE (id= "+newsid+")";
myCommand2.CommandText = strCon;
myCommand2.ExecuteNonQuery();
MyConnection.Close();
}
</script>
</head>
<body bgcolor="#FFFFFF" link="#000000">
<div align="center" class="p2">
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><div align="center">新闻发布系统</div></td>
</tr>
<tr><td> </td>
</tr>
</table>
<table width="500" border="1" cellspacing="0" cellpadding="0" bordercolordark="#FFFFFF" bordercolorlight="#CCCCCC">
<tr>
<td background="mmto.gif" height="15" width="470">
<table width="500" border="0" cellspacing="0" cellpadding="0" background="mmto.gif">
<tr>
<td background="mmto.gif"> </td>
<td background="mmto.gif">
<div align="right">
<img src="printer.gif" width="16" height="14">
<a href="javascript:window.print()">打印本页</a>
</div>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="470">
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr align="center">
<td class="p3"><br> <%=dr["biaoti"]%></td>
</tr>
<tr align="center">
<td> <div> <hr size="1" width="300">
<font color="#999999">[<%=dr["shijian"]%>]</font><br>
</div>
</td>
</tr>
<tr>
<td>
<%
if(dr["img"] != "") {
Response.Write("<img src=‘" + dr["img"] + "‘ border=0 align=‘left‘ width=‘20‘ height=‘200‘>");
}
%><br> <%=dr["neirong"]%>
</td>
</tr>
<tr>
<td> <br>
<br> 摘自: <%=dr["zhaizi"]%>
<br>
</td>
</tr>
<tr>
<td>
<div align="right"></div>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="17">
<table width="498" border="0" cellspacing="0" cellpadding="0" mmto.gif">
<tr>
<td width="172" background="mmto.gif">本条消息被浏览了
<font color="#FF3333">[<%=dr["Counter"]%>]</font>回
</td>
<td width="172" background="mmto.gif">
</td>
<td width="134" background="mmto.gif">
<p align="right"><a href="javascript:window.close()">关闭本窗口 </a></p>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src="bian.gif" width="500" height="13"></td>
</tr>
</table>
</div>
</body>
</html>
<%@Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.SqlClient"%>
<html>
<head>
<title>新闻发布系统</title>
<style type="text/css">
<!--
TABLE { FONT-SIZE: 9pt }
INPUT { FONT-SIZE: 9pt }
SELECT{ FONT-SIZE: 9pt }
BODY { FONT-SIZE: 9pt }
a:link { color: #000099; text-decoration: none}
a:visited { color:#000099; text-decoration: none}
a:hover { color: #990000; text-decoration: underline}
td {font-size:9pt;line-height:13pt;}
.p1 { font-family: "宋体"; font-size: 9pt}
.p2 { }
.p3 { font-family: "宋体"; font-size: 12pt}
-->
</style>
<script Language="C#" runat="server">
DataSet ds;
DataRow dr;
String newsid;
void Page_Load(Object sender,EventArgs e)
{
SqlConnection MyConnection;
String ConnStr,strCon;
newsid = Request.Params["id"];
MyConnection =new SqlConnection("Initial Catalog=mydb;Data Source=netfiresoft;Integrated Security=SSPI;");
MyConnection.Open();
String strCom= "SELECT biaoti ,zhaizi ,neirong,img,counter ,shijian FROM news where id="+newsid;
ds= new DataSet();
SqlDataAdapter myCommand=new SqlDataAdapter(strCom,MyConnection);
myCommand.Fill(ds,"news");
dr = ds.Tables["news"].Rows[0];
strCon ="SELECT counter FROM news WHERE id = "+newsid;
SqlCommand myCommand2= new SqlCommand(strCon, MyConnection);
SqlDataReader reader = myCommand2.ExecuteReader();
reader.Read();
int i = reader.GetInt32(0);
i++;
reader.Close();
strCon ="UPDATE news SET counter = "+i.ToString()+" WHERE (id= "+newsid+")";
myCommand2.CommandText = strCon;
myCommand2.ExecuteNonQuery();
MyConnection.Close();
}
</script>
</head>
<body bgcolor="#FFFFFF" link="#000000">
<div align="center" class="p2">
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><div align="center">新闻发布系统</div></td>
</tr>
<tr><td> </td>
</tr>
</table>
<table width="500" border="1" cellspacing="0" cellpadding="0" bordercolordark="#FFFFFF" bordercolorlight="#CCCCCC">
<tr>
<td background="mmto.gif" height="15" width="470">
<table width="500" border="0" cellspacing="0" cellpadding="0" background="mmto.gif">
<tr>
<td background="mmto.gif"> </td>
<td background="mmto.gif">
<div align="right">
<img src="printer.gif" width="16" height="14">
<a href="javascript:window.print()">打印本页</a>
</div>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="470">
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr align="center">
<td class="p3"><br> <%=dr["biaoti"]%></td>
</tr>
<tr align="center">
<td> <div> <hr size="1" width="300">
<font color="#999999">[<%=dr["shijian"]%>]</font><br>
</div>
</td>
</tr>
<tr>
<td>
<%
if(dr["img"] != "") {
Response.Write("<img src=‘" + dr["img"] + "‘ border=0 align=‘left‘ width=‘20‘ height=‘200‘>");
}
%><br> <%=dr["neirong"]%>
</td>
</tr>
<tr>
<td> <br>
<br> 摘自: <%=dr["zhaizi"]%>
<br>
</td>
</tr>
<tr>
<td>
<div align="right"></div>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="17">
<table width="498" border="0" cellspacing="0" cellpadding="0" mmto.gif">
<tr>
<td width="172" background="mmto.gif">本条消息被浏览了
<font color="#FF3333">[<%=dr["Counter"]%>]</font>回
</td>
<td width="172" background="mmto.gif">
</td>
<td width="134" background="mmto.gif">
<p align="right"><a href="javascript:window.close()">关闭本窗口 </a></p>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src="bian.gif" width="500" height="13"></td>
</tr>
</table>
</div>
</body>
</html>