成都网站推广
 

中联无限-服务热线:028-86637122 工作时间:9:00-18:00 星期一-星期五

 
首页 | 关于我们 | 新闻动态 | 成都网站建设 | 成都网站推广 | 成都微信营销 | 网站制作案例 | 软件开发 | 常见问题 | 域名注册 | 虚拟主机
  网站知识
     推荐工具 / Tools
     网站运营/Operation
  
  客户案例
·巨丰达人减肥训练营
·成都写意经典家具有限公司
·成都卡姆士安防设备有限公司
·新千里装饰集团
·成都吕氏三才中医博爱堂
·泊菲特语言学校
·四川高地易景园林工程公司
·成都烽火建筑装饰设计有限公司
·优尼客酒店
·成都杜嘉机电有限公司
·菲颖1购
·成都安东尼体育文化传播有限公
·四川九章生物化工科技发展有限
·成都环球体育文化传播有限公司
·万友爱淘网上商城
·四川省斯博兰德建筑装饰设计有
·肥犇仔
·成都市猛追湾游泳场
  网站运营
·企业新站推广中存在的六大误区
·网站运营之八大要素
·怎么区别网站运营与网络运营
·怎样区分网站运营和网络运营
·SQL Server 200
·新网站运营后如何迅速增加网站
·减肥网站是怎样提高收入的
·个人网站赢利模式的拓展方向
·商业门户网站运营模式探讨
·成都地方门户网站运营模式
·网站策划人如何盈利
·网站运营模式观察与分析
  最新资讯
·成都网络推广公司介绍的六种推
·分析导致成都网络推广效果差的
·成都网站优化:企业网站关键词
·成都网站优化初期企业应该需要
·浅析成都网站优化中企业如何建
·分析企业在做成都网站优化过程
·短视频营销可以为成都网络推广
·小品牌怎么样才能既省钱又省力
·分析成都网络推广中企业需要注
·如何根据成都网络推广的营销周
·成都网站优化需要遵循哪些规则
·成都SEO网站优化过程中如何
 
   您的位置在:成都网站推广 > 网站知识 > 网站教程 /Tutorial > 正文
   网站教程 /Tutorial

ASP.NET入门之数据篇

[ 来源:中联无限科技有限公司   发布日期:2010/10/9 10:49:57 ]
对于网站编程的初学者来说,总是会上网找些源码来看,但久而久之还是停留在改代码的阶段,并不明白怎样去写一个完整的网站程序.有见如此我就开始写这样的文章(c#版),不足之处请批评指正. 数据库连接篇 在WEB项目里看到Web.config配置文件,在configuration这行加入下面代码用于和SQL服务器进行连接

中联无限科技公司提供专业的成都网站建设成都网站设计成都网站制作成都网站推广

对于网站编程的初学者来说,总是会上网找些源码来看,但久而久之还是停留在改代码的阶段,并不明白怎样去写一个完整的网站程序.有见如此我就开始写这样的文章(c#版),不足之处请批评指正.

数据库连接篇

在WEB项目里看到Web.config配置文件,在configuration这行加入下面代码用于和SQL服务器进行连接

<appSettings>
<!-- 数据库连接字符串 -->
<add key="ConnStr" value="Data Source=localhost;database=company;UID=sa;Password=;Persist Security Info=True;" />
</appSettings>

数据列表显示篇,如图:

 


using System;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

//引用命名空间:SQL托管,配置文件
using System.Data.SqlClient;
using System.Configuration;


public partial class _Default : System.Web.UI.Page
{
    protected SqlConnection myconn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
    //读取web.config配置文件中的数据库连接字符串,并连接到指定的数据库

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)//判断页面是否第一次运行
     {

          string strsql="select * from Product";//定义一个数据库的查询字符串
                DataSet ds = new DataSet();
                myconn.Open();//打开数据库连接
                SqlDataAdapter command = new SqlDataAdapter(strsql,myconn);//表示用于填充DataSet 和更新SQL Server 数据库的一组数据命令和一个数据库连接
                command.Fill(ds, "Product");
                productList.DataSource = ds.Tables[0].DefaultView;
                productList.DataBind();
                ds.Clear();
          myconn.Close();//关闭数据库连接
            }
 }

    protected void grid_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        foreach (System.Web.UI.WebControls.HyperLink link in e.Item.Cells[7].Controls)
        {
            link.Attributes.Add("onClick", "if (!window.confirm('您真的要删除这条记录吗?')){return false;}");
        }
    }

}


protected void btnAdd_Click(object sender, EventArgs e)
    {
        string ProductId = this.txtProductId.Text;
        string CategoryId = this.txtCategoryId.Text;
        string Name = this.txtName.Text;
        string Description = this.txtDescription.Text;
        string Price =this.txtPrice.Text;

        string sql_Exeits = "select * from Product where ProductId='" + ProductId + "'";
        SqlCommand cmd_Exeits = new SqlCommand(sql_Exeits, myconn);
        myconn.Open();
        SqlDataReader rdr = cmd_Exeits.ExecuteReader();
        while (rdr.Read())
        {
            Response.Write("<script language='JavaScript'>");
            Response.Write("alert('对不起,该产品编号已经存在!')");
            Response.Write("</script>");
            this.txtCategoryId.Text = "";
            this.txtDescription.Text = "";
            this.txtName.Text = "";
            this.txtPrice.Text = "";
            this.txtProductId.Text = "";
            return;
        }
        rdr.Close();


        string sql_add = "insert into Product(ProductId,CategoryId,Name,Description,Price)values('" + ProductId + "','" + CategoryId + "','" + Name + "','" + Description + "','" + Price + "')";
        SqlCommand cmd_add = new SqlCommand(sql_add, myconn);//SqlCommand:表示要对SQL Server数据库执行的一个Transact-SQL语句或存储过程
        cmd_add.ExecuteNonQuery();//对连接执行Transact-SQL语句并返回受影响的行数。对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。对于所有其他类型的语句,返回值为 -1。如果发生回滚,返回值也为 -1。
        myconn.Dispose();
        myconn.Close();
    }
[/CODE


[COLOR=Red]数据显示篇[/COLOR]
[CODE]
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = Request.Params["id"];
            if (id == null || id.Trim() == "")
            {
                Response.Redirect("default.aspx");
                Response.End();
            }
            else
            {
                string sql_show = "select * from Product Where ProductId=" + id;
                SqlCommand cmd_show = new SqlCommand(sql_show, conn);
                conn.Open();
                SqlDataReader rd_show = cmd_show.ExecuteReader();//使用SqlDataReader对象读取并返回一个记录集
                shows.DataSource = rd_show;//指向数据源
                shows.DataBind();//绑定数据
                rd_show.Close();//关闭SqlDataReader
             }
         }
    }


数据修改篇


    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string ProductId = this.lblProductId.Text;
        string CategoryId = this.txtCategoryId.Text;
        string Name = this.txtName.Text;
        string Description = this.txtDescription.Text;
        decimal Price = decimal.Parse(this.txtPrice.Text);

        string sql_edit = "update Product set CategoryId='" + CategoryId + "',Name='" + Name + "',Description='" + Description + "',Price='" + Price + "' where ProductId =" + ProductId;
        SqlCommand cmd_edit = new SqlCommand(sql_edit, conn);

        conn.Open();
        cmd_edit.ExecuteNonQuery();
        conn.Close();
        Response.Write("<script language=javascript>window.alert('保存成功!')</script>");
        Response.Redirect("show.aspx?id=" + ProductId);
    }


数据删除篇

 

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string ProductId = Request.Params["id"];
            string sql_del = "delete from Product where ProductId=" + ProductId;
            SqlCommand cmd_del = new SqlCommand(sql_del, conn);
            conn.Open();
            cmd_del.ExecuteNonQuery();
            conn.Close();
            Response.Redirect("default.aspx");
        }
    }

中联无限科技公司提供专业的成都网站建设成都网站设计成都网站制作成都网站推广


上一篇:网站设计的同时应考虑到SEO优化
下一篇:robots.txt和Robots META标签
   相关网站教程 /Tutorial
·SEO之如何更好的布局?2011-08-11
·Ajax2011-05-26
·JSP的定义2011-05-26
·PHP是什么2011-05-26
·CSS教程:CSS背景全攻略2011-03-26
·javascript cookies 存2011-03-25
·网站外部链接的注意事项2011-03-24
·PHP防御木马攻击的技巧2011-03-11
·Apache服务器的配置与管理2011-03-11
·SQL Server数据库查询优化的常用2011-03-09
·SQL Server触发器2011-03-09
·ASP.NET JMAIL 发送邮件方法2011-03-09
首页 | 公司简介 |联系方式 |付款方式 |人才招聘 | 域名空间 | 网页设计案例 | 网站知识 | 解决方案 | 建站指南 | 网站地图
地址:成都市青羊区西大街1号   邮编:610041   E- Mail:619027769@qq.com   代理合作: 028-86637122
网站建设/推广咨询:028-86637122  86618860  传真:028-86637322   
成都中联无限科技有限公司 2004-2014    蜀ICP备05017733号