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}
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}
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}