使用 DataAdapter 和 DataSet 更新数据库
生活随笔
收集整理的這篇文章主要介紹了
使用 DataAdapter 和 DataSet 更新数据库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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#region?Web?Form?Designer?generated?code
36????????override?protected?void?OnInit(EventArgs?e)
37????????{
38????????????//
39????????????//?CODEGEN:?This?call?is?required?by?the?ASP.NET?Web?Form?Designer.
40????????????//
41????????????InitializeComponent();
42????????????base.OnInit(e);
43????????}
44????????
45????????/**//**//**////?<summary>
46????????///?Required?method?for?Designer?support?-?do?not?modify
47????????///?the?contents?of?this?method?with?the?code?editor.
48????????///?</summary>
49????????private?void?InitializeComponent()
50????????{????
51????????????this.Button1.Click?+=?new?System.EventHandler(this.Button1_Click);
52????????????this.delete.Click?+=?new?System.EventHandler(this.delete_Click);
53????????????this.Button2.Click?+=?new?System.EventHandler(this.Button2_Click);
54????????????this.Load?+=?new?System.EventHandler(this.Page_Load);
55
56????????}
57????????#endregion
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}?
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#region?Web?Form?Designer?generated?code
36????????override?protected?void?OnInit(EventArgs?e)
37????????{
38????????????//
39????????????//?CODEGEN:?This?call?is?required?by?the?ASP.NET?Web?Form?Designer.
40????????????//
41????????????InitializeComponent();
42????????????base.OnInit(e);
43????????}
44????????
45????????/**//**//**////?<summary>
46????????///?Required?method?for?Designer?support?-?do?not?modify
47????????///?the?contents?of?this?method?with?the?code?editor.
48????????///?</summary>
49????????private?void?InitializeComponent()
50????????{????
51????????????this.Button1.Click?+=?new?System.EventHandler(this.Button1_Click);
52????????????this.delete.Click?+=?new?System.EventHandler(this.delete_Click);
53????????????this.Button2.Click?+=?new?System.EventHandler(this.Button2_Click);
54????????????this.Load?+=?new?System.EventHandler(this.Page_Load);
55
56????????}
57????????#endregion
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}?
轉載于:https://www.cnblogs.com/qiantuwuliang/archive/2009/06/02/1494266.html
總結
以上是生活随笔為你收集整理的使用 DataAdapter 和 DataSet 更新数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CCNA配置试验之六 标准ACL和扩展A
- 下一篇: c#的winform调用外部exe作为子