使用 DataAdapter 和 DataSet 更新数据库

DataAdapter 的 Update 方法:将 DataSet 中的更改解析回数据源。DataSet保存的数据是位于服务器内存里面的原数据库的“副本”。所以用DataSet更新数据的过程就是先对“副本”进行更新,然后在将“原本”更新。

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

EntityAA.cs
 1 None.gif using  System;
 2 None.gif using  System.Data;
 3 None.gif using  System.Data.SqlClient;
 4 None.gif
 5 None.gif namespace  DataSetAdapter
 6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 8InBlock.gif    /// Summary description for EntityAA.
 9ExpandedSubBlockEnd.gif    /// </summary>

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

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

46InBlock.gif
47InBlock.gif        public DataSet List
48ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
49ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gif{return ds;}
50ExpandedSubBlockEnd.gif        }

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

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

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

80InBlock.gif
81InBlock.gif
82ExpandedSubBlockEnd.gif    }

83ExpandedBlockEnd.gif}

84 None.gif

WebForm1.aspx.cs
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Data.SqlClient;
None.gif
None.gif
namespace  DataSetAdapter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for WebForm1.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class WebForm1 : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.Label Label1;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label2;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox txt_a;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox txt_c;
InBlock.gif        
protected System.Web.UI.WebControls.Button delete;
InBlock.gif        
protected System.Web.UI.WebControls.Button Button2;
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid DataGrid1;
InBlock.gif        
protected System.Web.UI.WebControls.Button Button1;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(!this.Page.IsPostBack)
InBlock.gif                BindGrid();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Web Form Designer generated code#region Web Form Designer generated code
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required method for Designer support - do not modify
InBlock.gif        
/// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Button1.Click += new System.EventHandler(this.Button1_Click);
InBlock.gif            
this.delete.Click += new System.EventHandler(this.delete_Click);
InBlock.gif            
this.Button2.Click += new System.EventHandler(this.Button2_Click);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void BindGrid()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            EntityAA entityaa 
= new EntityAA();
InBlock.gif            DataSet ds 
= entityaa.List;
InBlock.gif
InBlock.gif            
this.DataGrid1.DataSource = ds;
InBlock.gif            
this.DataGrid1.DataBind();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        
private void Button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int ids = Int32.Parse(this.txt_a.Text);
InBlock.gif            
string name = this.txt_c.Text;
InBlock.gif
InBlock.gif            EntityAA entityaa 
= new EntityAA();
InBlock.gif            entityaa.up_date(ids,name);
InBlock.gif
InBlock.gif            BindGrid();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
private void delete_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int ids = Int32.Parse(this.txt_a.Text);
InBlock.gif
InBlock.gif            EntityAA entityaa 
= new EntityAA();
InBlock.gif            entityaa.del(ids);
InBlock.gif
InBlock.gif            BindGrid();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
InBlock.gif
InBlock.gif        
private void Button2_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string c = this.txt_c.Text;
InBlock.gif            
InBlock.gif            EntityAA entityaa 
= new EntityAA();
InBlock.gif            entityaa.insert(c);
InBlock.gif
InBlock.gif            BindGrid();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值