使用OleDbDataAdapter更新Access
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中的参数的顺序一致。
访问类型名称 | 数据库数据类型 | OLE DB 类型 | .NET Framework 类型 | 成员名称 |
文本 | VarWChar | DBTYPE _ WSTR | System.String | OleDbType.VarWChar |
备注 | LongVarWCha r | DBTYPE _ WSTR | System.String | OleDbType.LongVarWChar |
编号: 字节 | UnsignedTinyInt | DBTYPE _ UI 1 | System.Byte | OleDbType.UnsignedTinyInt |
是 / 否 | Boolean 类型的值 | DBTYPE_BOOL | System.Boolean | OleDbType.Boolean |
日期 / 时间 | 日期时间 | DBTYPE_DATE | System.DateTime | OleDbType.Date |
货币 | 小数 | DBTYPE_NUMERIC | System.Decimal | OleDbType.Numeric |
编号: 十进制 | 小数 | DBTYPE_NUMERIC | System.Decimal | OleDbType.Numeric |
编号: Double 类型的值 | 双击 | DBTYPE_R8 | System.Double | OleDbType.Double |
自动编号 (复制 ID) | GUID | DBTYPE_GUID | System.Guid | OleDbType.Guid |
号码: (复制 ID) | GUID | DBTYPE_GUID | System.Guid | OleDbType.Guid |
自动编号 (长整型) | 整数 | DBTYPE_I4 | System.Int 32 | OleDbType.Integer |
号码: (长整型) | 整数 | DBTYPE_I4 | System.Int 32 | OleDbType.Integer |
OLE 对象 | LongVarBinary | DBTYPE_BYTES | System.Byte 数组 | OleDbType.LongVarBinary |
编号: Single 类型的值 | 单个 | DBTYPE_R4 | System.Single | OleDbType.Single |
编号: Integer 类型的值 | smallint | DBTYPE_I2 | System.Int 16 | OleDbType.SmallInt |
二进制 | varbinary * | DBTYPE_BYTES | System.Byte 数组 | OleDbType.Binary |
超链接 | VarWChar | DBTYPE _ WSTR | System.String | OleDbType.VarWChar |