zoukankan      html  css  js  c++  java
  • 使用 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
    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}
     
  • 相关阅读:
    AX ERROR: Could not find my mock parent, most likely I am stale 不及格的程序员
    利用Segue在视图控制器间传值的问题 不及格的程序员
    Creating a Singleton Instance 不及格的程序员
    iPad 通知 UIKeyboardWillShowNotification 不会在keyBoard处在Undock状态下接到通知 不及格的程序员
    Why RootViewController's view is rotated Automatically by System when the app first loaded? 不及格的程序员
    如何弹出UIDatePicker最好 不及格的程序员
    jQuery开始做恶了 不及格的程序员
    what is the SEL,id and IMP,Class ,Method? 不及格的程序员
    Objectivec 字符串比较的陷井 不及格的程序员
    Unable to create any keyboard shortcuts after the iOS 6.1.3 update on iPad. 不及格的程序员
  • 原文地址:https://www.cnblogs.com/netwom/p/950352.html
Copyright © 2011-2022 走看看