并发冲突:UpdateCommand 影响 0 个记录,看了很多帖子,没有发现解决了的。没有高手了吗
原代码如下:
public bool UpdateUpLoadDV(DataSet upLoadDV)
{
dsCommand = new OleDbDataAdapter();
//dsCommand.SelectCommand = new OleDbCommand();
//dsCommand.SelectCommand.Connection = new OleDbConnection(dvConnectString.ConnectionString);
//dsCommand.TableMappings.Add("Table", UpLoadDVInfo_TABLE);
if ( dsCommand == null )
{
throw new System.ObjectDisposedException( GetType().FullName );
}
//
// Get the command and update the database
//
dsCommand.UpdateCommand = GetUpdateCommand();
dsCommand.Update(upLoadDV,UpLoadDVInfo_TABLE);
//
// Check for table errors to see if the update failed.
//
if (upLoadDV.HasErrors )
{
upLoadDV.Tables[UpLoadDVInfo_TABLE].GetErrors()[0].ClearErrors();
return false;
}
else
{
upLoadDV.AcceptChanges();
return true;
}
}
//获得UpdateCommand得函数
private OleDbCommand GetUpdateCommand()
{
OleDbParameter parm;
if ( updateCommand == null )
{
updateCommand = new OleDbCommand("UPDATE UpLoadDVInfo SET iDI_IsChecked = @iDI_IsChecked,cDI_FileNameOld = @cDI_FileNameOld,cDI_ImageFileName =@cDI_ImageFileName WHERE iDI_ID =@iDI_ID " ,new OleDbConnection(dvConnectString.ConnectionString));
//
// Construct the command since we don't have it already
//
updateCommand.CommandType = CommandType.Text;
OleDbParameterCollection oleDbParams = updateCommand.Parameters;
parm = oleDbParams.Add(new OleDbParameter(iDI_ID_PARM,OleDbType.Integer));
oleDbParams.Add(new OleDbParameter(iDI_IsChecked_PARM,OleDbType.Integer));
oleDbParams.Add(new OleDbParameter(cDI_FileNameNew_PARM, OleDbType.VarChar, 200));
oleDbParams.Add(new OleDbParameter(cDI_FileNameOld_PARM, OleDbType.VarChar, 200));
oleDbParams.Add(new OleDbParameter(cDI_ImageFileName_PARM, OleDbType.VarChar, 200));
//
// Define the parameter mappings from the data table in the
// dataset.
//
oleDbParams[iDI_ID_PARM].SourceColumn = iDI_ID_FIELD;
oleDbParams[iDI_IsChecked_PARM].SourceColumn = iDI_IsChecked_FIELD;
oleDbParams[cDI_FileNameNew_PARM].SourceColumn = cDI_FileNameNew_FIELD;
oleDbParams[cDI_FileNameOld_PARM].SourceColumn = cDI_FileNameOld_FIELD;
oleDbParams[cDI_ImageFileName_PARM].SourceColumn = cDI_ImageFileName_FIELD;
parm.SourceVersion = DataRowVersion.Original;
}
return updateCommand;
}
其中,iDI_ID是自增字段。也是关键字段,
UPDATE UpLoadDVInfo SET iDI_IsChecked = @iDI_IsChecked,cDI_FileNameOld = @cDI_FileNameOld,cDI_ImageFileName =@cDI_ImageFileName WHERE iDI_ID =@iDI_ID
这个语句如果写成
UPDATE UpLoadDVInfo SET iDI_IsChecked = @iDI_IsChecked WHERE iDI_ID =@iDI_ID
没有问题,可以更新。但是更新2个以上字段就并发冲突了。
在线等候,希望能解决。
public bool UpdateUpLoadDV(DataSet upLoadDV)
{
dsCommand = new OleDbDataAdapter();
//dsCommand.SelectCommand = new OleDbCommand();
//dsCommand.SelectCommand.Connection = new OleDbConnection(dvConnectString.ConnectionString);
//dsCommand.TableMappings.Add("Table", UpLoadDVInfo_TABLE);
if ( dsCommand == null )
{
throw new System.ObjectDisposedException( GetType().FullName );
}
//
// Get the command and update the database
//
dsCommand.UpdateCommand = GetUpdateCommand();
dsCommand.Update(upLoadDV,UpLoadDVInfo_TABLE);
//
// Check for table errors to see if the update failed.
//
if (upLoadDV.HasErrors )
{
upLoadDV.Tables[UpLoadDVInfo_TABLE].GetErrors()[0].ClearErrors();
return false;
}
else
{
upLoadDV.AcceptChanges();
return true;
}
}
//获得UpdateCommand得函数
private OleDbCommand GetUpdateCommand()
{
OleDbParameter parm;
if ( updateCommand == null )
{
updateCommand = new OleDbCommand("UPDATE UpLoadDVInfo SET iDI_IsChecked = @iDI_IsChecked,cDI_FileNameOld = @cDI_FileNameOld,cDI_ImageFileName =@cDI_ImageFileName WHERE iDI_ID =@iDI_ID " ,new OleDbConnection(dvConnectString.ConnectionString));
//
// Construct the command since we don't have it already
//
updateCommand.CommandType = CommandType.Text;
OleDbParameterCollection oleDbParams = updateCommand.Parameters;
parm = oleDbParams.Add(new OleDbParameter(iDI_ID_PARM,OleDbType.Integer));
oleDbParams.Add(new OleDbParameter(iDI_IsChecked_PARM,OleDbType.Integer));
oleDbParams.Add(new OleDbParameter(cDI_FileNameNew_PARM, OleDbType.VarChar, 200));
oleDbParams.Add(new OleDbParameter(cDI_FileNameOld_PARM, OleDbType.VarChar, 200));
oleDbParams.Add(new OleDbParameter(cDI_ImageFileName_PARM, OleDbType.VarChar, 200));
//
// Define the parameter mappings from the data table in the
// dataset.
//
oleDbParams[iDI_ID_PARM].SourceColumn = iDI_ID_FIELD;
oleDbParams[iDI_IsChecked_PARM].SourceColumn = iDI_IsChecked_FIELD;
oleDbParams[cDI_FileNameNew_PARM].SourceColumn = cDI_FileNameNew_FIELD;
oleDbParams[cDI_FileNameOld_PARM].SourceColumn = cDI_FileNameOld_FIELD;
oleDbParams[cDI_ImageFileName_PARM].SourceColumn = cDI_ImageFileName_FIELD;
parm.SourceVersion = DataRowVersion.Original;
}
return updateCommand;
}
其中,iDI_ID是自增字段。也是关键字段,
UPDATE UpLoadDVInfo SET iDI_IsChecked = @iDI_IsChecked,cDI_FileNameOld = @cDI_FileNameOld,cDI_ImageFileName =@cDI_ImageFileName WHERE iDI_ID =@iDI_ID
这个语句如果写成
UPDATE UpLoadDVInfo SET iDI_IsChecked = @iDI_IsChecked WHERE iDI_ID =@iDI_ID
没有问题,可以更新。但是更新2个以上字段就并发冲突了。
在线等候,希望能解决。
...全文
相关推荐
C#中DataSet的用法(很详细)
DataSet是ADO.NET开发人员为方便数据处理开发出来的,是数据的集合。DataSet的功能:浏览、排序、搜索、过滤、处理分级数据、缓存更改等。还可以与XML数据互换。DataSet中可包括多个DataTable,可将多个查询结构存到一个DataSet中,方便操作.....
scott笔记.doc
scott教程的详细笔记,笔者精心制作。包括前16讲!
DataAdapter是如何更新数据库数据的
DataAdapter对象不仅只是捕获数据的功能,事实上它还可以帮助你将更新后的DataSet以批次的方式再写回后台数据库,而且还相当简单,只要调入Update方法,并传入准备更新的DataSet及DataTable即可。
C# Access数据库增删查改的简单方法
主要介绍了C# Access数据库增删查改的简单方法,有需要的朋友可以参考一下
数据库课程设计实战:从PowerDesigner建模到SQL Server与C#系统实现
数据库课程设计通常涉及概念模型、物理模型与应用程序的完整落地。其核心原理在于通过E-R图定义实体关系,再转化为带约束的物理表结构,并利用事务与锁机制保障数据一致性。技术价值在于将业务规则翻译为数据库约束与索引,避免脏数据与并发异常。典型应用场景包括图书借阅、学生选课等管理系统的开发。本文以图书借阅系统为例,详细讲解从PowerDesigner建模、SQL Server建库建表到Visual Studio中C#实现增删改查与事务处理的完整路径,并总结常见避坑经验。


