Default.aspx

View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default2" %>

<!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>
    
    </div>
    </form>
</body>
</html>

Default.aspx.cs

View Code
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 Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // 创建数据库连接对象
        SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
        //创建数据适配器
        SqlDataAdapter dap = new SqlDataAdapter("select ID,Name,BccdPrice from bccd", con);
        //创建数据集
        DataSet ds = new DataSet();
        //填充数据集
        dap.Fill(ds,"Bccd");
        //显示未更新前的数据信息
        ShowDsTable(ds.Tables[0]);
        //更改数据操作
        for (int i = 0; i <= ds.Tables["Bccd"].Rows.Count - 1; i++)
        {
            ds.Tables["Bccd"].Rows[i]["Name"] += DateTime.Today.ToShortDateString();
        }
        //创建SqlCommandBuilder对象,并和SqlDataAdapter关联
        SqlCommandBuilder builder = new SqlCommandBuilder(dap);
        //应用SqlDataAdapter和的Update方法更新数据
        dap.Update(ds, "Bccd");
        //显示更新后的数据
        ShowDsTable(ds.Tables[0]);
        dap.Dispose();
        ds.Dispose();
    }
    public void ShowDsTable(DataTable dataTable)
    {
        foreach (DataRow row in dataTable.Rows)
        {
            //输出表格
            Response.Write("<table border=1 align='center' ");
            Response.Write("<tr><th>编号</th><th>名称</th><th>价格</th></tr>");
            Response.Write("<tr>");
            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                //按照顺序以列名指定要读取的项
                Response.Write("<td align='center'>" + row[i] + "</td>");
            }
            Response.Write("</tr>");
            Response.Write("</table>");
        }
    }
}

 

posted on 2013-03-12 02:25  松竹梅  阅读(464)  评论(0编辑  收藏  举报