当传递具有已修改行的DataRow 集合时,更新要求有效的UpdateCommand?[原]

今天在写程序,一直出现“当传递具有已修改行的DataRow 集合时,更新要求有效的UpdateCommand?”
找了很久,才发现问题。

=====DBClass.cs======

using System;
using System.Data;
using System.Data.SqlClient;

namespace LinkSQL
{
 /// <summary>
 /// DBClass 的摘要说明。
 /// </summary>
 public class DBClass
 {
        private SqlConnection conn;
        public SqlDataAdapter da;
  public DataSet ds;
  public SqlCommand command;
  public SqlCommandBuilder objcmdBuilder;
  public DBClass()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  public void ConnectDB(){
          string ConnStr,Sql;
          ConnStr = "server=127.0.0.1;uid=sa;pwd=sa;database=pubs";
    conn = new SqlConnection(ConnStr);
   conn.Open();
    Sql = "select * from jobs";
    da = new SqlDataAdapter(Sql,conn);
          ds = new DataSet();
    da.Fill(ds,"jobs");
   objcmdBuilder = new SqlCommandBuilder(da);
   da.UpdateCommand = objcmdBuilder.GetUpdateCommand();
   da.InsertCommand = objcmdBuilder.GetInsertCommand();
   da.DeleteCommand = objcmdBuilder.GetDeleteCommand();
  }
  public void DoCommand(string Sql){
    command = new SqlCommand(Sql,conn);
          command.CommandType = CommandType.Text;
    command.ExecuteNonQuery();
   conn.Close();
  }
 }
}

==========调用文件=======

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

namespace LinkSQL
{
 /// <summary>
 /// frmMain 的摘要说明。
 /// </summary>
 public class frmMain : System.Windows.Forms.Form
 {
  private System.Windows.Forms.DataGrid dataGrid1;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.MenuItem menuItem1;
  private System.Windows.Forms.MenuItem menuItem2;
  private System.Windows.Forms.MenuItem menuItem3;
  private System.Windows.Forms.MainMenu mainMenu;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.Label label4;
  private DBClass DB;
  private System.Windows.Forms.TextBox btnNo;
  private System.Windows.Forms.TextBox btnName;
  private System.Windows.Forms.TextBox btnMin;
  private System.Windows.Forms.TextBox btnMax;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.Button button3;
  private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1;
  private System.Windows.Forms.Button button4;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

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

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

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }
  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.dataGrid1 = new System.Windows.Forms.DataGrid();
   this.dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle();
   this.button1 = new System.Windows.Forms.Button();
   this.mainMenu = new System.Windows.Forms.MainMenu();
   this.menuItem1 = new System.Windows.Forms.MenuItem();
   this.menuItem2 = new System.Windows.Forms.MenuItem();
   this.menuItem3 = new System.Windows.Forms.MenuItem();
   this.label1 = new System.Windows.Forms.Label();
   this.btnNo = new System.Windows.Forms.TextBox();
   this.btnMin = new System.Windows.Forms.TextBox();
   this.label2 = new System.Windows.Forms.Label();
   this.btnName = new System.Windows.Forms.TextBox();
   this.label3 = new System.Windows.Forms.Label();
   this.btnMax = new System.Windows.Forms.TextBox();
   this.label4 = new System.Windows.Forms.Label();
   this.button2 = new System.Windows.Forms.Button();
   this.button3 = new System.Windows.Forms.Button();
   this.button4 = new System.Windows.Forms.Button();
   ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
   this.SuspendLayout();
   //
   // dataGrid1
   //
   this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.dataGrid1.DataMember = "";
   this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Top;
   this.dataGrid1.FlatMode = true;
   this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
   this.dataGrid1.Location = new System.Drawing.Point(0, 0);
   this.dataGrid1.Name = "dataGrid1";
   this.dataGrid1.Size = new System.Drawing.Size(528, 240);
   this.dataGrid1.TabIndex = 0;
   this.dataGrid1.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] {
                          this.dataGridTableStyle1});
   //
   // dataGridTableStyle1
   //
   this.dataGridTableStyle1.DataGrid = this.dataGrid1;
   this.dataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
   this.dataGridTableStyle1.MappingName = "";
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(8, 312);
   this.button1.Name = "button1";
   this.button1.TabIndex = 1;
   this.button1.Text = "用SQL添加";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // mainMenu
   //
   this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                      this.menuItem1});
   //
   // menuItem1
   //
   this.menuItem1.Index = 0;
   this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                       this.menuItem2,
                       this.menuItem3});
   this.menuItem1.Text = "文件(&F)";
   //
   // menuItem2
   //
   this.menuItem2.Index = 0;
   this.menuItem2.Text = "断开数据库";
   //
   // menuItem3
   //
   this.menuItem3.Index = 1;
   this.menuItem3.Text = "退出";
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(4, 248);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(56, 16);
   this.label1.TabIndex = 2;
   this.label1.Text = "工作编号";
   //
   // btnNo
   //
   this.btnNo.Location = new System.Drawing.Point(68, 243);
   this.btnNo.Name = "btnNo";
   this.btnNo.TabIndex = 3;
   this.btnNo.Text = "edtNo";
   //
   // btnMin
   //
   this.btnMin.Location = new System.Drawing.Point(68, 272);
   this.btnMin.Name = "btnMin";
   this.btnMin.TabIndex = 5;
   this.btnMin.Text = "textBox2";
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(4, 280);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(56, 16);
   this.label2.TabIndex = 4;
   this.label2.Text = "最低工资";
   //
   // btnName
   //
   this.btnName.Location = new System.Drawing.Point(248, 240);
   this.btnName.Name = "btnName";
   this.btnName.TabIndex = 7;
   this.btnName.Text = "textBox3";
   //
   // label3
   //
   this.label3.Location = new System.Drawing.Point(192, 248);
   this.label3.Name = "label3";
   this.label3.Size = new System.Drawing.Size(56, 16);
   this.label3.TabIndex = 6;
   this.label3.Text = "工作名称";
   //
   // btnMax
   //
   this.btnMax.Location = new System.Drawing.Point(248, 272);
   this.btnMax.Name = "btnMax";
   this.btnMax.TabIndex = 9;
   this.btnMax.Text = "textBox4";
   //
   // label4
   //
   this.label4.Location = new System.Drawing.Point(192, 272);
   this.label4.Name = "label4";
   this.label4.Size = new System.Drawing.Size(56, 16);
   this.label4.TabIndex = 8;
   this.label4.Text = "最高工资";
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(104, 312);
   this.button2.Name = "button2";
   this.button2.TabIndex = 10;
   this.button2.Text = "dataGrid增加";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // button3
   //
   this.button3.Location = new System.Drawing.Point(200, 312);
   this.button3.Name = "button3";
   this.button3.TabIndex = 11;
   this.button3.Text = "保存";
   this.button3.Click += new System.EventHandler(this.button3_Click);
   //
   // button4
   //
   this.button4.Location = new System.Drawing.Point(328, 312);
   this.button4.Name = "button4";
   this.button4.TabIndex = 12;
   this.button4.Text = "button4";
   this.button4.Click += new System.EventHandler(this.button4_Click);
   //
   // frmMain
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(528, 393);
   this.Controls.Add(this.button4);
   this.Controls.Add(this.button3);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.btnMax);
   this.Controls.Add(this.btnName);
   this.Controls.Add(this.btnMin);
   this.Controls.Add(this.btnNo);
   this.Controls.Add(this.label4);
   this.Controls.Add(this.label3);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.dataGrid1);
   this.Controls.Add(this.button1);
   this.Menu = this.mainMenu;
   this.Name = "frmMain";
   this.Text = "数据库测试";
   this.Load += new System.EventHandler(this.frmMain_Load);
   ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
   this.ResumeLayout(false);

  }
  #endregion

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

  private void frmMain_Load(object sender, System.EventArgs e)
  {
   DB = new DBClass();
   DB.ConnectDB();
   dataGrid1.SetDataBinding(DB.ds,"jobs");
   //setDataBind();
  }
  private void setDataBind()
  {
   btnNo.DataBindings.Clear();
   btnNo.DataBindings.Add("Text",DB.ds,"jobs.job_id");
   btnName.DataBindings.Add("Text",DB.ds,"jobs.job_desc");
   btnMin.DataBindings.Add("Text",DB.ds,"jobs.min_lvl");
   btnMax.DataBindings.Add("Text",DB.ds,"jobs.max_lvl");
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   //SQL语句增加记录
   string sql;
   sql = "insert into jobs(job_desc,min_lvl,max_lvl) values('"+btnName.Text+"',"+btnMin.Text+","+btnMax.Text+")";
   try
   {
    DB.DoCommand(sql);
   }
   catch(Exception E)
   {
     MessageBox.Show(E.Message);
   }
  }

  private void button3_Click(object sender, System.EventArgs e)
  {
   DB.ConnectDB();
  }

  private void button2_Click(object sender, System.EventArgs e)
  {
   //在dateGrid上直接添加
   DataRow Row = DB.ds.Tables[0].NewRow();
   Row["job_desc"] = btnName.Text;
   Row["min_lvl"] = btnMin.Text;
   Row["max_lvl"] = btnMax.Text;
            DB.ds.Tables[0].Rows.Add(Row);
   try
   {
    DB.da.Update(DB.ds,"jobs");}//注意,一定要用SqlCommandBuiler
   catch(Exception E)
   {MessageBox.Show(E.Message);}
  }

  private void button4_Click(object sender, System.EventArgs e)
  {
   DB.da.Update(DB.ds,"jobs");
  }
 }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值