对于更新Access数据库的操作,必须保持参数数组与sql语句中参数顺序一致,如下:
public bool Update(MyModel model)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("update table1 set ");
strSql.Append(" column1 = @column1 , ");
strSql.Append(" column2 = @column2, ");
strSql.Append(" where ID=@ID "); OleDbParameter[] parameters =
{
// 必须要这个顺序 ヾ(。 ̄□ ̄)ツ゜゜゜
new OleDbParameter("@column1", OleDbType.Integer, ),
new OleDbParameter("@column2", OleDbType.Integer, ),
new OleDbParameter("@ID", OleDbType.Integer, )
}; parameters[].Value = model.column1;
parameters[].Value = model.column1;
parameters[].Value = model.ID; int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
if (rows > )
{
return true;
}
else
{
return false;
}
}
如果改成下面这样,顺序不一致了,就妥妥的更新失败,还不报任何异常。。。。
public bool Update(MyModel model)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("update table1 set ");
strSql.Append(" column1 = @column1 , ");
strSql.Append(" column2 = @column2, ");
strSql.Append(" where ID=@ID "); OleDbParameter[] parameters =
{
// 顺序与sql语句不一致了 ヾ(。 ̄□ ̄)ツ゜゜゜
new OleDbParameter("@ID", OleDbType.Integer, ),
new OleDbParameter("@column1", OleDbType.Integer, ),
new OleDbParameter("@column2", OleDbType.Integer, )
}; parameters[].Value = model.ID;
parameters[].Value = model.column1;
parameters[].Value = model.column1; int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
if (rows > )
{
return true;
}
else
{
return false;
}
}