using System.Data; using Validation.Core; namespace Validation.Rule.Common { /// /// 非空校验规则 /// public class NotNullValidationRule : IValidationRule { public string RuleName => "NotNull"; public string Description => "检查数据是否包含空值"; /// public ValidationResult Validate(DataTable dataTable) { var result = new ValidationResult { IsValid = true }; foreach (DataRow row in dataTable.Rows) { foreach (DataColumn col in dataTable.Columns) { if (row.IsNull(col)) { int rowIndex = dataTable.Rows.IndexOf(row); result.AddError(rowIndex, $"第{rowIndex + 1}行,列'{col.ColumnName}'不能为空"); } } } return result; } /// public ValidationResult Validate(string[] headers, int rowIndex, object[] values) { var result = new ValidationResult { IsValid = true }; for (int i = 0; i < headers.Length && i < values.Length; i++) { if (values[i] == null || values[i] == DBNull.Value) { result.AddError(rowIndex, $"第{rowIndex + 1}行,列'{headers[i]}'不能为空"); } } return result; } } }