Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining the field length of a column in a datatable

Tags:

c#

xml

datatable

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

like image 713
John S Avatar asked Nov 27 '25 22:11

John S


1 Answers

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

like image 57
marc_s Avatar answered Nov 29 '25 13:11

marc_s



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!