问题
I am trying to import a csv file and I need to get the maximum field length so I know how large to make my varchar field in the new table.
I have a small table with the longest text value of 315. Here is a logical description of what is happening:
If Len(Field) > 255 And RowNumOf315Record > 25 Then FieldLen = 255
If Len(Field) > 255 And RowNumOf315Record <= 25 Then FieldLen = 315
If we nuke all records with the field length > 255 then the correct length is always returned.
I tried Linq:
Table.Rows.Cast(Of DataRow)().Aggregate(0, Function(current, dRow) Math.Max(current, dRow("STAFF_ID_LAST_UPDT").ToString.Length))
I tried a direct query with ADODB:
Dim rsLength As New ADODB.Recordset
Dim connX As New ADODB.Connection
connX.Open("Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=d:\EnvironProcessing\Q2_2016\MN\ust\;Extensions=csv,tab,txt;")
rsLength.Open("select max(len([STAFF_ID_LAST_UPDT])) as l from [ff_other_liable_leaksites.txt]", connX)
rsLength.MoveFirst()
FieldLength = rsLength.Fields("l").Value
rsLength.Close()
I tried using Column.MaxLength:
SourceAdapter.Fill(Table)
SourceAdapter.FillSchema(Table, SchemaType.Source)
FieldLength3 = Int(Table.Columns(x).MaxLength / 2)
I know that ODBC determines the field type by looking at only the first 25 or so records so I get why FillSchema would fail. But when I am querying the entire table directly or with Linq with an aggregate function, it should actually read the entire table, right?
If I do a Sum or an Avg aggregate it reads the entire dataset.
来源:https://stackoverflow.com/questions/36759170/i-cant-get-correct-maxlenfieldname-of-csv-file-using-linq-sql-or-maxlength