今天在寫程序,一直出現“當傳遞具有已修改行的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");
}
}
}