Frog frog recommendation: basic database drill code in Asp.net

Source: Internet
Author: User

Frog frog recommendation: basic database drill code in Asp.net
The database operation methods for each programming language are different from those for database operations. Being familiar with database operations for Asp.net is the first thing that every beginner must master, this article uses a simple example to show you the rich database operation classes under Asp.net. This example demonstrates the database binding, sorting, paging, dataset, dataview, the simple use of datareader and so on is simple, but it is enough to prove the change in the concept of database access under Asp.net. There are many bugs in this example. You should pay attention to it in actual application, for example, when opening a database, use a structured error processing statement to capture Possible errors, and explicitly close the database; this article did not write these statements to make readers focus on database operations. no too many comments are made in the code to make the program more compact.
In addition, we recommend that new users not rely on. net and other visual programming tools to write ASP. net, you need to use some text editors for some common C # script contact. If you have any questions in the code or have some comments on this article, please contact me. Thank you.
The following is the source code. Save the two files to the same directory and access Wawa. aspx is enough, provided that you have configured sqlser and installed the sample database. In addition, you may need to modify the user and password in the connection string of the database:

Wawa. aspx

The following is a reference clip:
<% @ Page Language = "C #" src = "Wawa. aspx. cs" inherits = "Wawa. Song" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> song </title>
<Meta name = "generator" content = "Microsoft Visual Studio. NET 7.1">
<Meta name = "code_language" content = "C #">
<Meta name = "vs_defaultclientscript" content = "JavaScript">
<Meta name = "vs_targetschema" content = "http://schemas.microsoft.com/intellisense/ie5">
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Font face = "">
<Table id = "Table1" style = "Z-INDEX: 104; left: 160px; position: absolute; top: 56px" cellspacing = "1"
Cellpadding = "1" width = "300" border = "1">
<Tr>
<TD>
<Asp: Label id = "label2" runat = "server" font-bold = "true"> This is an example of using datareader and stringbuilder </ASP: Label> </TD>
</Tr>
<Tr>
<TD>
<Asp: literal id = "htmlcontent" runat = "server"> </ASP: literal> </TD>
</Tr>
<Tr>
<TD>
<Asp: Label id = "label1" runat = "server" font-bold = "true"> This is an example of binding to a DataGrid and sorting </ASP: label> </TD>
</Tr>
<Tr>
<TD>
<Asp: DataGrid id = "datagrid1" runat = "server" allowsorting = "true">
<Columns>
<Asp: boundcolumn> </ASP: boundcolumn>
</Columns>
</ASP: DataGrid> </TD>
</Tr>
<Tr>
<TD>
<Asp: Label id = "label3" runat = "server"> This is an example of using DataGrid paging </ASP: Label> </TD>
</Tr>
<Tr>
<TD>
<Asp: DataGrid id = "datagrid2" runat = "server" pagesize = "2" allowpaging = "true">
<Pagerstyle nextpagetext = "Next" prevpagetext = "previous"> </pagerstyle>
</ASP: DataGrid> </TD>
</Tr>
</Table>
</Font>
</Form>
</Body>
</Html>
 

Wawa. aspx. CS
The following is a reference clip:
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Using system. text;
Namespace Wawa
{
Public class song: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. Literal htmlcontent;
Protected system. Web. UI. webcontrols. Label label1;
Protected system. Web. UI. webcontrols. Label label2;
Protected system. Web. UI. webcontrols. Label label3;
Protected system. Web. UI. webcontrols. DataGrid datagrid2;
Protected system. Web. UI. webcontrols. DataGrid datagrid1;
Protected dataview view1 = new dataview ();
Protected sqlcommand cmd = new sqlcommand ();
Private void page_load (Object sender, system. eventargs E)
{
Opendb ();
Showreader ();
Datagrid1_bind ();
Datagrid2_bind ();
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
// Override the onint event of the system. Web. UI. Page class
Initializecomponent (); // call the initializecomponent () function
Base. oninit (E); // call the oninit method of the parent class to initialize this event.
}
Private void initializecomponent ()
{
// Connect some events
This. datagrid1.sortcommand + = new system. Web. UI. webcontrols. datagridsortcommandeventhandler (this. datagrid1_sortcommand );
This. Maid + = new system. Web. UI. webcontrols. Maid (this. Maid );
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

Private void opendb (){
// Open the database and create a sqldatareader: reader, a sqldataadapter: Da, and a dataset: Ds. Fill the DS employees table with DA,
// Assign the default view value of the Employees table to the private view variable view1.
String connstring = "Server = 192.168.0.110; database = northwind; uid = sa; Pwd = sa ;";
String SQL = @ "select top 5 employeeid, titleofcourtesy +'' + firstname + ''+ lastname as fullname from employees order by employeeid DESC ";
Sqlconnection conn = new sqlconnection (connstring );
Cmd. commandtext = SQL;
Cmd. Connection = conn;
Conn. open ();
Sqldataadapter da = new sqldataadapter (SQL, Conn );
Dataset DS = new dataset ();
Da. Fill (DS, "employees ");
View1 = Ds. Tables ["employees"]. defaultview;
}
Private void showreader (){
// Bind datareader to a stringbuilder object and use htmlcontent to output
// Do not forget to reference the system. Text namespace if you want to use the stringbuilder object.
Sqldatareader reader = cmd. executereader ();
Stringbuilder htmlstr = new stringbuilder ("");
While (reader. Read ()){
Htmlstr. append ("<li> ");
Htmlstr. append (Reader ["employeeid"]);
Htmlstr. append ("& nbsp ;");
Htmlstr. append (reader. getstring (1 ));
Htmlstr. append ("</LI> ");
}
Reader. Close ();
Htmlcontent. Text = htmlstr. tostring ();
}
Private void datagrid1_bind (){
// Bind the datagrid1
Datagrid1.datasource = view1;
Datagrid1.databind ();
}
Private void datagrid2_bind (){
// Bind datagrid2
Datagrid2.datasource = view1;
Datagrid2.databind ();
}
Private void datagrid1_sortcommand (Object source, system. Web. UI. webcontrols. datagridsortcommandeventargs E)
{
// Functions triggered by the sort event of the datagrid1
Viewstate ["sortexpreesion"] = E. sortexpression;
If (viewstate ["sortexpreesion"]! = NULL ){
View1.sort = viewstate ["sortexpreesion"]. tostring ();
Datagrid1_bind ();
}
}
Private void datagrid2_pageindexchanged (Object source, system. Web. UI. webcontrols. datagridpagechangedeventargs E)
// Functions triggered by the paging time of the datagrid2
{
Datagrid2.currentpageindex = E. newpageindex;
Datagrid2_bind ();
}
}
}

 

 

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.