最近在搞一个小程序,数据库使用的是MySQL 5.0,可是通过DataGrid进行数据更新时遇到了"并发冲突",请各位帮忙看一下.程序代码如下:
注:程序中被注释的是使用MS Sql Server 2000数据库,使用这个的操作一切正常,可以换成My SQL的数据库就报并发冲突错误了。期间我以为是MySQL的驱动问题,但换了两个"MySQLDriverCS"和"MySQL Connector Net 1.0.7"后都出现相同的情况,不知道何解。郁闷ing....
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using MySql.Data;
using MySql.Data.MySqlClient;
using MySql.Data.Types; 
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
public SqlConnection sqlCon;
public MySqlConnection mysqlCon;
public string sCon = "workstation id=localhost;Integrated Security=SSPI;database=CCB";
public string sMySqlCon = "Server=127.0.0.1;Port=3306;Database=Test;Uid=root;Pwd=nnisjim;";
public SqlDataAdapter sqlDA;
public MySqlDataAdapter mysqlDA;
public DataSet sqlDS = new DataSet();
/// <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 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
// 
// dataGrid1
// 
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 8);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(688, 280);
this.dataGrid1.TabIndex = 0;
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(152, 304);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// button2
// 
this.button2.Location = new System.Drawing.Point(280, 304);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(704, 341);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
//this.sqlCon = new SqlConnection(this.sCon);
//this.sqlDA = new SqlDataAdapter("select * from tt",this.sqlCon);
this.mysqlCon = new MySqlConnection(this.sMySqlCon);
this.mysqlDA = new MySqlDataAdapter("select * from tmp_import1", this.mysqlCon); //SqlCommand updateCmd = new SqlCommand("update tt set Col002=@Col002 where Col001=@Col001",this.sqlCon);
//updateCmd.Parameters.Add("@Col002",SqlDbType.VarChar,20,"Col002");
//SqlParameter idParm = new SqlParameter("@Col001",SqlDbType.Int,4,"Col001");
//updateCmd.Parameters.Add(idParm);
MySqlCommand mysqlUpdateCmd = new MySqlCommand("update tmp_import1 set C_KFBH=@C_KFBH where I_ID=@I_ID",this.mysqlCon);
mysqlUpdateCmd.Parameters.Add("@C_KFBH",MySql.Data.MySqlClient.MySqlDbType.VarChar,30,"C_KFBH");
MySqlParameter idParm = new MySqlParameter("@I_ID",MySql.Data.MySqlClient.MySqlDbType.Int16);
idParm.SourceColumn = "I_ID";
mysqlUpdateCmd.Parameters.Add(idParm); //this.sqlDA.UpdateCommand = updateCmd;
this.mysqlDA.UpdateCommand = mysqlUpdateCmd; //this.sqlDA.Fill(this.sqlDS,"tt");
this.mysqlDA.Fill(this.sqlDS,"tmp_import1"); dataGrid1.DataSource = this.sqlDS; //dataGrid1.SetDataBinding(this.sqlDS,"tt");
dataGrid1.SetDataBinding(this.sqlDS,"tmp_import1");
 

} private void button2_Click(object sender, System.EventArgs e)
{
try
{
//this.mysqlDA.Update(this.sqlDS.GetChanges(),"tt");
this.mysqlDA.Update(this.sqlDS.GetChanges(),"tmp_import1"); this.sqlDS.AcceptChanges();
}
catch(Exception ex)
{
throw ex;
}
}
}
}