try
{
MdbDAL dal = new MdbDAL();
DataTable dt = (DataTable)gv.DataSource;
IDbCommand cmd = dal.NewDbCommand( string .Format( " UPDATE {0} SET [Msg]=@Msg, [MsgChs]=@MsgChs, [MsgCht]=@MsgCht WHERE Id=@Id " , table));
OleDbParameter p = new OleDbParameter( " @Msg " , OleDbType.LongVarChar);
p.SourceColumn = " Msg " ;
cmd.Parameters.Add(p);
p = new OleDbParameter( " @MsgChs " , OleDbType.LongVarChar);
p.SourceColumn = " MsgChs " ;
cmd.Parameters.Add(p);
p = new OleDbParameter( " @MsgCht " , OleDbType.LongVarChar);
p.SourceColumn = " MsgCht " ;
cmd.Parameters.Add(p);
p = new OleDbParameter( " @Id " , OleDbType.BigInt);
p.SourceColumn = " Id " ;
cmd.Parameters.Add(p);
dal.Update(dt, cmd);
MessageBox.Show( " 保存成功! " , " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, " 错误 " , MessageBoxButtons.OK, MessageBoxIcon.Error);
}
在更新的过程中可能出现如下错误:{
MdbDAL dal = new MdbDAL();
DataTable dt = (DataTable)gv.DataSource;
IDbCommand cmd = dal.NewDbCommand( string .Format( " UPDATE {0} SET [Msg]=@Msg, [MsgChs]=@MsgChs, [MsgCht]=@MsgCht WHERE Id=@Id " , table));
OleDbParameter p = new OleDbParameter( " @Msg " , OleDbType.LongVarChar);
p.SourceColumn = " Msg " ;
cmd.Parameters.Add(p);
p = new OleDbParameter( " @MsgChs " , OleDbType.LongVarChar);
p.SourceColumn = " MsgChs " ;
cmd.Parameters.Add(p);
p = new OleDbParameter( " @MsgCht " , OleDbType.LongVarChar);
p.SourceColumn = " MsgCht " ;
cmd.Parameters.Add(p);
p = new OleDbParameter( " @Id " , OleDbType.BigInt);
p.SourceColumn = " Id " ;
cmd.Parameters.Add(p);
dal.Update(dt, cmd);
MessageBox.Show( " 保存成功! " , " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, " 错误 " , MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Data Type mismatch in criteria expression.
Concurrency violation: the UpdateCommand affected 0 of the expected 1 records
注意:数据类型要正确、SQL中的参数要和加入command中的参数的顺序一致。