对于更新Access数据库的操作,必须保持参数数组与sql语句中参数顺序一致,如下:
1 public bool Update(MyModel model) 2 { 3 StringBuilder strSql = new StringBuilder(); 4 strSql.Append("update table1 set "); 5 strSql.Append(" column1 = @column1 , "); 6 strSql.Append(" column2 = @column2, "); 7 strSql.Append(" where ID=@ID "); 8 9 OleDbParameter[] parameters = 10 { 11 // 必须要这个顺序 ヾ(。 ̄□ ̄)ツ゜゜゜ 12 new OleDbParameter("@column1", OleDbType.Integer, 4), 13 new OleDbParameter("@column2", OleDbType.Integer, 4), 14 new OleDbParameter("@ID", OleDbType.Integer, 4) 15 }; 16 17 parameters[0].Value = model.column1; 18 parameters[1].Value = model.column1; 19 parameters[2].Value = model.ID; 20 21 int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters); 22 if (rows > 0) 23 { 24 return true; 25 } 26 else 27 { 28 return false; 29 } 30 }
如果改成下面这样,顺序不一致了,就妥妥的更新失败,还不报任何异常。。。。
1 public bool Update(MyModel model) 2 { 3 StringBuilder strSql = new StringBuilder(); 4 strSql.Append("update table1 set "); 5 strSql.Append(" column1 = @column1 , "); 6 strSql.Append(" column2 = @column2, "); 7 strSql.Append(" where ID=@ID "); 8 9 OleDbParameter[] parameters = 10 { 11 // 顺序与sql语句不一致了 ヾ(。 ̄□ ̄)ツ゜゜゜ 12 new OleDbParameter("@ID", OleDbType.Integer, 4), 13 new OleDbParameter("@column1", OleDbType.Integer, 4), 14 new OleDbParameter("@column2", OleDbType.Integer, 4) 15 }; 16 17 parameters[0].Value = model.ID; 18 parameters[1].Value = model.column1; 19 parameters[2].Value = model.column1; 20 21 int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters); 22 if (rows > 0) 23 { 24 return true; 25 } 26 else 27 { 28 return false; 29 } 30 }