随笔 - 1330  文章 - 1  评论 - 378  阅读 - 482万 
DataAdapter 的 Update 方法:将 DataSet 中的更改解析回数据源。DataSet保存的数据是位于服务器内存里面的原数据库的“副本”。所以用DataSet更新数据的过程就是先对“副本”进行更新,然后在将“原本”更新。

Update 方法会将更改解析回数据源,但是自上次填充 DataSet 以来,其他客户端可能已修改了数据源中的数据。若要使用当前数据刷新 DataSet,请再次使用 DataAdapter 填充 (Fill) DataSet。

 1using System;
 2using System.Data;
 3using System.Data.SqlClient;
 4
 5namespace DataSetAdapter
 6{
 7    /**//// <summary>
 8   /// Summary description for EntityAA.
 9    /// </summary>

10    public class EntityAA
11    {
12        private string connstr = System.Configuration.ConfigurationSettings.AppSettings["connString"];
13        private SqlConnection conn;
14
15        private string sql;
16
17        private SqlDataAdapter adp;
18        private SqlCommandBuilder cb;
19
20        private DataSet ds;
21        private DataTable dt;
22
23        public EntityAA()
24        {
25            conn = new SqlConnection(connstr);
26            sql = "select * from aa";
27
28            adp = new SqlDataAdapter(sql,conn);
29            cb = new SqlCommandBuilder(adp);
30
31            ds = new DataSet();
32
33            FillDataSet();
34
35            dt = ds.Tables["table_aa"];
36
37            dt.PrimaryKey = new DataColumn[]{dt.Columns["a"]};
38        }

39        
40       private void FillDataSet()
41        {
42            conn.Open();
43            adp.Fill(ds,"table_aa");
44            conn.Close();
45       }

46
47        public DataSet List
48        {
49            get {return ds;}
50        }

51
52       public void insert(string c)
53        {
54            dt.Columns["a"].AutoIncrement = true;                        
55
56           DataRow dr = dt.NewRow();
57            dr["c"= c;
58           dt.Rows.Add(dr);                //添加新行
59
60           adp.Update(ds,"table_aa");
61
62       }

63
64       public void up_date(int ids,string name)
65       {
66           DataRow dr = dt.Rows.Find(ids);        //获取由主键值指定的行
67            dr["c"= name;                        //更新
68
69            adp.Update(ds,"table_aa");
70        }

71
72        public void del(int ids)
73       {
74           DataRow dr = dt.Rows.Find(ids);        //获取由主键值指定的行
75            dr.Delete();
76
77           adp.Update(ds,"table_aa");
78
79       }

80
81    }

82}
 
 
 1using System;
 2using System.Collections;
 3using System.ComponentModel;
 4using System.Data;
 5using System.Drawing;
 6using System.Web;
 7using System.Web.SessionState;
 8using System.Web.UI;
 9using System.Web.UI.WebControls;
10using System.Web.UI.HtmlControls;
11using System.Data.SqlClient;
12
13namespace DataSetAdapter
14{
15    /**//// <summary>
16    /// Summary description for WebForm1.
17    /// </summary>

18    public class WebForm1 : System.Web.UI.Page
19    {
20        protected System.Web.UI.WebControls.Label Label1;
21        protected System.Web.UI.WebControls.Label Label2;
22        protected System.Web.UI.WebControls.TextBox txt_a;
23        protected System.Web.UI.WebControls.TextBox txt_c;
24        protected System.Web.UI.WebControls.Button delete;
25        protected System.Web.UI.WebControls.Button Button2;
26        protected System.Web.UI.WebControls.DataGrid DataGrid1;
27        protected System.Web.UI.WebControls.Button Button1;
28    
29        private void Page_Load(object sender, System.EventArgs e)
30        {
31            if(!this.Page.IsPostBack)
32                BindGrid();
33        }

34
35        Web Form Designer generated codeWeb Form Designer generated code
58
59        private void BindGrid()
60        {
61            EntityAA entityaa = new EntityAA();
62            DataSet ds = entityaa.List;
63
64            this.DataGrid1.DataSource = ds;
65            this.DataGrid1.DataBind();
66        }

67        private void Button1_Click(object sender, System.EventArgs e)
68        {
69            int ids = Int32.Parse(this.txt_a.Text);
70            string name = this.txt_c.Text;
71
72            EntityAA entityaa = new EntityAA();
73            entityaa.up_date(ids,name);
74
75            BindGrid();
76        }

77        private void delete_Click(object sender, System.EventArgs e)
78        {
79            int ids = Int32.Parse(this.txt_a.Text);
80
81            EntityAA entityaa = new EntityAA();
82            entityaa.del(ids);
83
84            BindGrid();
85        }

86
87        private void Button2_Click(object sender, System.EventArgs e)
88        {
89            string c = this.txt_c.Text;
90            
91            EntityAA entityaa = new EntityAA();
92            entityaa.insert(c);
93
94            BindGrid();
95        }

96
97    }

98}
 
posted on   钱途无梁  阅读(392)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示