点击这里给我发消息 点击这里给我发消息

最简单的ASP.NET数据库操作示例源代码

添加时间:2013-12-6
    相关阅读: ASP ASP.NET 数据库

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();
        /*写入数据库
        SqlCommand cmd = new SqlCommand("INSERT INTO [Ad_Sort] ([sortname])  VALUES  ('首页广告')",con);
        cmd.ExecuteNonQuery();
        Response.Write("写入成功!");
        con.Close();
        */
        string strsql = "select * from ad_sort order by id desc";
        //读取数据
        SqlDataAdapter sda = new SqlDataAdapter(strsql, con);
        DataSet ds = new DataSet();
        sda.Fill(ds, "ad_sort");
        this.hotcommentlist.DataSource = ds;
        this.hotcommentlist.DataBind();

        //读取单个数据
        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读取数据</td></tr>");
        listbody += ("<tr><th>编号</th><th>名称</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();
    }
}

 


 

咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册