日期:2014-05-18  浏览次数:20484 次

SqlDataAdapter怎么更新操作
怎么用UpdateCommand、DeleteCommand、InsertCommond对dataset进行操作,然后update到数据库?
最好有例子学习。

------解决方案--------------------
C# code

UpdateMore
            DataSet ds = new DataSet();
            DataTable t1 = new DataTable("M");
            DataColumn newColumn1 = t1.Columns.Add("id", typeof(int));
            DataColumn newColumn2 = t1.Columns.Add("money", typeof(int));

            string[] id = textBox1.Text.Split(',');
            string[] money = textBox2.Text.Split(',');
            for (int i = 0; i < id.Length; i++)
            {
                DataRow newRow = t1.NewRow();
                newRow[0] = id[i];
                newRow[1] = money[i];
                t1.Rows.Add(newRow);

                newRow.AcceptChanges();
                newRow.SetModified();

            }
            ds.Tables.Add(t1);

            SqlConnection con = new SqlConnection("server=.;database=northwind;uid=sa");
            SqlCommand cmd = new SqlCommand("update moneytable set [money]=@money where id=@id", con);
            SqlParameter p1 = new SqlParameter("@id", SqlDbType.Int);
            p1.SourceColumn = "id";
            SqlParameter p2 = new SqlParameter("@money", SqlDbType.Int);
            p2.SourceColumn = "money";
            cmd.Parameters.Add(p1);
            cmd.Parameters.Add(p2);

            try
            {
                SqlDataAdapter da = new SqlDataAdapter();
                da.UpdateCommand = cmd;
                int num = da.Update(ds, "M");
                MessageBox.Show("Update" + num);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

------解决方案--------------------
http://www.cnblogs.com/tinachang021/articles/1274041.html
http://www.cnblogs.com/zhangzheny/archive/2007/10/27/939736.html
------解决方案--------------------
随便一本基础的书都会有这个例子