确定数据表中列的字段长度 [英] Determining the field length of a column in a datatable

查看:182
本文介绍了确定数据表中列的字段长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataTable,我已经通过Dataset.ReadXML()方法填充。我试图以编程方式确定datatable中每列的最大字段长度。我所有列的MaxLength值始终为-1。



有关如何确定正确的最大长度的任何想法或示例?可能根据表中的实际数据? (数据可以在25列乘以200,000+行范围)



C#2.0

解决方案

我不太明白你的目标是什么 - 你想找什么?给定列将使用的字节数(例如,对于INT为4,对于BIGINT为8,等等),或者例如实际的当前最大长度。列ColA中的所有字符串?



对于INT和其他数值和布尔类型 - 具有固定的系统给定长度 - 没有问题。 >

除非您的XML具有限制字符串长度的模式(XSD文件),否则XML中的字符串字段可以是任何长度,因此在读取数据后,您的DataTable可以't真的知道定义的最大长度可以是什么。



您可以做的是循环遍历DataTable中的所有行,并确定字符串的当前长度,并获取这些当前长度的最大值作为您的参考框架。



这是否有帮助?



Marc


I have a DataTable that I have populated via the Dataset.ReadXML() method. I am trying to programatically determine the maximum field lengths for each column in the datatable. The MaxLength value for all of my columns is always at the default of -1.

Any thoughts or examples on how to determine the proper max length? maybe based on the actual data in the table? (Datatable can be in the 25 column by 200,000+ row range)

C# 2.0

解决方案

I don't quite understand what your objectives are - what are you trying to find out? The number of bytes a given column will use (e.g. 4 for INT, 8 for BIGINT and so forth), or the actual current maximum length of e.g. all strings in column "ColA" ?

As for the INT and other numerical and boolean types - those have a fixed system-given length - no problems there.

Unless your XML has a schema (XSD file) which limits the string lengths, the string fields from an XML can be any length, really, so after reading them in, your DataTable can't really know what the defined max length can be.

All you can do is loop over all rows in your DataTable and determine the current length of the strings, and get the maximum of those current lengths, as your frame of reference.

Does that help at all?

Marc

这篇关于确定数据表中列的字段长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆