using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace ADOExamples
{
    public partial class Form4
    {
        SqlConnection cn;
        SqlCommand cm;
        SqlDataAdapter da;
        SqlCommandBuilder cmb;
        DataSet ds;        public Form4()
        {
            InitializeComponent();
        }        private void LoadButton_Click(object sender, EventArgs e)
        {
            da.Fill(ds, "Customers");
            CIDTB.DataBindings.Add("Text", ds, "Customers.CustomerID");
            CNTB.DataBindings.Add("Text", ds, "Customers.CustomerName");
            MCTB.DataBindings.Add("Text", ds, "Customers.MemberCategory");
        }        private void Form4_Load(object sender, EventArgs e)
        {
            string conS = "data source=(local); integrated security=SSPI; initial catalog=Northwind";
            cn = new SqlConnection(conS);
            cm = new SqlCommand("Select CustomerID,CustomerName, MemberCategory from Customers", cn);
            da = new SqlDataAdapter(cm);
            cmb = new SqlCommandBuilder(da);
            ds = new DataSet();
        }        private void NextButton_Click(object sender, EventArgs e)
        {
            this.BindingContext[ds, "Customers"].Position++;
        }        private void PrevButton_Click(object sender, EventArgs e)
        {
            this.BindingContext[ds, "Customers"].Position--;
        }        private void FirstRecButton_Click(object sender, EventArgs e)
        {
            this.BindingContext[ds, "Customers"].Position = 0;
        }        private void LastRecButton_Click(object sender, EventArgs e)
        {
            this.BindingContext[ds, "Customers"].Position = ds.Tables["Customers"].Rows.Count-1;
        }        private void Update_Click(object sender, EventArgs e)
        {
            this.BindingContext[ds, "Customers"].EndCurrentEdit();
            da.Update(ds, "Customers");
        }        private void DeleteButton_Click(object sender, EventArgs e)
        {
            ds.Tables["Customers"].Rows[(this.BindingContext[ds, "Customers"].Position)].Delete();
        }        private void InsertButton_Click(object sender, EventArgs e)
        {
            this.BindingContext[ds, "Customers"].AddNew();
        }    }
}
如果说textbox控件绑定的只是内存中的本地DataSet,那么以上代码中,是不是需要将所有更新数据操作的EventHandler最后一句加上da.Update(ds, "Customers");?而不是如以上代码中,仅仅在Update按钮中有此句?

解决方案 »

  1.   

    不知楼主所云:
    1、数据从数据库取出来后,保存在程序所在的机器内存中
    2、数据返回到页面(客户端)显示后,即以HTML代码的方式保存在客户端,服务器端会释放相应的内存空间
    3、页面修改数据后需要重新使用Update方法写回数据库
      

  2.   

    多谢各位前辈的热心解答,可是,那段代码是winform程序的
      

  3.   

    dataset是放内存中的,你用dataset来绑定,就是取内存中的数据,没有加da.Update,就只是修改内存中的数据,加了,则会把这个在内存中改变后的数据更新到数据库,