Basic petapoco. CS usage

Source: Internet
Author: User

1. First define a newsinfoss class, which encapsulates all fields in my database table

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using petapoco;
/// <Summary>
/// Summary of newsinfoss
/// </Summary>
Public class newsinfoss
{
Public newsinfoss ()
{
//
// Todo: add the constructor logic here
//

}

Public int newsid {Get; set ;}

Public String newstitle {Get; set ;}

Public String newscontent {Get; set ;}

Public datetime newsdate {Get; set ;}


}

 

2. encapsulate a sqlhelper

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;

/// <Summary>
/// Summary of newssql
/// </Summary>
Public class newssql
{
Public newssql ()
{
//
// Todo: add the constructor logic here
//
}

Petapoco. Database DB = new petapoco. Database ("connstr ");

/// <Summary>
/// Query all the data in the table
/// </Summary>
/// <Returns> </returns>
Public list <newsinfoss> AAA ()
{
String SQL = "select * From newsinfo ";
VaR DB = new petapoco. Database ("connstr ");
Return dB. Fetch <newsinfoss> (SQL );
}

///


/// obtain a news item based on the news ID.
///
//
//
public list AAA (string newsid)
{< br> string SQL = "select newstitle, newscontent, convert (char (10), newsdate, 120) as newsdate from newsinfo where newsid =" + newsid;
var DB = new petapoco. database ("connstr");
return dB. fetch (SQL);
}

///


/// obtain the corresponding data based on the ID
///
//
//
Public newsinfoss getnewsbyid (int id)
{< br> return dB. singleordefault ("select newstitle, newscontent, convert (char (10), newsdate, 120) as newsdate from newsinfo where newsid = @ 0", ID );
}

/// <Summary>
/// Query a piece of data that needs to be updated based on the news ID
/// </Summary>
/// <Param name = "ID"> </param>
/// <Returns> </returns>
Public newsinfoss Update (string ID)
{
Return dB. singleordefault <newsinfoss> ("select * From newsinfo where newsid = @ 0", ID );
}
}

3. Add an edit. ASPX page to display news.

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "Edit. aspx. cs" inherits = "edit" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: gridview id = "mygrid" runat = "server" autogeneratecolumns = "false"
Datakeynames = "newsid" onrowdeleting = "mygrid_rowdeleting">
<Columns>
<Asp: boundfield datafield = "newsid" headertext = "News No."/>
<Asp: boundfield datafield = "newstitle" headertext = "news title"/>
<Asp: boundfield datafield = "newscontent" headertext = "news content"/>
<Asp: boundfield datafield = "newsdate" headertext = "Release Date"/>
<Asp: templatefield>
<Itemtemplate>
<Asp: linkbutton id = "linkid" runat = "server"
Commandargument = '<% # eval ("newsid") %>'
Onclick = "linkid_click" tooltip = '<% # eval ("newsid") %>'
> Modify </ASP: linkbutton>
<Asp: linkbutton id = "lkdele" runat = "server" causesvalidation = "false" commandname = "delete"
TEXT = "delete" onclientclick = "Return confirm ('Are you sure you want to delete it? '); "> </ASP: linkbutton>
</Itemtemplate>
</ASP: templatefield>
</Columns>
</ASP: gridview>
</Div>
</Form>
</Body>
</Html>

CSCodeAs follows:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Data;
Using system. Data. sqlclient;

Public partial class EDIT: system. Web. UI. Page
{

Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Databindmygrid ();
}
}

Protected void linkid_click (Object sender, eventargs E)
{
Linkbutton BTN = sender as linkbutton;
String id = BTN. tooltip. tostring ();
Response. Redirect ("addnews. aspx? Newsid = "+ id );
}

Public void databindmygrid ()
{
Mygrid. datasource = newshelp. databindgrid ();
Mygrid. databind ();
}

Protected void mygrid_rowdeleting (Object sender, gridviewdeleteeventargs E)
{
String id = mygrid. datakeys [E. rowindex]. Values [0]. tostring ();
Int I = newshelp. deletegrid (ID );
If (I> 0)
{
Response. Write ("<SCRIPT> alert ('deleted successfully !!!! ') </SCRIPT> ");
}
Databindmygrid ();
}
}

 

 

4. Create a page for adding and editing news. Ceshi. aspx

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "Ceshi. aspx. cs" inherits = "Ceshi" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Style type = "text/CSS">

. Style1
{
Width: 300px;
Border-collapse: collapse;
Border-style: solid;
Border-width: 1px;
}
</Style>
<SCRIPT src = "scripts/calendar4.js" type = "text/JavaScript"> </SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>

<Table border = "1" class = "style1">
<Tr>
<TD>
News Title: </TD>
<TD>
<Asp: textbox id = "txttitle" runat = "server"> </ASP: textbox>
</TD>
</Tr>
<Tr>
<TD>
News content: </TD>
<TD>
<Asp: textbox id = "txtcontent" runat = "server"> </ASP: textbox>

</TD>
</Tr>
<Tr>
<TD>
Release date: </TD>
<TD>
<Asp: textbox id = "txtdate" runat = "server" onclick = "mycalendar. setdate (this)"> </ASP: textbox>

</TD>
</Tr>
<Tr>
<TD colspan = "2" style = "text-align: Center">
<Asp: button id = "button1" runat = "server" text = "save" onclick = "button#click"/>
</TD>
</Tr>
</Table>

</Div>
</Form>
</Body>
</Html>

 

The CS code is as follows:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using petapoco;

Public partial class Ceshi: system. Web. UI. Page
{
String newsid = "";
Newssql SQL = new newssql ();
Petapoco. Database DB = new petapoco. Database ("connstr ");
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
If (request ["newsid"]! = NULL & request ["newsid"]. tostring (). Trim ()! = "")
{
Newsid = request ["newsid"]. tostring ();
Datanewsinfo (convert. toint32 (newsid ));
}
}
}
/// <Summary>
/// Click the Add or update button.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void button#click (Object sender, eventargs E)
{
If (request ["newsid"]! = NULL & request ["newsid"]. tostring (). Trim ()! = "")
{
Update ();
Response. Redirect ("newsinfoedit. aspx ");
}
Else
{

Addnewsinfo ();
}
}
/// <Summary>
/// Add a news Method
/// </Summary>
Private void addnewsinfo ()
{
VaR A = new newsinfoss ();
A. newstitle = txttitle. Text. Trim ();
A. newscontent = txtcontent. Text. Trim ();
A. newsdate = convert. todatetime (txtdate. Text. Trim ());
DB. insert ("newsinfo", "newsid", true, );
Response. Redirect ("newsinfoedit. aspx ");
}
/// <Summary>
/// Method for updating news
/// </Summary>
Void Update ()
{

String sqlid = request ["newsid"]. tostring ();
Newsinfoss up = SQL. Update (sqlid );
Up. newstitle = txttitle. Text. Trim ();
Up. newscontent = txtcontent. Text. Trim ();
Up. newsdate = convert. todatetime (txtdate. Text. Trim ());
DB. Update ("newsinfo", "newsid", up, sqlid );
}

/// <Summary>
/// Obtain the corresponding news Based on the news ID
/// </Summary>
/// <Param name = "ID"> </param>
Void datanewsinfo (int id)
{
Newsinfoss A = new newsinfoss ();
A = SQL. getnewsbyid (ID );
Txttitle. Text = A. newstitle;
Txtcontent. Text = A. newscontent;
Txtdate. Text = A. newsdate. tostring ("yyyy-mm-dd"). tostring ();
}
}

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.