验证数据表的多个列中是否存在值

发布于 2024-12-08 19:43:20 字数 1823 浏览 0 评论 0原文

我正在通过 asp.net C# Web 应用程序导入电子表格。我需要验证 excel 的结果 ->在将数据表结果传递给存储过程之前。如果枚举每个必需列的所有行来验证是否存在值,那么很短,是否有人有更快的解决方案?

实施詹姆士的建议;我最终做的是克隆原始表,它只克隆模式。然后我将所需的列设置为AllowDBNull = false。最后一步是在 TryCatch 语句内进行合并。如果合并失败,您将收到向用户抛出的必填字段验证错误。

   public DataTable UploadSpreadsheetData(string tempFile)
   {
        try
        {
            __filepath = tempFile;
            this.onConnectionStringChanged();
            string _sheetname = GetSheetName();
            DataTable _importedData = ReadTable(_sheetname);
            DataTable _newTableStructure = GetClone(_importedData);
            MergeDataTables(_importedData, _newTableStructure);
            return _newTableStructure;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            this.Connection.Close();
            this.Connection.Dispose();
            DeleteTempFile(tempFile);
        }
    }

    private DataTable GetClone(DataTable table)
    {
        DataTable _cloneTable  = table.Clone();
        _cloneTable.Columns["System Code"].AllowDBNull = false;
        return _cloneTable;
    }

    private static void MergeDataTables(DataTable _importedData, 
         DataTable _newTableStructure)
    {
        try
        {
            _newTableStructure.Merge(_importedData, true, MissingSchemaAction.Add);
        }
        catch (Exception ex)
        {
            // Add a reference to required value structure for the 
            // end user to verify validity of the spreadsheet
            throw new ApplicationException(String.Format("The following 
            error was encountered while importing the spreadsheet data. {0}. 
            Please check the spreadsheet to ensure all required values are 
            present.", ex.Message));
        }
    }

I am importing a spreadsheet through an asp.net C# web application. I need to validate the results of the excel -> datatable results before I pass it to the Stored Procedure. Short if enumerating all rows of each required column to validate that there is a value does anyone have a faster solution?

Implementation of Jame's suggestion; What I ended up doing was cloning the original table which only clones the schema. I then set the columns I wanted as AllowDBNull = false. Then the last step was to do a merge inside a TryCatch statement. If the merge failed then you will get a required field validation error thrown to the user.

   public DataTable UploadSpreadsheetData(string tempFile)
   {
        try
        {
            __filepath = tempFile;
            this.onConnectionStringChanged();
            string _sheetname = GetSheetName();
            DataTable _importedData = ReadTable(_sheetname);
            DataTable _newTableStructure = GetClone(_importedData);
            MergeDataTables(_importedData, _newTableStructure);
            return _newTableStructure;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            this.Connection.Close();
            this.Connection.Dispose();
            DeleteTempFile(tempFile);
        }
    }

    private DataTable GetClone(DataTable table)
    {
        DataTable _cloneTable  = table.Clone();
        _cloneTable.Columns["System Code"].AllowDBNull = false;
        return _cloneTable;
    }

    private static void MergeDataTables(DataTable _importedData, 
         DataTable _newTableStructure)
    {
        try
        {
            _newTableStructure.Merge(_importedData, true, MissingSchemaAction.Add);
        }
        catch (Exception ex)
        {
            // Add a reference to required value structure for the 
            // end user to verify validity of the spreadsheet
            throw new ApplicationException(String.Format("The following 
            error was encountered while importing the spreadsheet data. {0}. 
            Please check the spreadsheet to ensure all required values are 
            present.", ex.Message));
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂牵梦绕锁你心扉 2024-12-15 19:43:20

如果“缺失”列的值为 null (DBNull.Value),您可以执行以下操作:

// or whatever means you use to get your table...
DataTable dt = new DataTable();

// define your columns (whether imported or manual)

// set the columns that must have a value to deny DBNull, for example if col 3 & 4:
dt.Columns[3].AllowDBNull = false;
dt.Columns[4].AllowDBNull = false;

将这些列设置为 AllowDBNull = false code> 那么如果之前加载过数据,则将其设置为 false 时会出现异常,或者如果数据已加载,则在该行的 Add 上会出现异常设置为false后添加。

因此,如果可能的话,首先设置您的列,但如果这些列是在导入中定义的,只需在 try/catch 中将您的列设置为 AllowDBNull = false ,如果您捕获异常,您就知道您的情况该专栏有问题。

如果值是空白字符串,这当然不起作用。但如果那是你需要的,我可以挖掘更多......

If the values of the columns that are "missing" are null (DBNull.Value) you can do:

// or whatever means you use to get your table...
DataTable dt = new DataTable();

// define your columns (whether imported or manual)

// set the columns that must have a value to deny DBNull, for example if col 3 & 4:
dt.Columns[3].AllowDBNull = false;
dt.Columns[4].AllowDBNull = false;

Once those columns are set to AllowDBNull = false then you will get an exception when you set it to false if the data was loaded before, or you will get an exception on the Add of the row if the data is added after set to false.

So, if possible, set up your columns first, but if those are defined in the import, just set your columns to AllowDBNull = false in a try/catch, and if you catch an exception you know you have an issue in that column.

If the values are blank strings, this won't work of course. But if that's what you need I can dig more...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文