C # version of the website news release system

Source: Internet
Author: User
Tags datetime table name tostring
When writing this article, first of all, I would like to express my thanks to Bben_h and Jdxx, is Bben_h put forward in C # character replacement This problem, jdxx very good solution to this problem, but also remind me of some of the previous procedures (simple website news release system), now give it to everyone, I'd like to learn a little help with the ado.net operation of SQL Server database and DataGrid control components.

Database structure

Database name: mydb, data table name: News
News table Structure
ID (int 4) Increment ID
Biaoti (nvarchar 50) news Headlines
Zhaizi (nvarchar 50) is derived from
Neirong (ntext 16) news content
Shijian (datatime 8) Release time
img (nvarchar 50) image path
Counter (int 4) Hits

program to connect the SQL Server database, where Netfiresoft is the name of my machine.

Website news Release System source program: (Database with SQL Server)

Add page (addnews.aspx)

〈% @Page language= "C #" debug= "True"%〉
〈% @Import namespace= "System.Data"%〉
〈% @Import namespace= "System.Data.SqlClient"%〉
〈html〉
〈head〉
〈title〉 website News Add page 〈/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)
{
After you click the Submit button, execute the following code
SqlConnection myconnection;
SqlCommand mycommand;
String connstr;
DateTime now= DateTime.Now;
Linked SQL Server database
MyConnection =new SqlConnection ("Initial catalog=mydb;data source=netfiresoft;integrated;");
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= "title or content cannot be empty! ";
}
else if (Biaoti. TEXT.LENGTH〉=100)
{
Label1.text= "Your headline is too long!" ";
}
else if (img. TEXT.LENGTH〉=100)
{
label1.text= "Your picture path is too long!" ";
}
Else
{
Insert a new record into the database
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= "Increase success!" ";
}
Biaoti. Text= "";
Zhaizi. Text= "";
Neirong. Text= "";
Img. Text= "";
}
void Reset_click (Object Sender,eventargs e)
{
After you click the Cancel button, execute the following code
Biaoti. Text= "";
Zhaizi. Text= "";
Neirong. Text= "";
Img. Text= "";
}
〈/script〉
〈/head〉
〈body〉
〈table width= "border=" 1 "cellspacing=" 0 "cellpadding=" 0 "align=" center "
bordercolorlight= "#666666" bordercolordark= "#FFFFFF"
〈form runat=server〉
〈tr bgcolor= "#CCCCCC"
〈TD colspan= "2" Website news information Add page 〈/td〉
〈/tr〉
〈tr〉
〈TD colspan= "2" 〉〈/td〉
〈/tr〉
〈tr〉
〈TD width= "78" title: 〈/td〉
〈TD width= "416" 〉〈asp:textbox id= "Biaoti" size= "a" runat= "server"/〉
〈/td〉
〈/tr〉
〈tr〉
〈TD width= "78" is excerpted from: 〈/td〉
〈TD width= "416" 〉〈asp:textbox id= "Zhaizi" size= "" runat= "Server"/〉〈/td〉
〈/tr〉
〈tr〉
〈TD width= "78" Picture: 〈/td〉
〈TD width= "416" 〉〈asp:textbox id= "img" size= "" runat= "Server"/〉〈/td〉
〈/tr〉
〈tr〉
〈TD width= "78" content: 〈/td〉
〈TD width= "416" 〉〈asp:textbox id= "Neirong" columns= "a" rows= "" 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= "submitted" 〉〈/asp:button〉
〈asp:button id= "reset" Onclick=reset_click runat= "Server" text= "Cancel" 〉〈/asp:button〉
〈/div〉
〈/td〉
〈/tr〉
〈/form〉
〈/table〉
〈p align= "center" 〉〈asp:label id= "Label1" runat= "Server"/〉
〈/body〉
〈/html〉

Display News title page (newsshow.aspx): (Use the DataGrid control Element)

〈% @Page language= "C #" debug= "True"%〉
〈% @Import namespace= "System.Data"%〉
〈% @Import namespace= "System.Data.SqlClient"%〉
〈html〉
〈head〉
〈title〉 website news Release System 〈/title〉
〈script language= "C #" runat= "Server"
Execute the following program when you just load the page.
int startIndex;
void Page_Load (Object Src,eventargs E)
{
if (! IsPostBack)
{
StartIndex = 0;
}
Binding DataGrid
Binding ();
}
Linked databases, binding datagrid
void Binding ()
{
SqlConnection myconnection;
String connstr;
MyConnection =new SqlConnection ("Initial catalog=mydb;data source=netfiresoft;integrated;");
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);
Fill DataSet
Mycommand.fill (myDataSet, "News");
Close link
DataGrid1.DataSource = mydataset.tables["News". DefaultView;
Binding 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" News Browse 〈/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= "title"
datanavigateurlfield= "id"
datanavigateurlformatstring= "Show.aspx?id={0}"
Datatextfield= "Biaoti"
target= "_new"
/〉
〈asp:boundcolumn headertext= "excerpted from" itemstyle-width=20% datafield=zhaizi/〉
〈asp:boundcolumn headertext= "click Times" itemstyle-width=10% datafield=counter/〉
〈asp:boundcolumn headertext= "Publication date" itemstyle-width=20% datafield=shijian/〉
〈/columns〉
〈/asp:datagrid〉
〈/form〉
〈/body〉
〈/html〉

News Content Browsing page (show.aspx):

〈% @Page language= "C #" debug= "True"%〉
〈% @Import namespace= "System.Data"%〉
〈% @Import namespace= "System.Data.SqlClient"%〉
〈html〉
〈head〉
〈title〉 News Release System 〈/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: "XXFarEastFont-Arial"; font-size:9pt}
. P2 {}
. p3 {font-family: "XXFarEastFont-Arial"; 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;");
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= "border=" 0 "cellspacing=" 0 "cellpadding=" 0 "
〈tr〉
〈td〉〈div align= "Center" press release system 〈/div〉〈/td〉
〈/tr〉
〈tr〉〈td〉〈/td〉
〈/tr〉
〈/table〉
〈table width= "border=" 1 "cellspacing=" 0 "cellpadding=" 0 "bordercolordark=" #FFFFFF "bordercolorlight=" #CCCCCC "
〈tr〉
〈TD background= "Mmto.gif" height= "width=" "470"
〈table width= "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= "height=" "14"
〈a href= "Javascript:window.print ()" Print this page 〈/a〉
〈/div〉
〈/td〉
〈/tr〉
〈/table〉
〈/td〉
〈/tr〉
〈tr〉
〈TD width= "470"
〈table width= "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= ' height= ' 200 '");
}
%〉〈br〉〈%=dr["Neirong"]%〉
〈/td〉
〈/tr〉
〈tr〉
〈td〉〈br〉
〈br〉 excerpt from: 〈%=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" This message was browsed
〈font color= "#FF3333" 〉[〈%=dr["Counter"]%〉]〈/font〉 back
〈/td〉
〈TD width= "172" background= "Mmto.gif"
〈/td〉
〈TD width= "134" background= "Mmto.gif"
〈p align= "right" 〉〈a href= "Javascript:window.close ()" Closes this window 〈/a〉〈/p〉
〈/td〉
〈/tr〉
〈/table〉
〈/td〉
〈/tr〉
〈/table〉
〈table width= "border=" 0 "cellspacing=" 0 "cellpadding=" 0 "
〈tr〉
〈td〉〈img src= "Http://www.163design.net/n/c/bian.gif" width= "13" height=
〈/tr〉
〈/table〉
〈/div〉
〈/body〉
〈/html〉



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.