The simplest asp.net database operations sample source code

Source: Internet
Author: User
Tags net tostring

Using System;
Using System.Collections;
Using System.Configuration;
Using System.Data;
Using System.Linq;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Xml.Linq;
Using System.Data.SqlClient;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
SqlConnection con = new SqlConnection ("SERVER=DBTALBE;UID=SA;PWD=SA;DATABASE=ADDB;");
Con. Open ();
SqlConnection con = new SqlConnection (configurationmanager.appsettings["strconn"));
Con. Open ();
/* Write to the database
SqlCommand cmd = new SqlCommand ("INSERT into [Ad_sort] ([Sortname]) VALUES (' Home ads ')", con);
Cmd. ExecuteNonQuery ();
Response.Write ("Write succeeded!") ");
Con. Close ();
*/
String strSQL = "SELECT * from Ad_sort ORDER by id DESC";
Reading data
SqlDataAdapter SDA = new SqlDataAdapter (strSQL, con);
DataSet ds = new DataSet ();
Sda. Fill (ds, "Ad_sort");
This.hotcommentlist.DataSource = ds;
This.hotcommentlist.DataBind ();

Read single data
SqlDataAdapter Set = new SqlDataAdapter (strSQL, con);
DataSet Rs = new DataSet ();
Set.fill (Rs, "Ad_sort");
DataRowView Mydrview = rs.tables["Ad_sort"]. DEFAULTVIEW[0];
this.sortname.InnerHtml = convert.tostring (rs.tables["Ad_sort"). Defaultview[0]. ROW[1]);
/*
SqlDataAdapter Set = new SqlDataAdapter (Strsql,con);
DataSet Rs = new DataSet ();
Set.fill (Rs, "Ad_sort");
this.sortname.InnerHtml = convert.tostring (rs.tables["Ad_sort"). Defaultview[0]. ROW[1]);
this.sortname.InnerHtml = convert.tostring (rs.tables["Ad_sort"). Defaultview[0]. ROW[1]);
*/

SqlCommand cmd = new SqlCommand (strSQL, con);
SqlDataReader Res1 = cmd. ExecuteReader ();
String Listbody= "";
Listbody + + ("<table border=0");
listbody+= ("<tr><td>executereader read Data </td></tr>");
Listbody + + ("<tr><th> number </th><th> name </th></tr>");
while (Res1.read ())
{
Listbody + + ("<tr>");
Listbody + = ("<td align= ' center ' >" + res1["id"]. ToString () + "</td>");
Listbody + = ("<td align= ' left ' >" + res1["Sortname"]. ToString () + "</td>");
Listbody + + ("</tr>");
}
Listbody + + ("</table>");
List2. InnerHtml = Listbody;
Res1.close ();

Con. Close ();
}
}



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.