如何将DataGrid中的修改的数据,放到数据库中呢?

主  题:  如何将DataGrid中的修改的数据,放到数据库中呢?
作  者:  zhangjinshui200 (望眼江湖)
等  级:  ^
信 誉 值:  100
所属论坛:  .NET技术 C#
问题点数:  50
回复次数:  12
发表时间:  2003-11-26 16:41:33

实现过程:
从一个函数中,获得数据集,将数据集绑定到DataGrid上面。
现在,从DataGrid中修改的数据,想把这个数据重新写回到数据库中,
必知道如何做了?
能够给几句完整的代码吗?
非常谢谢!



回复人: zhangjinshui200(望眼江湖) ( 一级(初级)) 信誉:100 2003-11-26 16:42:33 得分:0

实现过程:
从一个函数中,获得数据集,将数据集绑定到DataGrid上面。
现在,从DataGrid中修改的数据,想把这个数据重新写回到数据库中,
不知道如何做了?
能够给几句完整的代码吗?
非常谢谢!

回复人: polarlm(polarlm) ( 三级(初级)) 信誉:100 2003-11-26 16:46:38 得分:5

oleDbDataAdapter1.Update(dsAuthors1,"authors");

回复人: hare007(qghao) ( 一级(初级)) 信誉:100 2003-11-26 16:51:19 得分:5

up

回复人: keyplayer(冰) ( 一级(初级)) 信誉:92 2003-11-26 17:09:48 得分:10

sqlDbDataAdapter1.Update(dsAuthors1,"authors");

回复人: smiletosky(仰笑九天) ( 五级(中级)) 信誉:101 2003-11-26 17:11:40 得分:5

用dataset更新

回复人: zhangjinshui200(望眼江湖) ( 一级(初级)) 信誉:100 2003-11-26 17:15:53 得分:0

用了上面的做法,为什么提示如下信息呢?
Update requires a valid InsertCommand when passed DataRow collection with new rows.

回复人: heroniexiaowei(白痴) ( 一级(初级)) 信誉:100 2003-11-26 17:17:06 得分:10

string query="Update dsAuthors1 set authors=""";
OdbcCommand thisBuilder1 = new OdbcCommand(query, connv);
thisBuilder1.ExecuteNonQuery();

回复人: dahuzizyd(你就是我心中的女神) ( 一星(中级)) 信誉:105 2003-11-26 19:16:40 得分:0

是因为你的DataAdapter没有和selectCommand相对应的InsertCommand,可以使用Commandbuilder(OleDB和SQL的都有),也可是自己写Insert语句然后赋给DataAdapter

下面的是帮助里的,使用SQLCommandBuilder:
SqlDataAdapter 不会自动生成实现 DataSet 的更改与关联的 SQL Server 实例之间的协调所需的 Transact-SQL 语句。但是,如果设置了 SqlDataAdapter 的 SelectCommand 属性,则可以创建一个 SqlCommandBuilder 对象来自动生成用于单表更新的 Transact-SQL 语句。然后,SqlCommandBuilder 将生成其他任何未设置的 Transact-SQL 语句。

一旦设置 DataAdapter 属性,SqlCommandBuilder 就将其自身注册为 RowUpdating 事件的侦听器。一次只能将一个 SqlDataAdapter 与一个 SqlCommandBuilder 对象(或相反)互相关联。

为了生成 INSERT、UPDATE 或 DELETE 语句,SqlCommandBuilder 会自动使用 SelectCommand 属性来检索所需的元数据集。如果在检索元数据后(例如在第一次更新后)更改 SelectCommand,则应调用 RefreshSchema 方法来更新元数据。

SelectCommand 还必须至少返回一个主键列或唯一的列。如果什么都没有返回,就会产生 InvalidOperation 异常,不生成命令。

SqlCommandBuilder 还使用由 SelectCommand 引用的 Connection、CommandTimeout 和 Transaction 属性。如果修改了任何这些属性或者替换了 SelectCommand 本身,用户则应调用 RefreshSchema。否则,InsertCommand、UpdateCommand 和 DeleteCommand 属性将保留它们以前的值。

如果调用 Dispose,则会解除 SqlCommandBuilder 与 SqlDataAdapter 的关联,并且不再使用所生成的命令。

public static DataSet SelectSqlSrvRows(string myConnection, string mySelectQuery, string myTableName)
{
    SqlConnection myConn = new SqlConnection(myConnection);
    SqlDataAdapter myDataAdapter = new SqlDataAdapter();
    myDataAdapter.SelectCommand = new SqlCommand(mySelectQuery, myConn);
    SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter);

    myConn.Open();

    DataSet ds = new DataSet();
    myDataAdapter.Fill(ds, myTableName);

    //code to modify data in DataSet here

    //Without the SqlCommandBuilder this line would fail
    myDataAdapter.Update(ds, myTableName);

    myConn.Close();

    return ds;
}

使用自己写SQL语句的办法:

SqlDataAdapter catDA = new SqlDataAdapter("SELECT CategoryID, CategoryName FROM Categories", nwindConn);

catDA.UpdateCommand = new SqlCommand("UPDATE Categories SET CategoryName = @CategoryName " +
                                     "WHERE CategoryID = @CategoryID" , nwindConn);

catDA.UpdateCommand.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15, "CategoryName");

SqlParameter workParm = catDA.UpdateCommand.Parameters.Add("@CategoryID", SqlDbType.Int);
workParm.SourceColumn = "CategoryID";
workParm.SourceVersion = DataRowVersion.Original;

DataSet catDS = new DataSet();
catDA.Fill(catDS, "Categories");

DataRow cRow = catDS.Tables["Categories"].Rows[0];
cRow["CategoryName"] = "New Category";

catDA.Update(catDS);

回复人: mynewbigame(学习者) ( 一级(初级)) 信誉:100 2003-11-26 19:41:47 得分:15

c# & .net技术平台实战演练上的例子,我稍作修改:
form1.cs

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace csnetdata
{
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.TabControl tabControl1;
        private System.Windows.Forms.TabPage tabPage1;
        private System.Windows.Forms.TabPage tabPage2;
        private System.Windows.Forms.ListBox lstconsole;
        private System.Windows.Forms.DataGrid dataGrid1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button btnquit;
        private System.Windows.Forms.Button btnsave;
        private System.Windows.Forms.Button btnstart;
        private System.Windows.Forms.Button btnclear;
        private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
        private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
        private System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
        private System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
        private System.Data.SqlClient.SqlConnection sqlConnection1;
        private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
        private csnetdata.DataSet1 dataSet11;
        private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1;
        private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1;
        private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn2;
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();

            //
            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            //
        }

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.btnquit = new System.Windows.Forms.Button();
            this.btnsave = new System.Windows.Forms.Button();
            this.btnstart = new System.Windows.Forms.Button();
            this.tabPage2 = new System.Windows.Forms.TabPage();
            this.lstconsole = new System.Windows.Forms.ListBox();
            this.btnclear = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
            this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
            this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
            this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
            this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
            this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
            this.dataSet11 = new csnetdata.DataSet1();
            this.dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle();
            this.dataGridTextBoxColumn1 = new System.Windows.Forms.DataGridTextBoxColumn();
            this.dataGridTextBoxColumn2 = new System.Windows.Forms.DataGridTextBoxColumn();
            this.tabControl1.SuspendLayout();
            this.tabPage1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            this.tabPage2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
            this.SuspendLayout();
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                        this.tabPage1,
                                                                                        this.tabPage2});
            this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(640, 461);
            this.tabControl1.TabIndex = 0;
            // 
            // tabPage1
            // 
            this.tabPage1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                    this.textBox1,
                                                                                    this.dataGrid1,
                                                                                    this.btnquit,
                                                                                    this.btnsave,
                                                                                    this.btnstart});
            this.tabPage1.Location = new System.Drawing.Point(4, 21);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Size = new System.Drawing.Size(632, 436);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "Data Maintain";
            // 
            // dataGrid1
            // 
            this.dataGrid1.AlternatingBackColor = System.Drawing.Color.GhostWhite;
            this.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.dataGrid1.BackColor = System.Drawing.Color.GhostWhite;
            this.dataGrid1.BackgroundColor = System.Drawing.Color.Lavender;
            this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dataGrid1.CaptionBackColor = System.Drawing.Color.RoyalBlue;
            this.dataGrid1.CaptionForeColor = System.Drawing.Color.White;
            this.dataGrid1.DataMember = "forum_subject";
            this.dataGrid1.DataSource = this.dataSet11;
            this.dataGrid1.FlatMode = true;
            this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8F);
            this.dataGrid1.ForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.GridLineColor = System.Drawing.Color.RoyalBlue;
            this.dataGrid1.HeaderBackColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
            this.dataGrid1.HeaderForeColor = System.Drawing.Color.Lavender;
            this.dataGrid1.LinkColor = System.Drawing.Color.Teal;
            this.dataGrid1.Location = new System.Drawing.Point(8, 80);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.Lavender;
            this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.SelectionBackColor = System.Drawing.Color.Teal;
            this.dataGrid1.SelectionForeColor = System.Drawing.Color.PaleGreen;
            this.dataGrid1.Size = new System.Drawing.Size(616, 352);
            this.dataGrid1.TabIndex = 3;
            this.dataGrid1.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] {
                                                                                                    this.dataGridTableStyle1});
            // 
            // btnquit
            // 
            this.btnquit.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
            this.btnquit.Location = new System.Drawing.Point(232, 16);
            this.btnquit.Name = "btnquit";
            this.btnquit.Size = new System.Drawing.Size(104, 24);
            this.btnquit.TabIndex = 2;
            this.btnquit.Text = "Quit";
            this.btnquit.Click += new System.EventHandler(this.button4_Click);
            // 
            // btnsave
            // 
            this.btnsave.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
            this.btnsave.Location = new System.Drawing.Point(120, 16);
            this.btnsave.Name = "btnsave";
            this.btnsave.Size = new System.Drawing.Size(104, 24);
            this.btnsave.TabIndex = 1;
            this.btnsave.Text = "Save";
            this.btnsave.Click += new System.EventHandler(this.btnsave_Click);

回复人: mynewbigame(学习者) ( 一级(初级)) 信誉:100 2003-11-26 19:42:28 得分:0

继续:

            // 
            // btnstart
            // 
            this.btnstart.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
            this.btnstart.Location = new System.Drawing.Point(8, 16);
            this.btnstart.Name = "btnstart";
            this.btnstart.Size = new System.Drawing.Size(104, 24);
            this.btnstart.TabIndex = 0;
            this.btnstart.Text = "Start";
            this.btnstart.Click += new System.EventHandler(this.button2_Click);
            // 
            // tabPage2
            // 
            this.tabPage2.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                    this.lstconsole,
                                                                                    this.btnclear});
            this.tabPage2.Location = new System.Drawing.Point(4, 21);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Size = new System.Drawing.Size(632, 436);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "Console Display";
            // 
            // lstconsole
            // 
            this.lstconsole.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.lstconsole.BackColor = System.Drawing.Color.Black;
            this.lstconsole.ForeColor = System.Drawing.Color.LawnGreen;
            this.lstconsole.ItemHeight = 12;
            this.lstconsole.Location = new System.Drawing.Point(8, 48);
            this.lstconsole.Name = "lstconsole";
            this.lstconsole.Size = new System.Drawing.Size(616, 376);
            this.lstconsole.TabIndex = 1;
            // 
            // btnclear
            // 
            this.btnclear.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnclear.Location = new System.Drawing.Point(8, 16);
            this.btnclear.Name = "btnclear";
            this.btnclear.Size = new System.Drawing.Size(176, 23);
            this.btnclear.TabIndex = 0;
            this.btnclear.Text = "Clear";
            this.btnclear.Click += new System.EventHandler(this.button1_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(8, 48);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(616, 21);
            this.textBox1.TabIndex = 4;
            this.textBox1.Text = "server=LocalHost;uid=sa;pwd=;database=book_4";
            // 
            // sqlSelectCommand1
            // 
            this.sqlSelectCommand1.CommandText = "SELECT forum_subject_id, forum_subject_name FROM forum_subject";
            this.sqlSelectCommand1.Connection = this.sqlConnection1;
            // 
            // sqlInsertCommand1
            // 
            this.sqlInsertCommand1.CommandText = "INSERT INTO forum_subject(forum_subject_id, forum_subject_name) VALUES (@forum_su" +
                "bject_id, @forum_subject_name); SELECT forum_subject_id, forum_subject_name FROM" +
                " forum_subject WHERE (forum_subject_id = @forum_subject_id)";
            this.sqlInsertCommand1.Connection = this.sqlConnection1;
            this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@forum_subject_id", System.Data.SqlDbType.VarChar, 50, "forum_subject_id"));
            this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@forum_subject_name", System.Data.SqlDbType.VarChar, 50, "forum_subject_name"));
            // 
            // sqlUpdateCommand1
            // 
            this.sqlUpdateCommand1.CommandText = @"UPDATE forum_subject SET forum_subject_id = @forum_subject_id, forum_subject_name = @forum_subject_name WHERE (forum_subject_id = @Original_forum_subject_id) AND (forum_subject_name = @Original_forum_subject_name); SELECT forum_subject_id, forum_subject_name FROM forum_subject WHERE (forum_subject_id = @forum_subject_id)";
            this.sqlUpdateCommand1.Connection = this.sqlConnection1;
            this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@forum_subject_id", System.Data.SqlDbType.VarChar, 50, "forum_subject_id"));
            this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@forum_subject_name", System.Data.SqlDbType.VarChar, 50, "forum_subject_name"));
            this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_forum_subject_id", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "forum_subject_id", System.Data.DataRowVersion.Original, null));
            this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_forum_subject_name", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "forum_subject_name", System.Data.DataRowVersion.Original, null));
            // 
            // sqlDeleteCommand1
            // 
            this.sqlDeleteCommand1.CommandText = "DELETE FROM forum_subject WHERE (forum_subject_id = @Original_forum_subject_id) A" +
                "ND (forum_subject_name = @Original_forum_subject_name)";
            this.sqlDeleteCommand1.Connection = this.sqlConnection1;
            this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_forum_subject_id", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "forum_subject_id", System.Data.DataRowVersion.Original, null));
            this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_forum_subject_name", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "forum_subject_name", System.Data.DataRowVersion.Original, null));
            // 
            // sqlConnection1
            // 
            this.sqlConnection1.ConnectionString = "data source=XXK;initial catalog=book_4;persist security info=False;user id=sa;wor" +
                "kstation id=XXK;packet size=4096";
            // 
            // sqlDataAdapter1
            // 
            this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
            this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
            this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
            this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
                                    new System.Data.Common.DataTableMapping("Table", "forum_subject", new System.Data.Common.DataColumnMapping[] {
                                            new System.Data.Common.DataColumnMapping("forum_subject_id", "forum_subject_id"),
                                            new System.Data.Common.DataColumnMapping("forum_subject_name", "forum_subject_name")})});
            this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
            // 
            // dataSet11
            // 
            this.dataSet11.DataSetName = "DataSet1";
            this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN");
            this.dataSet11.Namespace = "http://www.tempuri.org/DataSet1.xsd";
            // 
            // dataGridTableStyle1
            // 
            this.dataGridTableStyle1.DataGrid = this.dataGrid1;
            this.dataGridTableStyle1.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] {
                                                                                                                    this.dataGridTextBoxColumn1,
                                                                                                                    this.dataGridTextBoxColumn2});
            this.dataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGridTableStyle1.MappingName = "forum_subject";
            // 

回复人: mynewbigame(学习者) ( 一级(初级)) 信誉:100 2003-11-26 19:42:39 得分:0

再继续:

            // dataGridTextBoxColumn1
            // 
            this.dataGridTextBoxColumn1.Format = "";
            this.dataGridTextBoxColumn1.FormatInfo = null;
            this.dataGridTextBoxColumn1.HeaderText = "帖子编号";
            this.dataGridTextBoxColumn1.MappingName = "forum_subject_id";
            this.dataGridTextBoxColumn1.Width = 88;
            // 
            // dataGridTextBoxColumn2
            // 
            this.dataGridTextBoxColumn2.Format = "";
            this.dataGridTextBoxColumn2.FormatInfo = null;
            this.dataGridTextBoxColumn2.HeaderText = "帖子主题";
            this.dataGridTextBoxColumn2.MappingName = "forum_subject_name";
            this.dataGridTextBoxColumn2.Width = 198;
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(640, 461);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                            this.tabControl1});
            this.Name = "Form1";
            this.Text = "Form1";
            this.tabControl1.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            this.tabPage2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
            this.ResumeLayout(false);
        }
        #endregion

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        //public use function
        private void ErrorHandler(string ErrorDescription)
        {
            MessageBox.Show (ErrorDescription);
        }

        private void ConsoleWrite(string strInput)
        {
            lstconsole.Items.Add  (strInput);
        }

        private int getchanges()
        {
            try
            {
                int ncount=0;
                DataSet modifiedds=this.dataSet11.GetChanges();
                if (modifiedds==null)
                {
                    return 0;
                }
                else
                {
                    foreach(DataRow r in modifiedds.Tables["forum_subject"].Rows)
                    {
                        if(r.RowState==DataRowState.Deleted)
                        {
                            ConsoleWrite(r["forum_subject_id",DataRowVersion.Original].ToString()+":"+r.RowState.ToString ()+":");
                            ++ncount;
                        }
                        if (r.RowState==DataRowState.Modified)
                        {
                            ConsoleWrite(r["forum_subject_id",DataRowVersion.Original].ToString()+":"+r.RowState.ToString()+":"+r["forum_subject_id",DataRowVersion.Current].ToString()+" "+r["forum_subject_name",DataRowVersion.Current].ToString());
                            ++ncount;
                        }
                        if (r.RowState==DataRowState.Added)
                        {
                            ConsoleWrite(r["forum_subject_id",DataRowVersion.Current].ToString()+":"+r.RowState.ToString()+":"+r["forum_subject_name",DataRowVersion.Current].ToString());
                            ++ncount;
                        }
                    }
                    return ncount;
                }
            }
            catch(System.Exception E)
            {
                ErrorHandler(E.ToString());
                return 0;
            }
        }

        private void button4_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            lstconsole.Items.Clear();
        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            try
            {
                this.sqlConnection1.ConnectionString=this.textBox1.Text;
                ConsoleWrite("create a new connection:["+this.textBox1.Text+"]");
                this.sqlDataAdapter1.Fill(this.dataSet11);
                ConsoleWrite("fill the dataset");
                btnstart.Enabled=false;
            }
            catch(System.Exception E)
            {
                ErrorHandler(E.ToString());
            }
        }

        private void btnsave_Click(object sender, System.EventArgs e)
        {
            //code No.1
            try
            {
                if (this.dataSet11.HasChanges())
                {
                    // this.sqlDataAdapter1.Update(this.dataSet11);
                    DataSet changedata=this.dataSet11.GetChanges();
                    this.sqlDataAdapter1.Update(changedata);
                    ConsoleWrite("SUM["+getchanges().ToString()+"] rows have been changed!");
                    MessageBox.Show("data modify success!");
                    ConsoleWrite("data modify success!");
                }
                else
                {
                    MessageBox.Show("no data need to modify!");
                    ConsoleWrite("no data need to modify!");
                }
            }
            catch(System.Exception E)
            {
                ErrorHandler(E.ToString());
            }
        }
    }
}

回复人: zhangjinshui200(望眼江湖) ( 一级(初级)) 信誉:100 2003-11-27 11:45:57 得分:0

是不是我不能够理解,
我想把DataGrid中得值,直接写回到数据库中,
而上面例子,都是告诉如何通过数据集,来更新数据库的呢?

该问题已经结贴 ,得分记录: polarlm (5)、 hare007 (5)、 keyplayer (10)、 smiletosky (5)、 heroniexiaowei (10)、 mynewbigame (15)、

Contributors: FHL