DataAdapter 的 Update 方法:将 DataSet 中的更改解析回数据源。DataSet保存的数据是位于服务器内存里面的原数据库的“副本”。所以用DataSet更新数据的过程就是先对“副本”进行更新,然后在将“原本”更新。
Update 方法会将更改解析回数据源,但是自上次填充 DataSet 以来,其他客户端可能已修改了数据源中的数据。若要使用当前数据刷新 DataSet,请再次使用 DataAdapter 填充 (Fill) DataSet。
Update 方法会将更改解析回数据源,但是自上次填充 DataSet 以来,其他客户端可能已修改了数据源中的数据。若要使用当前数据刷新 DataSet,请再次使用 DataAdapter 填充 (Fill) DataSet。
1
using System;
2
using System.Data;
3
using System.Data.SqlClient;
4![]()
5
namespace 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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Web;
7
using System.Web.SessionState;
8
using System.Web.UI;
9
using System.Web.UI.WebControls;
10
using System.Web.UI.HtmlControls;
11
using System.Data.SqlClient;
12![]()
13
namespace 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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98
