验证数据表的多个列中是否存在值
我正在通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果“缺失”列的值为
null
(DBNull.Value
),您可以执行以下操作:将这些列设置为
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:Once those columns are set to
AllowDBNull = false
then you will get an exception when you set it tofalse
if the data was loaded before, or you will get an exception on theAdd
of the row if the data is added after set tofalse
.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...