如何在VB.NET中获取datatable列的最大长度 [英] How to do get max length of datatable column in VB.NET
本文介绍了如何在VB.NET中获取datatable列的最大长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi,
For Each dc In ds.Tables(0).Columns
dc.ColumnMapping = MappingType.Attribute
writer.WriteAttributeString("dt", "type", "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882", _
GetDatatype(dc.DataType.ToString))
writer.WriteAttributeString("dt", "maxlength", "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882", _
dc.MaxLength.ToString)
writer.WriteAttributeString("rs", "maybenull", "urn:schemas-microsoft-com:rowset", _
dc.AllowDBNull.ToString)
writer.WriteEndElement()
writer.WriteEndElement()
writer.Flush()
Next
dc = Nothing
这是我的代码我试图获得数据类型列的数据类型和最大长度,但是 maxlength对所有列显示为-1 如何解决问题,任何人都给出建议。
我尝试过的事情:
我已经尝试了上面的代码,它给出了正确的数据类型,但MAxlength对所有列都显示为-1。
This is my code i tried to get Datatype and max length of datatable columns, its working but maxlength shows -1 for all columns how to solve the problem,any one give suggestion.
What I have tried:
I have tried the above code , its give correct datatype but MAxlength shows -1 for all columns.
推荐答案
据我所知,您无法从DataTable的DataColumn中获取底层数据库列的最大长度。文档说它被非文本列忽略: DataColumn.MaxLength属性(System.Data) [ ^ ]并且对于未指定长度的列,它返回-1。
并使用DataAdapter加载DataTable来自DB不会使基础SQL列长度超过 - 它为NVARCHAR(255)列报告-1,并允许我将列中的值设置为大于255.
我怀疑你必须直接查询底层数据源,而不是试图通过它提供的数据获取信息。
As far as a know, you can't get the max length of the underlying database column from the DataColumn of a DataTable. The documentation says that it's ignored for non-text columns: DataColumn.MaxLength Property (System.Data)[^] and that it returns -1 for columns with no length assigned.
And using a DataAdapter to load a DataTable from by DB doesn't bring the underlying SQL column length over - it both reports -1 for a NVARCHAR(255) column, and allows me to set the value in the column to greater than 255.
I suspect you will have to query the underlying data source directly instead of trying to get the info "second hand" via the data it supplied.
这篇关于如何在VB.NET中获取datatable列的最大长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文