如何防止异常值插入数据库?
我有一个 MS SQL DB 包含一组表,每个表代表根据我们的公式计算的变量集合。 所有变量都是具有预定义精度的数值(我们使用数值数据类型,其中 nm 作为整数部分的 n 位数字和小数部分的 m 位数字)。
我的问题是如何防止异常值或任何违反列大小的无效值? 目前我们正在做简单的“try catch”,因为 ADO.net 会抛出无效值的异常,有没有更好的方法? 此外,我想在此列中为此异常值设置一个有效值(即:可能为零)。 我正在使用 C#3、MSSQL 2000 并使用 SqlBulkCopy
类进行插入。
PS:我询问数据库端或 dotnet 端的任何解决方案
I have a MS SQL DB contains set of tables each table represents a collection of variables calculated based on our formulas.
All the variables are numeric with predefined percision (we are using numeric data type with n.m as n number of digits for integral part and m number of digits for fractional part).
My question is how to prevent outliers or any invalid values that violate the size of the columns?
Currently we are doing simple "try catch" as ADO.net throws an exception for invalid values, is there any better way?
Moreover, I want to set a valid value in this column (ie: may be zero) for this outlier.
I am using C#3, MSSQL 2000 and inserting using SqlBulkCopy
class.
P.S: I am asking about any solution from DB side or dotnet side
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我必须同意 MartinK 的观点,并在批量插入数据之前添加验证步骤。 这也将使您能够更好地对该过程进行单元测试。
如果您有多个应用程序向该表添加数据,则存储过程可能是另一种强制验证的解决方案。
但是,另一种选择是将批量插入到对数据类型有更宽松限制的临时/原始表中,然后使用 TSQL 将这些记录整理到主表中。
I'd have to agree with MartinK and put a validtion step in before your bulk insert for the data. This will allow you to better unit test this process as well.
If you have more than one application adding data to this table, a stored procedure may be another solution to enforce your validations.
But, another option would be to do your bulk insert into a temporary/raw table that has a more relaxed restriction on your data types then use TSQL to massage those records into the main table.
您可以尝试编写 PSQL 触发器/序列来验证您的数据类型关系!
有多少应用程序使用数据库中的相同数据? 如果这只是您的应用程序,您不应该考虑将异常逻辑移出中间层。
You can try to write PSQL trigger/sequences which validate your data type relations!
How much applications work with the same data in the database? If it's only your app, you shouldn't think about moving the exception logic out of the middle tier.
您正在描述我希望出现在应用程序设计中的验证和异常处理逻辑,而不是数据库设计中。 为什么它看起来不是最适合您处理的地方? 您使用哪种 DAL?
You're describing validation and exception handling logic that I would expect to be in the application design, not the database design. Why doesn't it seem like the easiest place to handle it for you? What kind of DAL are you using?