/// <summary> /// 验证SQL与传入参数是否匹配 /// </summary> private void ValidateSqlStrAndParams(string str, params object[] values) { // 断言传入SQL语句为null或不为空串 PreconditionAssert.IsNotEmptyString(str, ErrorMessages.CommandTextIsNullOrEmpty); // 断言传入的SQL参数与提供的参数值列表匹配 string sqlDetail = ErrorMessages.SqlParameterNotMatchValues + "\n" + DbAccessHelper.GetFormatErrorMsg(str, values); PreconditionAssert.CanFormatString(str, values, sqlDetail); }
private object DoGetRC1Value(DbCommand cmd) { try { return(cmd.ExecuteScalar()); } catch (DbException ex) { throw HandleDbAccessException(cmd.Connection, ex, cmd); } catch (Exception e) { string errorMsg = "获取RC1值失败" + "\r\n" + DbAccessHelper.GetDbCommandErrorMsg(cmd.Connection, cmd); throw new DbAccessException(errorMsg, e); } }
private int DoExecCommand(DbCommand cmd) { try { int result = cmd.ExecuteNonQuery(); return(result); } catch (DbException ex) { throw HandleDbAccessException(cmd.Connection, ex, cmd); } catch (Exception ex) { string errorMsg = "执行DbCommand失败" + "\r\n" + DbAccessHelper.GetDbCommandErrorMsg(cmd.Connection, cmd); throw new DbAccessException(errorMsg, ex); } }
private DataSet DoGetDataSetByCommand(DbCommand cmd) { using (DbDataAdapter adp = _engine.CreateDataAdapter()) { try { adp.SelectCommand = cmd; DataSet ds = new DataSet(); adp.Fill(ds); return(ds); } catch (Exception ex) { string errorMsg = "获取DataSet失败" + "\r\n" + DbAccessHelper.GetDbCommandErrorMsg(cmd.Connection, cmd); throw new DbAccessException(errorMsg, ex); } } }
protected override DbAccessException HandleDbAccessException(DbConnection db, DbException ex, params DbCommand[] cmdList) { var sqlEx = ex as SqlException; string errorMsg; switch (sqlEx.Number) { case 2627: errorMsg = "主键冲突" + "\n" + DbAccessHelper.GetDbCommandErrorMsg(db, cmdList); return(new DbAccessException(errorMsg, ex)); case 2601: errorMsg = "索引冲突" + "\n" + DbAccessHelper.GetDbCommandErrorMsg(db, cmdList); return(new DbAccessException(errorMsg, ex)); default: errorMsg = "抛出执行SQL命令错误异常" + "\n" + DbAccessHelper.GetDbCommandErrorMsg(db, cmdList); return(new DbAccessException(errorMsg, ex)); } }