加载数据时如何忽略 ADO.NET Datatable.DataColumn 上的 MaxLength 约束
根据我使用数据集和数据表的经验,一个反复出现的问题是,在编译和发布数据集后更改数据库模式将导致约束检查失败。当然,这通常是一件好事。
然而,有时我想要一点灵活性。我通常希望它忽略 DataColumn.MaxLength 约束。
在这种情况下,我可以编译数据集以获得 TableA 的数据表,其中 Column1 是 MaxLength 20 的字符串。然后,几个月后,我决定最大长度 100 会更合适。如果我将数据库中 TableA.Column1 的定义从 varchar(20) 更改为 varchar(100),那么当我调用 DataAdapter 来 Fill() 这个 DataTable 时,它可能不会担心这些新的更长的内容,这会很好。 Column1 中的值。
我当前的代码会抛出异常。有没有一种方法可以编写代码,这样就可以忽略它?
A recurring problem in my experience when using Datasets and DataTables is that changing the database schema after the Dataset is compiled and released will cause constraint checking to fail. Of course, this is generally a good thing.
However, sometimes I would like a bit of flexibility here. I would usually like it to ignore the DataColumn.MaxLength constraint.
In this scenario, I could compile my dataset to have a datatable for TableA where Column1 is a String of MaxLength 20. Then, months later, I decide that a max length of 100 would be more appropriate. If I change the definition of TableA.Column1 in the datbase from varchar(20) to varchar(100), it would be nice that when I call a DataAdapter to Fill() this DataTable, that it might just not worry about these new longer values in Column1.
My current code would throw an exception. Is there a way to write the code so that this would would be ignored?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将要忽略的列的最大长度属性设置为 -1。这将清除最大长度限制,您将不再遇到这个问题。
Set the max length attribute on the columns you want to ignore to -1. This will clear the max length constraint and you will not have that issue any more.
按照以下步骤操作并更改 DataTable FieldName 的最大长度
转到您的
DataSet -->数据表 -->转到 Field 的属性 -->更改最大长度
Follow the below steps and change the Max Length of DataTable FieldName
Go to your
DataSet --> DataTable --> Go to properies of Field --> Change the Max length