并发冲突:UpdateCommand 影响 0 个记录,看了很多帖子,没有发现解决了的。没有高手了吗

beast2078 2004-09-20 12:00:48
原代码如下:
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个以上字段就并发冲突了。
在线等候,希望能解决。
...全文
260 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Firestone2003 2004-09-20
  • 打赏
  • 举报
回复
解决并发冲突

执行用于在 Try...Catch 块中更新数据库的命令。
如果引发异常,则检查 Catch 语句的 Row 属性,以确定导致冲突的原因。
根据您的应用程序业务规则添加代码来解决错误。
try
{
SqlDataAdapter1.Update(myDataset);
}
catch (DBConcurrencyException ex)
{
string customErrorMessage;
customErrorMessage = "Concurrency violation\n";
customErrorMessage += ex.Row[0].ToString();
// Replace the above code with appropriate business logic
// to resolve the concurrency violation.
}
beast2078 2004-09-20
  • 打赏
  • 举报
回复
up
beast2078 2004-09-20
  • 打赏
  • 举报
回复
先顶了来
beast2078 2004-09-20
  • 打赏
  • 举报
回复
void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e)
{
// For bound columns, the edited value is stored in a TextBox.
// The TextBox is the 0th element in the column's cell.
TextBox oldFileNameText = (TextBox)e.Item.Cells[2].Controls[0];
TextBox hbFileNameText = (TextBox)e.Item.Cells[4].Controls[0];

String FilmName= e.Item.Cells[1].Text;
String oldFileName = oldFileNameText.Text;
String hbFileName = hbFileNameText.Text;

bool result;
DataRow checkDataRow = checkDataTable.Rows[e.Item.ItemIndex];
checkDataRow[upLoadDVData.cDI_FileNameOld_FIELD]=oldFileName;
checkDataRow[upLoadDVData.cDI_ImageFileName_FIELD]=hbFileName;
// Label1.Text=checkDataRow[upLoadDVData.iDI_ID_FIELD].ToString();
result=upLoadDVData.UpdateUpLoadDV(checkDataSet);

// With a database, use an update command to update the data. Because
// the data source in this example is an in-memory DataTable, delete the
// old row and replace it with a new one.
if(result)
{
checkDataGrid.EditItemIndex = -1;
BindGrid();
}
这是datagrid的update事件
beast2078 2004-09-20
  • 打赏
  • 举报
回复
: Firestone2003(笨笨小猪)
我按照你的方法做了,现在报错是这样的。
System.Data.OleDb.OleDbException: UPDATE 语句的语法错误。

Line 385: try
Line 386: {
Line 387: dsCommand.Update(upLoadDV,UpLoadDVInfo_TABLE);
Line 388: }
Line 389: catch (DBConcurrencyException ex)

beast2078 2004-09-20
  • 打赏
  • 举报
回复
决定用存储过程试试, access数据库能使用存储过程吗。
还有如果我更新cDI_FileNameOld = @cDI_FileNameOld这个字段。无论我输入任何字符串,数据库存的都是0
为什么
yepy7812 2004-09-20
  • 打赏
  • 举报
回复
可以用事务解决当前并发问题
dino2000 2004-09-20
  • 打赏
  • 举报
回复
你更新玩后调用一下dataset.acceptchanges()
这个方法可以试试,因为我在做程序的时候也碰你上面的问题。
acceptchanges()之后可以的。
cjzlxy 2004-09-20
  • 打赏
  • 举报
回复
不会吧,更新两个字段和一个字段是一样的,怎么可能出现并发??
lyhold 2004-09-20
  • 打赏
  • 举报
回复
你更新玩后调用一下dataset.acceptchanges()
heiding 2004-09-20
  • 打赏
  • 举报
回复
可否使用存储过程。

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧