dtab.AcceptChanges(); //它会取消所以修改标识,这样一来,所以修改都不能通过SqlCommandBuilder取得了,所以这句不能要!SqlCommandBuilder会在myDataAdapter.Update之后自动执行AcceptChanges()的!

解决方案 »

  1.   

    to  wd_318(饭加加)
    我把dtab.AcceptChanges();去掉了,还是不行
      

  2.   

    LiSDN(LOCK ON) 
    说了不行了。有别的办法没
      

  3.   

    在更新之前,要 This.BindingContext["dataset","table1"].EndCurrentEdit();
      

  4.   


    myDataAdapter.Update(ds, myTableName);
    dtab.AcceptChanges();试试dtab.AcceptChanges();放在Update后面
      

  5.   

    This.BindingContext["dataset","table1"].EndCurrentEdit();
    应该是这样,再就是你的表没有主键!!!
      

  6.   

    rock1981() 
    不行,还是老样子
     michaelowenii(少年狂) 
    能说细点么,光加
    this.BindingContext[ds,myTableName].EndCurrentEdit();
    还是再需要加别的?这条加上后出错
    System.InvalidOperationException: 对于不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成。
       at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
       at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)
       at ddd.Form1.button1_Click(Object sender, EventArgs e) in c:\documents and settings\administrator\my documents\visual studio projects\ddd\form1.cs:line 149
      

  7.   

    lhx1977(清水无鱼) 
    的确是没有主键,这是为什么呢,怪,请老兄帮忙解释一下好不
      

  8.   

    在myDataAdapter.Update(ds, myTableName);
    前面加上
    myDataAdapter.UpdateCommand=cb.GetUpdateCommand();
    myDataAdapter.DeleteCommand=cb.GetDeleteCommand();
    myDataAdapter.InsertCommand=cb.GetInsertCommand();试试
      

  9.   

    建议还是用Command手动写Sql来更新数据库吧
      

  10.   

    加上
    myDataAdapter.UpdateCommand=cb.GetUpdateCommand();
    myDataAdapter.DeleteCommand=cb.GetDeleteCommand();
    myDataAdapter.InsertCommand=cb.GetInsertCommand();
    应该可以了吧,我比较赞成使用存储过程
      

  11.   

    我试了一下,不行,还是请高人指点
    .net被封装的东西比较多,myDataAdapter.update就被封装了,根本不知道他里面是怎么执行的,所以还是自己写sql吧
      

  12.   

    OK了
    1。去掉dtab.AcceptChanges();
    2。在myDataAdapter.Update(ds, myTableName);
    前面加上
    myDataAdapter.UpdateCommand=cb.GetUpdateCommand();
    myDataAdapter.DeleteCommand=cb.GetDeleteCommand();
    myDataAdapter.InsertCommand=cb.GetInsertCommand();^_^
      

  13.   

    我的测试代码,FYIusing System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Data.SqlClient;namespace DemoAcceptChanges
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class DemoAcceptChanges : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Label label2;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public DemoAcceptChanges()
    {
    //
    // 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.label1 = new System.Windows.Forms.Label();
    this.button1 = new System.Windows.Forms.Button();
    this.label2 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(72, 104);
    this.label1.Name = "label1";
    this.label1.TabIndex = 0;
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(64, 56);
    this.button1.Name = "button1";
    this.button1.TabIndex = 1;
    this.button1.Text = "Begin";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // label2
    // 
    this.label2.Location = new System.Drawing.Point(16, 104);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(48, 23);
    this.label2.TabIndex = 2;
    this.label2.Text = "Resault:";
    // 
    // DemoAcceptChanges
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(280, 157);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.label2,
      this.button1,
      this.label1});
    this.Name = "DemoAcceptChanges";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new DemoAcceptChanges());
    }
    private void RowsAcceptChanges()
    {
    try
    {
    string myConnection="server=HI4-SV05;DataBase=pubs;UID=sa;Password=aps;pooling=true";
    string mySelectQuery="SELECT * FROM authors";
    DataTable dtab= new DataTable();
    DataSet ds = new DataSet();
    DataRow dr;
    SqlConnection myConn = new SqlConnection(myConnection);
    SqlDataAdapter myDataAdapter = new SqlDataAdapter(mySelectQuery,myConn);
    SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter);
    //myDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    //--open Connection
    myConn.Open();
    myDataAdapter.Fill(ds,"pubs");

    //Make Changes
    dtab=ds.Tables["pubs"];
    dr=dtab.Rows[0];
    dr.BeginEdit();
    dr["au_lname"]="UserName";
    dr["zip"]="12345";
    dr["phone"]="13097777777";
    dr.EndEdit();
    //dtab.AcceptChanges();

    //Update DataBase
    myDataAdapter.UpdateCommand=cb.GetUpdateCommand();
    myDataAdapter.DeleteCommand=cb.GetDeleteCommand();
    myDataAdapter.InsertCommand=cb.GetInsertCommand();
    int i=myDataAdapter.Update(ds,"pubs");
    //int i=myDataAdapter.UpdateCommand.ExecuteNonQuery();
    myConn.Close();
    //--end //--Show Resault
    if(i>0)
    {
    this.label1.Text="Success!所影响的行数为:"+i;
    }
    else
    {
    this.label1.Text="Sorry!Failed!";
    }
    //--end Show
    }
    catch(Exception er)
    {
    throw er;
    }
    } private void button1_Click(object sender, System.EventArgs e)
    {
    try
    {
    this.RowsAcceptChanges();
    }
    catch(Exception er)
    {
    this.label1.Text=er.Message.Trim();
    }
    }
    public void d()
    {
    }
    }
    }