數據庫結構
  數據庫名: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〉 
  
  顯示新聞標題頁面(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〉 
  
  新聞內容瀏覽頁面(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="http://www.163design.net/n/c/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="http://www.163design.net/n/c/bian.gif" width="500" height="13"〉〈/td〉
 〈/tr〉
〈/table〉
〈/div〉
〈/body〉
〈/html〉
新聞熱點
疑難解答